Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't know why "Dialog Box" is not appearing? Even "Toast" Statement is not running

I Have just started learning Android, this is the code of my java file. I have not changed the XML file. On running this code, simple blank activity is shown, with no DialogBox. Help needed.

package com.example.dialogbox;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Add("Exit App", "Are u sure?");
    Toast.makeText(this, "open", Toast.LENGTH_LONG);
}

public void Add(String title, String msg) {
    Toast.makeText(this, "open", Toast.LENGTH_LONG);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(title)
            .setMessage(msg)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                        }
                    })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
    AlertDialog alert = builder.create();

}

}  
like image 382
Aexyn Avatar asked Jan 28 '26 18:01

Aexyn


1 Answers

You forget to call show() method. Add alert.show() and your Add() method will look like this:

public void Add(String title, String msg) {

    System.out.println("heyo");
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(title)
            .setMessage(msg)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                        }
                    })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
    AlertDialog alert = builder.create();
    alert.show()

}
like image 197
Hein Avatar answered Jan 30 '26 07:01

Hein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!