1. 7.3. Alert Message, List,
Dropdown, and AutoComplete
Oum Saokosal
Master of Engineering in Information Systems, South Korea
855-12-252-752
oum_saokosal@yahoo.com
3. Alert Message
• There are 2 type of alert messages in Android:
1. Toast
2. AlertDialog
Toast AlertDialog
4. Toast
• Toast message is a simple way of showing an
alert which does not take your focus away:
appear and disappear automatically.
Toast.makeText(this,"Your Message",
Toast.LENGTH_LONG).show();
– this: refers to this activity the toast is displayed in.
– "Your Message": is where your write your message.
– Toast.LENGTH_LONG: To display a bit long.
– Toast.LENGTH_SHORT: To display a bit short.
5. AlertDialog
• AlertDialog is like a classic dialog box style, with a
button or two. But it is a bit tricky:
new AlertDialog.Builder(this)
.setTitle("Message Demo")
.setMessage("eek!")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
// do nothing – it will close on its own
}
}).show();
6. Example of AlertDialog
public class Main extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setOnClickListener(this);
}
public void onClick(View v) {
new AlertDialog.Builder(this)
.setTitle("Alert Message")
.setMessage("Here is the message dialog box")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//To-do: nothing to do
}
}).show();
}
}
7.
8. AlertDialog with 2 buttons
new AlertDialog.Builder(this)
.setTitle("Update Status")
.setMessage("Message")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
9.
10. List
• To make a simple list, you can do it directly in Java
code:
public class Main extends ListActivity {
String[] items={"Cambodia", "Laos", "Thailand", "Vietnam"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
items));
}
11. List with onListItemClick
• You can put an event to each item by override onListItemClick:
public class Main extends ListActivity {
String[] items={"Cambodia", "Laos", "Thailand", "Vietnam"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
Toast.makeText(this, items[position], Toast.LENGTH_LONG).show();
}
}