I am trying to view a dialog box with radio buttons but when I clicked on the button alert dialog box never appears. Below is the code. Please let me know if there is any alternate solution to this. This sample was taken from API Demos.
public class MainPage extends Activity{
Button start;
private static final int DIALOG_SINGLE_CHOICE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
start = (Button) findViewById(R.id.start);
start.setBackgroundResource(R.drawable.read);
start.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
showDialog(DIALOG_SINGLE_CHOICE );
}
});
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
id = DIALOG_SINGLE_CHOICE;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose your option");
builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
return super.onCreateDialog(id);
}
}
}
You're doing it wrong inside
onCreateDialog()
.First, you shouldn't do
id = DIALOG_SINGLE_COICE
but testid
value if it equals toDIALOG_SINGLE_CHOICE
. * If yes, then create theDialog
withAlertDialog.Builder
and returnbuilder.create()
. * If no, returnsuper.onCreateDialog(id)
.But this method is deprecated, you should use
DialogFragment
instead.You need to replace
return super.onCreateDialog(id);
withreturn builder.show();
..create()
will create an alert dialog from the builder but it does not display that dialog..show()
will create the dialog from the builder and show it on the screen.I also think that the
int id
might be unnecessary. You could try the following code and it should work for you:Create this function and use showAskDialog() function in your click event