MessageBox or AlertDialog in Android Java

What?
A quick article on how to display a OK/Cancel popup which I use as a message to the user for debug purposes.

Why?
It's similar to javascript's message box or confirm box, only Android let's you redesign the dialog. For my purposes I have used an XML as the layout.

How?
This displays a standard Ok/cancel message popup which you attach to some event
copyraw
import android.app.AlertDialog;
import android.content.DialogInterface;

new AlertDialog.Builder(this)
        .setTitle("Some Title")
        .setMessage("some message")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                        // Some stuff to do when ok got clicked
                }
        })
        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                        // Some stuff to do when cancel got clicked
                }
        })
        .show();
  1.  import android.app.AlertDialog; 
  2.  import android.content.DialogInterface; 
  3.   
  4.  new AlertDialog.Builder(this) 
  5.          .setTitle("Some Title") 
  6.          .setMessage("some message") 
  7.          .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
  8.                  public void onClick(DialogInterface arg0, int arg1) { 
  9.                          // Some stuff to do when ok got clicked 
  10.                  } 
  11.          }) 
  12.          .setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
  13.                  public void onClick(DialogInterface arg0, int arg1) { 
  14.                          // Some stuff to do when cancel got clicked 
  15.                  } 
  16.          }) 
  17.          .show()


Additional: Pre-populating default value and returning input
Lots of confusing answers out there on the web but here's an all-in-one where I
  • Open an AlertDialog designed with a custom XML layout
  • Set the hint text of a specific user input field (EditText)
  • Store the input in a TextView (invisible or visible) held in the calling XML
copyraw
/* 201411041253 */
private String hangar_aircraft_name;

public void editHangarAircraftName(View view) {

        // get current value for hint
        TextView currentName = (TextView) findViewById(R.id.hangar_aircraft_001_header);
        hangar_aircraft_name = currentName.getText().toString();

        // get the AlertDialog XML layout file
        final View v = getLayoutInflater().inflate(R.layout.my_custom_dialog, null);

        // set the hint of the EditText in the AlertDialog
        EditText inputText = (EditText) v.findViewById(R.id.hangar_item_menu_name_field);
        inputText.setHint(hangar_aircraft_name);

        // Start building the AlertDialog
        AlertDialog.Builder b = new AlertDialog.Builder(this);

        // set the view of the AlertDialog along with changes (eg. changed "hint")
        b.setView(v);

        // set OK button action
        b.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {

                        // Get the f !!!
                        Dialog f = (Dialog) dialog;

                        // this is the EditText that has the value I want
                        EditText inputTemp = (EditText) f.findViewById(R.id.my_input_field);  

                        // this is the TextView to store the value in (not the AlertDialog but main xml)
                        TextView newName = (TextView) findViewById(R.id.hangar_aircraft_001_name);  

                        // setting header text to returned input
                        newName.setText(inputTemp.getText().toString());  
                }
        });

        // set Cancel button action
        b.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                }
        });

        // create and show the alertDialog
        b.create().show();
}
  1.  /* 201411041253 */ 
  2.  private String hangar_aircraft_name; 
  3.   
  4.  public void editHangarAircraftName(View view) { 
  5.   
  6.          // get current value for hint 
  7.          TextView currentName = (TextView) findViewById(R.id.hangar_aircraft_001_header)
  8.          hangar_aircraft_name = currentName.getText().toString()
  9.   
  10.          // get the AlertDialog XML layout file 
  11.          final View v = getLayoutInflater().inflate(R.layout.my_custom_dialog, null)
  12.   
  13.          // set the hint of the EditText in the AlertDialog 
  14.          EditText inputText = (EditText) v.findViewById(R.id.hangar_item_menu_name_field)
  15.          inputText.setHint(hangar_aircraft_name)
  16.   
  17.          // Start building the AlertDialog 
  18.          AlertDialog.Builder b = new AlertDialog.Builder(this)
  19.   
  20.          // set the view of the AlertDialog along with changes (eg. changed "hint") 
  21.          b.setView(v)
  22.   
  23.          // set OK button action 
  24.          b.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() { 
  25.                  @Override 
  26.                  public void onClick(DialogInterface dialog, int whichButton) { 
  27.   
  28.                          // Get the f !!! 
  29.                          Dialog f = (Dialog) dialog; 
  30.   
  31.                          // this is the EditText that has the value I want 
  32.                          EditText inputTemp = (EditText) f.findViewById(R.id.my_input_field)
  33.   
  34.                          // this is the TextView to store the value in (not the AlertDialog but main xml) 
  35.                          TextView newName = (TextView) findViewById(R.id.hangar_aircraft_001_name)
  36.   
  37.                          // setting header text to returned input 
  38.                          newName.setText(inputTemp.getText().toString())
  39.                  } 
  40.          })
  41.   
  42.          // set Cancel button action 
  43.          b.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() { 
  44.                  @Override 
  45.                  public void onClick(DialogInterface dialog, int which) { 
  46.                          dialog.cancel()
  47.                  } 
  48.          })
  49.   
  50.          // create and show the alertDialog 
  51.          b.create().show()
  52.  } 
The key mistake I was doing was trying to recalculate which view the main activity item was in.
Category: AndroidOS :: Article: 586

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.