0

I want to create a dialog box when a button is clicked inside the dialog box I need 2 radio buttons which are linked with mobile default music and other is external memory of the mobile. Anyone can help me out this here is my code as follows?

    buttonSound = (Button) findViewById(R.id.btn_sound);
        buttonSound.setOnClickListener (new OnClickListener(){
            public void onClick(View v){
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getApplicationContext());
                Log.d("TAG","button inside mobile");
                alertDialogBuilder
                //.setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Sound",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        AlarmActivity.this.finish();
                    }
                  });
//              builder.setPositiveButton("Sound", );
                alertDialogBuilder
                //.setMessage("Click yes to exit!")
                                                                                                            .setCancelable(false)
                .setPositiveButton("SdCard",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity

                        Log.d("TAG","button inside sdCard");
                        AlarmActivity.this.finish();
                    }
                  });
                 alertDialogBuilder.show();

And it is showing error as follows given below

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Naseer Ahammed
  • 89
  • 1
  • 2
  • 15

3 Answers3

0

Try this..

Change this..

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getApplicationContext());

to

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        AlarmActivity.this);

EDIT

radio_btn.xml

<?xml version="1.0" encoding="UTF-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/status_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/default_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default music" />

    <RadioButton
        android:id="@+id/external_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="External card music" />

</RadioGroup>

JAVa

    LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.radio_btn, (ViewGroup) getCurrentFocus());
    RadioButton default_btn = (RadioButton) dialoglayout.findViewById(R.id.default_btn);
    RadioButton external_btn = (RadioButton) dialoglayout.findViewById(R.id.external_btn);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        AlarmActivity.this);
                Log.d("TAG","button inside mobile");
                alertDialogBuilder
                //.setMessage("Click yes to exit!")
                .setCancelable(false)
                .setView(dialoglayout);
                .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        AlarmActivity.this.finish();
                    }
                  });
                 alertDialogBuilder.show();
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • but it is showing only one option in the dialog box. i need to show 2 option one is default music files of the phone and other is external card music files – Naseer Ahammed Apr 16 '14 at 05:49
0

Use AlarmActivity.this or this intead of getApplicationContext().

Create in layout in res/layout

  1. radio_dialog_layout.xml

    <RadioButton
        android:id="@+id/rd_!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A" />
    
    <RadioButton
        android:id="@+id/rd_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rd_!"
        android:text="B" />
    
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rd_2"
        android:layout_centerInParent="true"
        android:text="OK" />
    </RelativeLayout>
    
  2. create dialog in activity by using this code:

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.radio_dialog_layout);
    dialog.setTitle("This is my custom dialog box");
    dialog.setCancelable(true);
    // there are a lot of settings, for dialog, check them all out!
    // set up radiobutton
    RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_);
    RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2);
    
    // now that the dialog is set up, it's time to show it
    dialog.show();
    

You can also refer this tutorials: How to add lists, Checkboxes and Radio buttons to an AlertDialog – Android

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
0

Create one xml file,

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

<RadioButton
    android:id="@+id/radiobutton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radiobutton1"
    android:text="Bye" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radiobutton2"
    android:layout_centerInParent="true"
    android:text="Click" />

and create Dialog in activity,

Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.activity_main);
    dialog.setTitle("My Dialog Box");
    dialog.setCancelable(true);

    // set up radiobutton inside dialog box
    RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.radiobutton1);
    RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.radiobutton2);

    // Show the dialog
    dialog.show();
Meena
  • 1
  • 1