Monday, 29 December 2014

Dynamic pixel calculating in android

How to calculate dynamically pixel in android.

In android device/tablet screen size are too deferment. So as developer to hard to make code for all. In some case make UI dynamic, at that time this will help.

In this method input dp, and method will return in pixel.
This will help in making dynamic UI. like TextView, EditText, ImagesView...etc.


1
2
3
4
5
6
7
8
9
/**
  * convert dp to pixel
  * @param dp
  * @return pixel of dp
  */
 private int getPixel(int dp){
  Resources r = getResources();
  return  (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
 }

Sunday, 28 December 2014

Set application install location in android

In android, provision to set application install location.

you have to set only one property in manifest file "android:installLocation". This is recommended if application size is heavier then normal application.

There are there type in this property, 

By default, the system will not allow your application to install on the external storage, so you don't need to worry about your existing applications.
  1. "android:installLocation="auto""
    • If you declare "auto", you indicate that your application may be installed on the external storage, but you don't have a preference of install location.
  2. "android:installLocation="preferExternal""
    • If you declare "preferExternal", you request that your application be installed on the external storage, but the system does not guarantee that your application will be installed on the external storage. If the external storage is full, the system will install it on the internal storage. The user can also move your application between the two locations.
    • Some of the condition to applications that should not install on external Storage, for more information please click here.
    • Some of the condition to applications that should install on external Storage, for more information please click here.

  3. android:installLocation="internalOnly"
    • By default, the system will not allow your application to install on the external storage, so you don't need to worry about your existing applications. However, if you're certain that your application should never be installed on the external storage, then you should make this clear by declaringandroid:installLocation with a value of "internalOnly". 
Example.


1
2
3
4
5
6
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.YOURPACKAGE NAME"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="internalOnly"
     >

Further reference please click here.

Check android network connectivity (wi-fi / data plan)

How to determine android device connection.

In android device, so easy to determine android device connection type. This will help to improve application performance. for example if application


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 /**
  * <a>This method check Internet connection status.
  * If Internet connection not available return 0.
  * 1 for wi-fi connection,
  * 2 for mobile data plan.
  * @param activity
  * @return connection status, 1-wifi,2-data plan, 0- no internet connection
  */
 public static int checkNetworkStatus(final Activity activity) {
  int flag=0;
  // Get connect mangaer
  final ConnectivityManager connMgr = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);
  // check for wifi
  final android.net.NetworkInfo wifi =  connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

  // check for mobile data
  final android.net.NetworkInfo mobile =  connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

  /**
   * wifi connection
   */
  if( wifi.isAvailable() ){
   flag=1;
  }  
  /**
   * mobile dataplan
   */
  else if( mobile.isAvailable() ){
   flag=2;
  }  
  return flag;
 }  

You have to add permission in manifest file.

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

Check Internet connection in Android

Check Internet connection in Android

Just use of this method, checking that android device have internet connection or not.
This method return true, if Internet connection is available in android device, else return false.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionStatus {
 /**
  * <a>This method return true if the Internet connection are available else false</a>
  * @param context
  * @return return true if the Internet is available else false.
  */

 public  static boolean isOnline(Context context)
 {
  boolean status = false;
  try
  {
   ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
   if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED)
   {
    status = true;
   }
   else
   {
    netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED)
    {
     status = true;

    }
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
   return false;
  }
  return status;

 }
}

You have to add permission in manifest file.

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