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
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();
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
/* 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();
}
The key mistake I was doing was trying to recalculate which view the main activity item was in.
Discussion
Comments
Questions, corrections and useful additions are reviewed before appearing here.
No comments yet. Start the discussion.