1

I have an item in my menu

case R.id.theme:
   ShowRadioDialog();           
 return true;

With a method that shows an Alert dialog with 3 radio buttons. When i click the item the dialog shows but when i choose some item inside the dialog and i tap in the positiove buttons nothing happens. This is the method:

public void ShowRadioDialog() {
        final CharSequence[] items={"Rosso","Verde","Blu"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Seleziona un colore");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

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

            if (wich== 1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                            getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
                            toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                            Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                            Log.i("Colors", "Rosso Ok");
                        }
                } else if (wich ==2) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                    }
                } else if (wich == 3){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                    }
                }
            }
        });

        builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                    if ("Rosso".equals(items[which])) {
                        which = 1;
                    } else if ("Verde".equals(items[which])) {

                        which = 2;
                    } else if ("Blu".equals(items[which])) {

                        which = 3;
                    }

            }
        });
        builder.show();
    }

I'm not sure it's the correct way. However, not either one (log in the logcat and the Toast in the application) appears. Seems that the positive button not accept the choice. Something is wrong?

Atlas91
  • 5,754
  • 17
  • 69
  • 141

1 Answers1

1

Define a variable on top level and use that to access the selected item.

int index = -1;

public void ShowRadioDialog() {
    final CharSequence[] items={"Rosso","Verde","Blu"};
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setTitle("Seleziona un colore");
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

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

        if (index == 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                        Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                        Log.i("Colors", "Rosso Ok");
                    }
            } else if (index ==2) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                }
            } else if (index == 3){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                }
            }
        }
    });

    builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

                if ("Rosso".equals(items[which])) {
                    index = 1;
                } else if ("Verde".equals(items[which])) {

                    index = 2;
                } else if ("Blu".equals(items[which])) {

                    index = 3;
                }

        }
    });
    builder.show();
}

If you want to save the value of index in shared preference on click of ok button do like this

Store in SharedPreferences

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putInt("choice", index);
editor.commit();

Fetch from SharedPreferences when required

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
int index = preferences.getInt("choice",-1);
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • Yeah! Now it works thanks! is there a way to save in the sharedpreferences the choice? – Atlas91 Dec 24 '14 at 16:40
  • of course you can save it there. This would give you and idea how to do that. http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Rohit5k2 Dec 24 '14 at 16:41
  • mmh i'm not pretty sure how can i do it inside a dialog with the radio buttons for real..are you able to show me please? Thank you so much really! – Atlas91 Dec 24 '14 at 16:43
  • mmh ok so after all if clause in onClick of positive button, i paste the code to store and in my onCreate Activity the code to fetch..but not works :( – Atlas91 Dec 24 '14 at 16:54
  • mmh ok no wait, using a log i saw that it works.. it saves the correct position but not do what it have to do in that option. I don't know if you have understand what i mean – Atlas91 Dec 24 '14 at 17:01
  • Please post a new question for that and explain what you are trying to do. – Rohit5k2 Dec 24 '14 at 17:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67634/discussion-between-rohit5k2-and-end-game). – Rohit5k2 Dec 24 '14 at 17:05
  • ok, but it's simple. Now i post the question and i will write you here the new link to the quest. – Atlas91 Dec 24 '14 at 17:05
  • see here: http://stackoverflow.com/questions/27640219/dialog-with-radio-button-save-sharedpreferences-but-not-the-behaviour – Atlas91 Dec 24 '14 at 17:14