Google+

Monday, September 10, 2012

How to find Screen Size and Density of Device in Android programatically

Hi add this below code in on Create method and check your device or emulator screen configuration.




if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(Util.mContext, "Large Screen", Toast.LENGTH_SHORT).show();
Util.v("Screen Size : Large ");

} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
Toast.makeText(Util.mContext, "X Large Screen", Toast.LENGTH_SHORT).show();
Util.v("Screen Size : X Large ");

} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(Util.mContext, "Normal Screen", Toast.LENGTH_SHORT).show();
Util.v("Screen Size : Normal ");

}



//Determine density
   DisplayMetrics metrics = new DisplayMetrics();
       getWindowManager().getDefaultDisplay().getMetrics(metrics);
       int density = metrics.densityDpi;

       if (density==DisplayMetrics.DENSITY_HIGH) {
           Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("DENSITY_HIGH... Density is " + String.valueOf(density));
       }
       else if (density==DisplayMetrics.DENSITY_MEDIUM) {
           Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("DENSITY_MEDIUM... Density is " + String.valueOf(density));
       }
       else if (density==DisplayMetrics.DENSITY_LOW) {
           Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("DENSITY_LOW... Density is " + String.valueOf(density));
       }
       else {
           Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW.  Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
           Util.v("Density is neither HIGH, MEDIUM OR LOW... Density is " + String.valueOf(density));
       }

Monday, July 16, 2012

How to resolve Conversion to Dalvik format failed with error 1


Many times we faced problem of "Conversion to Dalvik format failed with error 1"


When i went through Google and found many solutions but below has solved my problem.



  1. In Eclipse Package Explorer, Right Click on Project -> Select Properties -> Select "Java Build Path" -> and remove all the JARs from library. Then click OK. Project will show you an error.
  2. Then as usual your try fix project property and clean project.
  3. Then again follow the above 1st steps to add your JARs.
  4. Now follow again 2nd step to clear and clean your project.
Hurreeee,
Your problem has been resolve. Run Project and enjoy Coding



Tuesday, April 10, 2012

How to find Android Unique Device ID or UDID - Part 2

In the previous post 
http://smartphonebysachin.blogspot.in/2011/09/how-to-find-android-unique-device-id-or.html
we had seen only how to get ANDROID_D.


Now We will see the Difference Between Android ID & Device ID of Android Phone

Android ID : 
ANDROID_ID is a 64-bit number (as a hex string) that is RANDOMLY GENERATED on the device's first boot and should remain constant for the lifetime of the device.(The value may change if a factory reset is performed on the device.)
Device ID :
Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.


Here is sample Code to get UDID


public class DemoActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    TelephonyManager tm = (TelephonyManager)           this.getSystemService(Context.TELEPHONY_SERVICE);


    Log.d("ID", "Android ID: " + Secure.getString(getContentResolver(), Secure.ANDROID_ID));
    Log.d("ID", "Device ID : " + tm.getDeviceId());


}
}


For getting Device ID you have to set following permission in AndroidMenifest.xml


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


For getting ANDROID_ID you don't need to set any permission.








ANDROID_ID seems a good choice for a unique device identifier.
Google+