Reputation: 39
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
Upvotes: 3
Views: 3189
Reputation: 11
just set the checked item as what you want...
builder.setTitle("Delete Files by?")
.setSingleChoiceItems(deleteFilesBy, WHICH ITEM YOU WANT , new DialogInterface.OnClickListener()
Upvotes: 1
Reputation: 2804
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>
Upvotes: 2
Reputation: 376
This should do it.
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.check(R.id.radioButton1);
Upvotes: 1
Reputation: 2804
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);
}
Upvotes: 0