I've been trying to get a handle on Dialogs. I read the android dev site info and a dozen bolg's. It seems there are several ways of doing it and I'm able to at least get a dialog with 3 buttons (not using a custom dialog with custom layout).
If I set up the positive/negative/neutral actions with something like finish(), cancel() etc, it works with those calls.
But, what I want to do is have the buttons do something more, if only display a text using a string defined in the Maincode (not a Toast). Eventually, I want to enter some numbers in a dialog and return them in a string. Yes, I can do that from another activity screen but, prefer not to as I like the compact size of a dialog.
Even cheating and returning an integer to the Maincode to do some switch/case stuff would be okay but, I seem not able to even return an integer.
I understand that I'll need to do a customized alert dialog to do input stuff and the following is my attempt at a start by just trying to return a string or integer - it doesn't seem to be getting me there!
This code presents the dialog with 3 buttons. This is just one of the try's I made (integer return stuff deleted)...
What can I do to return an integer to the Maincode from a dialog button? There are no code errors or warnings, it just doesn't work as I hoped...
public class Maincode extends Activity {
public static String rtndMessage = "Push Button";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView text = (TextView) findViewById(
R.id.TextView01);
final Button doIt = (Button) findViewById(R.id.Button01);
// -----------------------------------------------------
doIt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do something
Dialog1(); // call Dialog1
}
}); // end -----------------------------------------------
// do the rest of the code (ie, display the result of doIt)
text.setText(rtndMessage); // set text w/result
}//end onCreate ----------------------------------------------
public void Dialog1() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dialog Test");
builder.setIcon(R.drawable.redtest);
// chain the following together
builder.setMessage("Send text: Yes, No, Cancel")
// the positive action ----------------------------------
.setPositiveButton("Yes Action", new
DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Maincode.rtndMessage = "sent Yes back";
}
})
// The negative action ----------------------------------
.setNegativeButton("No Action", new
DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Maincode.rtndMessage = "sent No back";
}
})
// The negative action ------------------------------
.setNeutralButton("Cancel", new
DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Maincode.rtndMessage = "sent N/A back";
dialog.cancel(); // just return to activity
}
});
builder.show(); // show the dialog }//end Dialog }//end activity
Android 1.6+ provides an easy to use set of dialog functions for your Activity.
When you make the dialogs, just do whatever you have to do in the onClick() methods of the dialog's buttons. You can see an example I have of this below, in the DIALOG_CONFIRM_DELETE switch where I delete a record for a local database and requery a cursor.
I'd take a look at the "Creating Dialogs" section in the dev guide on http://d.android.com It shows you how to use the dialog functions. http://developer.android.com/guide/topics/ui/dialogs.html
This is VERY late, but hopefully it will help others in future. What I did was created a custom dialog that implemented OnClickListener:
Then in my main code I implemented an onCancelListener (dialogs have onCancel and onDismiss listeners all prepared for you).
Hope this helps!
You can implement the dialog as separate Activity and get any behavior you need. Just apply standard android Theme.Dialog theme to the activity to make it look like dialog. Also you can create theme of your own pointing Theme.Dialog as parent.