Cdacians

Cdacians
Cdacians

Tuesday, 7 August 2012

Create Dialog with options, using AlertDialog.Builder


Create Dialog with options, using AlertDialog.Builder

Dialog with options

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.AndroidAlertDialog;
 
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 AndroidAlertDialog extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        Button buttonStartDialog = (Button)findViewById(R.id.startdialog);
        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();
    }
}

akm
www.cdacians.com

No comments:

Post a Comment