Display Alert on Back button Pressed in Android Studio

Display Alert on Back button Pressed in Android Studio

Description: When user wants to exit from application, he is prompted to exit from application.
Complexity Level: Beginner
Keywords: back, back button, android, android studio, alert, dialog, onbackpress

                Alert means to get user’s input when needed. Most of us played games and when we try to exit it by pressing “Back” button in Android phone, it ask for “Do you really want to exit?”.             So, in this article we are going to learn how to prevent user to exit from application without giving response. 

Step 1:
Create a new Project with following parameters.


Step 2:
Open your Main Activity file, in my case it is “BackPressActivity” and paste following code inside.
===============
BackPressActivity
===============
package com.example.alertonbackpressdemo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class BackPressActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);
        builder.setMessage("Do you want to Exit?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //if user pressed "yes", then he is allowed to exit from application
                finish();
            }
        });
        builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //if user select "No", just cancel this dialog and continue with app
                dialog.cancel();
            }
        });
        AlertDialog alert=builder.create();
        alert.show();
    }   
}

Let’s understand Alert Dialog and its different methods.
In code there is written “builder.setCancelable(false);” which means that user can’t cancel this dialog by pressing back again or touch outside the alert dialog box and dismiss it. So user has to press one of the options you have provided to him.
Next is “builder.setMessage("Do you want to Exit?");”. Here you can write your own message which will be displayed to user in alert.
builder.setPositiveButton” will set a positive button in left side. Parameter will accept the name of that button as you can see in code is “Yes”. Same thing is set for “builder.setNegativeButton”. When you set these buttons, you need to pass a listener which will be fired when user click one of the button.
Step 3:
Run your application.


Summary
 In this article, we learned “How to show Alert Dialog” and “Show dialog at BACK button pressed”.

Android Unique Identification Number


How to get android unique id number?

Purpose
Some times, when we are integrate our android application with server, its requirement of server that it can identify every android device uniquely. So to do that, we will learn how to get android id from device.

Android IdA 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.)

Code
package com.did;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.util.Log;

public class DevideIdActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        String android_id = Secure.getString(this.getContentResolver(),
                Secure.ANDROID_ID);
       
        Log.d("Android","Android ID : "+android_id);
    }
}


 Output

download.jpg