3

can someone help me with this? I have an AlertDialog box and its working fine, but what I want is when the alertdialog pops up, it will automatically check one of the radio button.

For an example "10 minutes" is checked when the alertdialog box pops up.

How to do that?

Below is my working code.

final CharSequence[] deleteFilesBy = {"5 minutes","10 minutes", "15 minutes"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Delete Files by?")
                    .setSingleChoiceItems(deleteFilesBy,-1, new DialogInterface.OnClickListener()
                        {   
                            @Override
                            public void onClick(DialogInterface dialog, int which) 
                            {
                                if(deleteFilesBy[which]=="5 minutes")
                                {
                                    // do something
                                }
                                else if (deleteFilesBy[which]=="10 minutes")
                                {
                                    // do something
                                }
                                else if (deleteFilesBy[which]=="15 minutes")
                                {
                                    // do something
                                }
                                dialog.dismiss();
                            }
                        }).setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                // TODO Auto-generated method stub
                                arg0.dismiss();
                            }
                        });

                 AlertDialog alert = builder.create();
                 alert.show();

PLEASE HELP!

Thanks

RJUY

user1306165
  • 39
  • 2
  • 9
  • possible duplicate of [How to set a radio button in Android](http://stackoverflow.com/questions/4134582/how-to-set-a-radio-button-in-android) – Peter Ajtai Apr 02 '12 at 00:22

4 Answers4

2

TestRadio.java:

package com.dave.kelley;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class TestRadioActivity extends Activity {

    int timeSetting = 0;
    RadioButton radio0;
    RadioButton radio1;
    RadioButton radio2;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(listener);
        radio0 = (RadioButton) findViewById(R.id.radio0);
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio2 = (RadioButton) findViewById(R.id.radio2);
    }

    public OnClickListener listener = new OnClickListener() {
        public void onClick(View v) {
            if (timeSetting == 0) {
                radio0.setChecked(true);
                radio1.setChecked(false);radio2.setChecked(false);
            }
            if (timeSetting == 1) {
                radio1.setChecked(true);
                radio0.setChecked(false);radio2.setChecked(false);
            }
            if (timeSetting == 2) {
                radio2.setChecked(true);
                radio0.setChecked(false);radio1.setChecked(false);
            }
            timeSetting++;
            if(timeSetting == 3 || timeSetting > 3) {
                timeSetting = 0;
            }
        }
    };
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
Davek804
  • 2,804
  • 4
  • 26
  • 55
  • So this mean that I am going to call this class if I want to show the AlertDialog for example clicking on a button "show dialog box"? like Intent i = new Intent(TestRadioActivity.class, this); startActivity(i); – user1306165 Apr 02 '12 at 02:36
  • No, this is just to illustrate the kind of thing I'm suggesting. Wherever you have the code that creates and then shows the Alert Dialog, you should have: LayoutInflater inflater = getLayoutInflater(); View v = inflater.inflate(R.layout.MAKEANXMLFILETHATLOOKSHOWYOUWANTYOURDIALOGTOLOOK); alert.setLayout(v); alert.show(); – Davek804 Apr 02 '12 at 03:22
  • No problem. If it works, vote up/approve an answer so that others who come upon this question can find the solution! – Davek804 Apr 02 '12 at 04:13
  • sure! but I can't vote up as of now for I still have 3 points. I'll do it once I have 15 reputation points already. thanks again. – user1306165 Apr 02 '12 at 04:29
1

just set the checked item as what you want...

builder.setTitle("Delete Files by?")
                .setSingleChoiceItems(deleteFilesBy, WHICH ITEM YOU WANT , new DialogInterface.OnClickListener()
1

This should do it.

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.check(R.id.radioButton1);
Janhouse
  • 376
  • 1
  • 11
  • I think this one will now work, because I have no xml file for the AlertDialog. AlertDialog was created just using the code above. I am not sure though. thanks – user1306165 Apr 02 '12 at 01:33
0

You can do this in XML:

                <RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/sA" />

                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/A" />

                <RadioButton
                    android:id="@+id/radio3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/D" />

                <RadioButton
                    android:id="@+id/radio4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sD" />
            </RadioGroup>

The first radiobutton, radio1, has the attribute android:checked="true". That means that when it loads, that radio will be defaulted, the user can still change it of course.

EDIT:

A more programmatic approach:

    int timeSetting = 1;//say 0 = 5; 1=10; 2=15 (set this elsewhere in your code based
    //on how you have predetermined the time length
    RadioButton radio0 = (RadioButton) findViewById(R.id.radio0);
    RadioButton radio1 = (RadioButton) findViewById(R.id.radio1);
    RadioButton radio2 = (RadioButton) findViewById(R.id.radio2);

    if (timeSetting == 0) {
        radio0.setChecked(true);
        radio1.setChecked(false);radio2.setChecked(false);
    }
    if (timeSetting == 1) {
        radio1.setChecked(true);
        radio0.setChecked(false);radio2.setChecked(false);
    }
    if (timeSetting == 2) {
        radio2.setChecked(true);
        radio0.setChecked(false);radio1.setChecked(false);
    }
Davek804
  • 2,804
  • 4
  • 26
  • 55
  • but what I want is to make it checked dynamically. Bec in the comment "// do something" I have a code there that will save the values on the database then every time the alertdailog pops up it will query to the database and base on the data that is being saved in the database it will dynamically checked the radio button. Though I already have the code in saving the data and retrieving the data in the database. The only problem that I am facing right now is how to dynamically checked the radio button. Thanks for your reply... – user1306165 Apr 02 '12 at 00:38
  • Thanks on this, so that means I still need to create an XML file? like on your post above? How will I initial the AlertDialog then? That means I need to change my implementation? thanks – user1306165 Apr 02 '12 at 01:26
  • Well yes, the XML file makes the items for you, and you could change the layout etc. But you still initialize them in code, as I showed above. but you could use what Janhouse said too - it would probably be easier than the if statements I showed. – Davek804 Apr 02 '12 at 01:35
  • Wherever you are making this dialog popup (from a listener, for example) you can do this: LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.XMLFILEWITHRADIOGROUP); alert.setView(layout); You may need to add in another parameter to make the inflater.inflate work, though. Take a look here: http://stackoverflow.com/questions/9337344/dynamically-set-custom-alertdialog-content – Davek804 Apr 02 '12 at 01:51
  • how is this possible to implement now using AlertDialog box? I really have no idea... – user1306165 Apr 02 '12 at 01:52
  • Thanks a lot I get now what you mean.. I'll implement it once I arrive home. thanks again. – user1306165 Apr 02 '12 at 01:56
  • No problem, I am going to add a second post now to show you a full working program that represents the idea behind what I've suggested. – Davek804 Apr 02 '12 at 02:04