Create Dialog with options, using AlertDialog.Builder

Hi Guys!

In this tutorial I am sharing the code about the Alert Dialog with select option in android.
More details about the Alert Dialog visit the android developer site Alert Dialog

Lets start the coding part.

activty_main.xml

 <Linearlayout android:id="@+id/LinearLayout1" 
 android:layout_height="match_parent" 
 android:layout_width="match_parent" 
 android:orientation="vertical"  
 android:paddingbottom="@dimen/activity_vertical_margin" 
 android:paddingleft="@dimen/activity_horizontal_margin"
 android:paddingright="@dimen/activity_horizontal_margin"
 android:paddingtop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools">

    <Textview 
     android:id="@+id/textView1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"
     android:text="@string/hello_world"/>

    <Button 
     android:id="@+id/button_alert"
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:text="Alert Box"/>

</Linearlayout>

MainActivity.java

package com.sunil.alertdialogwithoption;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        Button buttonStartDialog = (Button)findViewById(R.id.button_alert);
        buttonStartDialog.setOnClickListener(new Button.OnClickListener(){
 
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    StartDialog();
   }});
    }
     
    private void StartDialog(){
     AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
     myAlertDialog.setTitle("My Alert Dialog");
     myAlertDialog.setMessage("It provide options for user to select");
     myAlertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        
            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_LONG).show();
            }
        });
     myAlertDialog.setNeutralButton("Option 1", new DialogInterface.OnClickListener() {
       
            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(getApplicationContext(), "'Option 1' button clicked", Toast.LENGTH_LONG).show();
            }
        });
     myAlertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
       
            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_LONG).show();
            }
        });
     myAlertDialog.show();
    }
 
}

 You can download the source code Alert Dialog.

Cheers Guys! 

1 comment:

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me

Name

Email *

Message *