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();
}
}
You forget to call
show()method. Addalert.show()and yourAdd()method will look like this: