Create AlertDialog – Android

Home / Android / Create AlertDialog – Android

Sometimes it is the requirement of the application to provide information to the user, or to get some input from the user, or to display a list of information in formatted layout. AlertDialog can be used for these and other similar purposes. For different purpose there is different process to create it.

Some process is discussed here, and the rest can be derived from these as per developers requirement(s).


1. AlertDialog to display information:

For solution of this problem, following demo program can be used.

main_activity.xml



    




MainActivity.java

package com.pcsalt.example.alertdialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	
	Button btnAlertDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnAlertDialog = (Button) findViewById(R.id.btn_AlertDialog);
		
		btnAlertDialog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				displayAlertDialog();
			}
		});
	}
	
	public void displayAlertDialog() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Title");
		alert.setMessage("Message for user");
		alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		AlertDialog dialog = alert.create();
		dialog.show();
	}
}

Screenshot

AlertDialog Demo Activity AlertDialog Message Dialog

2. To display and get confirmation from user

3. To display a formatted layout with some components like EditText, CheckBox, etc.

  • krishna dhas

    Hi.. i develop one app. that app i used baseadapter. it’s working eclipse. then i create new project android studio but the android studio project using base adapter getView function not called or run .


    public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Log.e("getView",""+"getView");
    if (convertView == null) {
    convertView = inflater.inflate(R.layout.chat_listview_layout_screen , parent, false);
    }
    setAttributes(position, convertView);
    convertView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    click(position);
    }
    });
    return convertView;
    }

    • Hi Krishna,

      Could you please post more of the code (here or GitHub or Gist)? I do not see any problem in this code block, other than final int position. In guidelines, they have said not to make parameters of getView() as final. If you could post the source code of the CustomBaseAdapter you are using, then I could suggest the problem with it.

      You could also refer to ListView using BaseAdapter – Android for more details. This tutorial is created using Android Studio.

      Thank you.

      • krishna dhas

        Really Thanks. It’s too useful for my work………….

        • I am glad, it worked out for you. Happy to help.

          • krishna dhas

            Hi, You have any idea about exact location tracking? now me using one method but that method sometimes get Tower location latitude and longitude other time it works perfectly (GPS Connect time)

          • Hi, if I am right, then you would like to get device location in Android. Sometimes you get tower location but sometimes it provides correct location. And, GPS takes time to connect.

            I would suggest you to try FusedLocationProviderApi. It will provide you the device location (only if location tracking is enabled for the device) with the accuracy level you have set. You can find more here https://developer.android.com/training/location/index.html

          • krishna dhas

            thanks. i have one doubt. if Gps not connected(that means gps on but tower not get properly) that scenario. how to show latitude and longitude?

          • You can display previously cached/stored Latitude and Longitude. And, display a dialog to ask user to turn on the GPS.