user3176937
user3176937

Reputation:

Associate Dialog's Item on click

I create a Dialog after the App'sUser click on the Button, so my problem is: I Have my mini Dialog-menu with two elements "Lux" and "Vigor". I want to associate different function for Lux and Vigor, It's possible? How I have to do?

My big problem is identify the event "Click on Lux or Vigor" Thanks for your time, Kings regards I post my code:

 private final static int BUTTON_DIOALOG = 2;
 final CharSequence[] items ={"Lux", "Vigor"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton immG = (ImageButton)this.findViewById(R.id.imageButton1);
    immG.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
 MainActivity.this.showDialog(BUTTON_DIOALOG);              
        }
    });


}
@Override
protected Dialog onCreateDialog(final int id) {
    Dialog dialog = null;
    AlertDialog.Builder builder =new AlertDialog.Builder(this);
switch (id){    
case  BUTTON_DIOALOG:
 builder.setTitle("scegli cosa").setItems(items, new
 DialogInterface.OnClickListener()
 {                             

@Override
public void onClick(DialogInterface dialog, int which) {


        }
    });
}
return dialog;
}}

Upvotes: 0

Views: 138

Answers (2)

apaaj
apaaj

Reputation: 11

You can do like this:

final Context ctx = this;
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setIcon(icon);
dialog.setTitle("");
dialog.setMessage("");
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "LUX", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int buttonId) {

    }
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Vigor", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int buttonId) {
    }
});
dialog.show();

Upvotes: 1

ElDuderino
ElDuderino

Reputation: 3263

The parameter "which" in the callback tells you which button you clicked...

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final CharSequence[] items ={"Lux", "Vigor"};
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_LONG).show();              
        }
    });
    builder.create().show();

Upvotes: 0

Related Questions