chicharp
chicharp

Reputation: 103

Android Alert Dialogs with sounds

I'm working with my application, this is my code please help me to add sounds on my AlertDialog. For example if I choose "Yes" in my alert dialog the notification "You got it" is on sounds function.

package com.example.radiobbutton;



import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, final int checkedId)
            {
                AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
                        MainActivity.this);

                alertDialog2.setTitle("Confirm Answer...");

                // Setting Dialog Message
                alertDialog2.setMessage("Is that your final Answer?");

                alertDialog2.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {


                switch(checkedId)
                {
                case R.id.radio0:
                    Toast.makeText(getApplicationContext(),
                            "You got it", Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.radio1:
                    Toast.makeText(getApplicationContext(),
                            "Wrong", Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.radio2:
                    Toast.makeText(getApplicationContext(),
                            "Draw", Toast.LENGTH_SHORT)
                            .show();
                    break;

                }
                }
            }
        );

                alertDialog2.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Write your code here to execute after dialog
                                Toast.makeText(getApplicationContext(),
                                        "Choose again", Toast.LENGTH_SHORT)
                                        .show();
                                dialog.cancel();
                            }
                        });

                  alertDialog2.show();

    }

        });

}

}

Upvotes: 2

Views: 3807

Answers (1)

Nambi
Nambi

Reputation: 12042

put the sound clip inside the android raw folder.

put the MediaPlayer object inside the alert postive onclick function android

public void onClick(DialogInterface dialog, int which) {
 final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.clip);
                 mp.start();
                switch(checkedId)
                {
                case R.id.radio0:
                    Toast.makeText(getApplicationContext(),
                            "You got it", Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.radio1:
                    Toast.makeText(getApplicationContext(),
                            "Wrong", Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.radio2:
                    Toast.makeText(getApplicationContext(),
                            "Draw", Toast.LENGTH_SHORT)
                            .show();
                    break;

                }
                }
            }
        );

Upvotes: 2

Related Questions