Home » Android » Android set selected item in alert dialog

Android set selected item in alert dialog

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m creating an Alert Dialog in the beginning of my application to let the user to choose where to store the data which my application is downloading from web. The thing that I want to achieve now is depending on the size of Internal / External Storage I want to set selected one of the items. Here is the code which I’m using to create the dialog :

@SuppressWarnings("static-access")
public void createDialog(){


    final CharSequence[] items = {"Phone Memory - "+memorysize+" free space", "SD Card - "+megAvailable+" MB free space"};

    final int userId = rpc.getUserId(this);
    final String servername = rpc.getCurrentServerName(this);

    SharedPreferences stampiiSettings = PreferenceManager.getDefaultSharedPreferences(MyCollectionList.this);
    final SharedPreferences.Editor editor = stampiiSettings.edit();

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());
    builder.setTitle("Select Storage Path");
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if(item == 0){

                rpc.createFoldersInInternalStorage(servername, userId, MyCollectionList.this);
                Toast.makeText(getApplicationContext(), "Selected Storage Path : Phone Memory", Toast.LENGTH_SHORT).show();
                editor.putInt("storagePath", 1);
                editor.commit();
            } else if (item == 1){

                rpc.createFoldersInExternalStorage(servername, userId, MyCollectionList.this);
                Toast.makeText(getApplicationContext(), "Selected Storage Path : SD Card", Toast.LENGTH_SHORT).show();
                editor.putInt("storagePath", 2);
                editor.commit();
            }
        }});

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                mHandlerUpdateUi.post(mUpdateUpdateUi); // update UI            
        }
        });
        AlertDialog alert = builder.show();
}

And another thing that I want to achieve, how can I prevent user to close the Alert Dialog if he didn’t choose any item. I don’t want to close the dialog on pressing back button or when he click OK button. Any ideas/ suggestions/ help are welcomed!

How to&Answers:

Do something like this:

int selected = 0; // or whatever you want
builder.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
              //onclick
    }});

Answer:

for closing button you can define the cancel button with the ok like this way

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // no need to write anything here just implement this interface into this button
        }
});

for selected item you can either make the spinner as local define for this class or you can assign this value to any variable like selected.

int selected = 0; // if you want in integer or 
String selected = "internal"; // you can go with string

now you can set the by default value to this and get the value in when you click on ok button of the dialog box

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
            // here get the selected item value 
    }
});