Pages

Thursday, July 17, 2014

Saturday, April 12, 2014

Hide Sofkeyboard when touched outside EditText in Android

Most of the times we will be entering some values using the EditText while entering values we will be using the softkeyboard and some times we may need to hide the softkeyboard when the user touches outside the the EditText. The following code will implement the functionality where in when the user scrolls or selects outside of the EditText, the softkeyboard will be hidden to the user.

The following code should be in the your activity class for hiding the softkeyboard on the outside click of EditText or while scrolling the UI

@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
            if (getCurrentFocus() != null) {
                  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }
        return super.dispatchTouchEvent(ev);
    }

Thursday, November 28, 2013

How to findout unused resources in Android Project

1. Download the AndroidUnusedResources.jar from the following link https://code.google.com/p/android-unused-resources/

2. Once the download is done , copy the AndroidUnusedResources.jar to your project Folder.

3.Open the command prompt and navigate to your project Folder










4. Than type the following command for generating the list of unused resources in your Android Project
java -jar AndroidUnusedResources.jar


Wednesday, November 27, 2013

How to monitor network connectivity using BroadCastReceiver in Android

 1. Now a days most of applications uses INTERNET in their Application. So most of the times we need to know whether the INTERNET is available or not before making any network call..

2. INTERNET Connection might change anytime – and it probably will, given the volatility of a mobile environment.

3. Thankfully Android’s ConnectivityManager allows you to register for a broadcast event that is fired whenever this happens

4. The Source Code for the registering the Broadcast Receiver for finding out the  Network Connectivity is given below

public class Reachability {
    public boolean isReachable = false;
    public boolean isReceiving = false;
   
    private BroadcastReceiver receiver = null;
   
    public static Reachability reachability = null;
    static {
        reachability = new Reachability();
    }
   
    public static boolean registerReachability(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
       
        if (reachability.isReceiving) {
            context.unregisterReceiver(reachability.receiver);
            reachability.isReceiving = false;
        }
       
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        reachability.isReachable = info != null && info.isAvailable() && info.isConnected();
       
        reachability.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectivityManager = (ConnectivityManager) context
                                .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo info = connectivityManager.getActiveNetworkInfo();
                reachability.isReachable = info != null && info.isAvailable() && info.isConnected();
            }
           
        };
        context.registerReceiver(reachability.receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        reachability.isReceiving = true;
       
        return reachability.isReachable;
    }
}

5. In the next Step you need to register the BroadCast Receiver for finding out the  Network Connectivity in the Main Application class which extends Application as given below

public class ELSApplication extends Application {
    @Override
    public void onCreate() {

        Reachability.registerReachability(this.getApplicationContext());
      
    }
   
}

6. So whenever your are making the Network call , using the following code  you can check whether the INTERNET is available or not

 if (!Reachability.reachability.isReachable) {
                Toast.makeText(getActivity(), "No Internet", Toast.LENGTH_SHORT).show();
            } else {
                String[] url = { "https://www.dropbox.com" };
                DownloadFileAsync dloadFAsync = new DownloadFileAsync();
                dloadFAsync.execute(url);
            }

7. And the following permissions need to be added in the AndroidManifestFile

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Tuesday, November 26, 2013

How to add ssh key to bitbucket using Windows

1. To generate the ssh key go through the following blog
http://mohan-android.blogspot.in/2013/11/how-to-generate-ssh-keys-using-git-on.html

2. Once the ssh key is generated copy the ssh key and log in to you bitbucket account and select Manage Account and select SSH Keys

3. Select Add key , give the label name in Label section  and paste the sshkey  in the key Section


















4. Once this is done, select Add key.

Screen Recording in Android 4.4

1. In order to perform a screenrecord, you’ll need the Android SDK and a familiarity with adb command.

2. On your Android device running KitKat, enable USB Debugging from Developer Options, which is hidden by default. To enable Developer options, head over to Settings -> About Phone/Tablet and tap on the Build Number seven times, after which you will get a toast notification saying “You’re now a Developer.”
Head over to Settings -> Developer Options now, and then enable the ‘USB Debugging’ option. If you also want your on-screen touches to show in the video, enable the ‘Show touches’ option as well.

3. You are now ready to record a video. Simply connect the Android device to your PC and start a new instance of Command Prompt in Windows or Terminal on Mac. You will now need to navigate to the android sdk folder.

4.  Now, as soon as you enter the command below, the screen recording will start.
adb shell screenrecord /sdcard/filename.mp4
5. You can further customize the speed at which it captures video by using the following command
adb shell screenrecord --bit-rate 8000000 /sdcard/kitkat.mp4

The above command would record at 8Mbps, instead of the default 4Mbps and save it to the SD Card on your device with the name of KitKat. 

6. You can further customize the length of time it records (the default duration is 3-minutes) using the following command
adb shell screenrecord --time-limit 30 /sdcard/kitkat.mp4

The above command would record for 30s, instead of the default 3 Minutes and save it to the SD Card on your device with the name of KitKat. 
7. Other information can be found in the following link

http://developer.android.com/tools/help/adb.html#screenrecord


Thursday, November 21, 2013

How to generate ssh keys using git on windows

1.To generate the ssh key first download the git from from the following link http://code.google.com/p/msysgit/downloads/list

2. Open Git Bash that you just installed (Start->All Programs->Git->Git Bash)



 3. Type in the following : ssh-keygen-t rsa



4. Than it will ask for the file name in which it needs to be saved . Just press Enter.

5.Than it will ask you  for entering passphrase. Just press Enter.



6.Than it will ask you  for reentering passphrase. Just press Enter.


  7. Once the steps mentioned above is done, the ssh key will be generated in the following path C:\Users\--UserName\.ssh Folder





8. The ssh key will be generated in the id_rsa File which is of .pub Format.