0

How can I create an alertDialog which consists of a list of items defined in the code. I want the alert similar to this:

enter image description here

I have seen this but still I'm not able to create the alert.

1 Answers1

3

Try this code. i think this will definitely helps you

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Pick Colour");
            dialog.setItems(R.array.colors,new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int position) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),"selected Item:"+position, Toast.LENGTH_SHORT).show();
                }

            });
            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
            AlertDialog alert = dialog.create();
            alert.show();

        }
    }

let me know if you have any doubts.

TheFlash
  • 5,997
  • 4
  • 41
  • 46
Veerababu Medisetti
  • 2,745
  • 2
  • 14
  • 11