How do I add multi-select checkbox in warning dialog for Android?
I created the Sync menu in the Android app. When we click on the “Sync” alert, a 4 checkbox layout opens. What I want is for them to work, like I automatically unclick other options after 15 minutes of clicking.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_settings:
alertDialog = new AlertDialog.Builder(HomePage.this).create(); Read Update
LayoutInflater adbInflater = this.getLayoutInflater();
View checkboxLayout = adbInflater.inflate(R.layout.sync_layout, null);
defaultchkbox = (CheckBox)checkboxLayout.findViewById(R.id.defaultchkbox);
after15mint = (CheckBox)checkboxLayout.findViewById(R.id.after15mint);
after30mint = (CheckBox)checkboxLayout.findViewById(R.id.after30mint);
after45mint = (CheckBox)checkboxLayout.findViewById(R.id.after45mint);
alertDialog.setView(checkboxLayout);
alertDialog.setTitle("Synchronization");
alertDialog.setMessage("Choose");
alertDialog.setButton(Dialog.BUTTON_POSITIVE,"Save changes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
TODO Auto-generated method stub
boolean checkBoxResult = false;
if(after15mint.isChecked())
{
Toast.makeText(getApplicationContext(), "15 Minute checked", Toast.LENGTH_LONG).show();
checkBoxResult = true;
}
else if(after30mint.isChecked())
{
Toast.makeText(getApplicationContext(), "30 Minute checked", Toast.LENGTH_LONG).show();
checkBoxResult = true;
}
else if(after45mint.isChecked())
{
Toast.makeText(getApplicationContext(), "45 Minute checked", Toast.LENGTH_LONG).show();
checkBoxResult = true;
}
else{
Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_LONG).show();
}
}
});
alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
alertDialog.dismiss();
}
});
alertDialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But I’m a little confused about the work of checkboxes in alerts. Advice would help a lot. Thank you. 🙂
Solution
It looks like you need to use radio buttons nested in RadioGroup. Then it will only allow you to select one option at a time.
For more information about RadioGroup, see:
http://developer.android.com/reference/android/widget/RadioGroup.html
For more information about creating radio buttons, see:
http://developer.android.com/reference/android/widget/RadioButton.html
Especially for your code, you must define RadioButtons in the RadioGroup in R.layout.sync_layout, for example
<RadioGroup
android:id="@+id/syncGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/defualt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "Default"
android:checked="true" />
<RadioButton
android:id="@+id/minute15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "15 Minute" />
<RadioButton
android:id="@+id/minute30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "30 Minute" />
<RadioButton
android:id="@+id/minute45"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "45 Minute" />
</RadioGroup>