Question

I am programming a simple game, and now, i want my Application to show a dialog box, when a Countdown finishes. I tryed that Code (see below), but it doesn't work:

protected void alertbox()
   {
   new AlertDialog.Builder(this)
      .setMessage("You've scored " + score + " points!")
      .setTitle("Congatulations!")
      .setCancelable(false)
      .setNeutralButton(android.R.string.cancel,
         new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int whichButton){}
         ButtonActivity.this.finish();
         })
      .show();
   }
Was it helpful?

Solution

Try this:

new AlertDialog.Builder(context).setMessage("Congratulations! You scored " + score + " points!")
.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        finish();
    }
}).show();

OTHER TIPS

Do like that:

    final Dialog dialog = new Dialog(ButtonActivity.this);
    dialog.setContentView(R.layout.dialog);
    dialog.setTitle("Dialog Title");
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Congratulations! You scored " + score + " points!");
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);

    dialogButton.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
        dialog.dismiss();
        ButtonActivity.this.finish();
       }
    });
    dialog.show();

dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >



    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        />

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/text"
        />

</RelativeLayout>
protected void alertbox(){
    new AlertDialog.Builder(this)
  .setMessage("You've scored " + score + " points!")
  .setTitle("Congatulations!")
  .setCancelable(false)
  .setNeutralButton(android.R.string.cancel,
     new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton){
         Intent intent =new Intent(getApplicationContext(), NextActivityClassName.class);
         intent.putExtra("ID",score);
         startActivity(intent);
         finish();
     }
  }).show();

}

In the next Activity use:

 int scoreValue= getIntent().getIntExtra("ID", defaultValueIntValue);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top