Upload
oum-saokosal
View
280
Download
0
Embed Size (px)
Citation preview
7.3. Alert Message, List, Dropdown, and AutoComplete
Oum SaokosalMaster of Engineering in Information Systems, South Korea
Agenda
• Alert Message• List • Dropdown• AutoComplete
Alert Message
• There are 2 type of alert messages in Android:1. Toast2. AlertDialog
Toast AlertDialog
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.
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();
Example of AlertDialogpublic 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();}
}
AlertDialog with 2 buttonsnew 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();
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"};@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);//setContentView(R.layout.main);setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items));
}
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"};@Overridepublic 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();}
}
Dropdown
• In Android, a dropdown selector is called Spinner.
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Select your country" />
<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
public class Main extends Activity implements AdapterView.OnItemSelectedListener { String[] items = {"Cambodia", "Laos", "Thailand", "Vietnam"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spin = (Spinner) findViewById(R.id.spinner1); spin.setOnItemSelectedListener(this); ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
items); aa.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa); }
@Overridepublic void onItemSelected(AdapterView<?> parent, View v, int position,long id) {Toast.makeText(this, items[position], Toast.LENGTH_LONG).show();}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {
}}
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<TextViewandroid:id="@+id/selection"android:layout_width="fill_parent"android:layout_height="wrap_content"/>
<AutoCompleteTextView android:id="@+id/edit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:completionThreshold="3"/>
</LinearLayout>
AutoComplete
• Auto Complete text in Android is named AutoCompleteTextView.
public class AutoComplete extends Activity implements TextWatcher {TextView selection;AutoCompleteTextView edit;String[] items={"Cambodia", "Cambodian", "Khmer", "Phnom Penh",
"Siem Reap", "Preah Vihear", "Angkor Wat", "Angkor Thom"};public void onCreate(Bundle icicle) {
super.onCreate(icicle);setContentView(R.layout.main);selection=(TextView)findViewById(R.id.selection);edit=(AutoCompleteTextView)findViewById(R.id.edit);edit.addTextChangedListener(this);edit.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, items));
}public void onTextChanged(CharSequence s, int start, int before, int count) {
selection.setText(edit.getText());}public void afterTextChanged(Editable s) {}public void beforeTextChanged(CharSequence s, int start, int count,
int after) {}
}
Go on to the next slide