mytechead

Android & Wordpress Help center

Archive for the month “August, 2011”

How to create alert dialog box in android??

This post explains, how to create alert dialog box in android:

You can use the following code to create an alert dialog box:

String positiveButtonText = "OK";
String negativeButtonText = "Cancel";

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("This is my tittle");
alert.setMessage("This is body of my alert box");

//To add buttons
alert.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//Do some onClick handling here
}
});

alert.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//Do some onClick handling here
}
});

alert.show();

Note: .show() method displays the alert dialog box, if you forget to add it, your alert box will not be visible ever!!

Advertisements

How to prevent Alert Dialog box from closing when back button is pressed?

In order to prevent Alert Dialog Box from closing, use the following piece of code:

ObjectOfAlertDialogBox.setCancel(false)

Note: Here ObjectOfAlertDialogBox is your AlertDialog box’s object which is used to create AlertDialog box as explained here.

How to hide tittle of Android activity?

Title bar of an android activity can be hidden using the following piece of code in the onCreate() method of android activity :

requestWindowFeature(Window.FEATURE_NO_TITLE);

How to get information about device’s ANDROID BUILD ?

This post explains how to get your android’s BUILD details viz. BOARD number, CPU type, DEVICE type, MODEL number, MANUFACTURER name etc etc..

The code below can be used to obtain all the BUILD related details:

String address = "BOARD: " + Build.BOARD + " BRAND: " + Build.BRAND + " CPU: " +Build.CPU_ABI + " DEVICE: "+ Build.DEVICE + " DISPLAY: " + Build.DISPLAY + " HOST:" + Build.HOST + " ID:" + Build.ID + " MANUFACTURER: " + Build.MANUFACTURER + " MODEL: "+Build.MODEL + " PRODUCT: "+Build.PRODUCT + "TAGS: " + Build.TAGS + " TYPE:" + Build.TYPE + " USER: " + Build.USER + " RELEASE:" + Build.VERSION.RELEASE;

address = address + " MANUFACTURER: " + Build.MANUFACTURER + " MODEL: "+Build.MODEL + " PRODUCT: "+Build.PRODUCT + " TYPE: " + Build.TYPE;

How to get IMEI number of android device?

IMEI number is another way of uniquely identifying cellular devices.
Each cellular device has a unique IMEI number which can never clash with another device’s IMEI number.

Android devices [phones only] also have IMEI number.

For more information about IMEI number visit this link

The following code helps in obtaining IMEI number of android devices :
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

String device_id = tm.getDeviceId();

Permissions required in Android Manifest:
android.permission.READ_PHONE_STATE

NOTE: In case of tablets or devices which can’t act as Mobile Phone IMEI will be null.

How to get ANDROID_ID of an android device?

This tutorial explains how to get ANDROID_ID of android devices.

ANDROID_ID is a unique string that can be used to identify your device. Though there are many pros and cons of the same. Some properties of ANDROID_ID are :

1) This is a 64-bit quantity that is generated and stored when the device first boots.
2) It is reset when the device is wiped.
3) It is not 100% reliable on releases of Android prior to 2.2 (“Froyo”).
4) Also, there has been at least one widely-observed bug in a popular handset from a major manufacturer, where every instance has the same ANDROID_ID.

So developers are adviced to use it keeping in mind all the above mentioned points.

The code below can be used to obtain ANDROID_ID :


String android_id = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);


Post Navigation