07.3. android alert message, list, dropdown, and auto complete

21
7.3. Alert Message, List, Dropdown, and AutoComplete Oum Saokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 [email protected]

Upload: oum-saokosal

Post on 27-Jan-2017

284 views

Category:

Education


0 download

TRANSCRIPT

Page 1: 07.3. Android Alert message, List, Dropdown, and Auto Complete

7.3. Alert Message, List, Dropdown, and AutoComplete

Oum SaokosalMaster of Engineering in Information Systems, South Korea

[email protected]

Page 2: 07.3. Android Alert message, List, Dropdown, and Auto Complete

Agenda

• Alert Message• List • Dropdown• AutoComplete

Page 3: 07.3. Android Alert message, List, Dropdown, and Auto Complete

Alert Message

• There are 2 type of alert messages in Android:1. Toast2. AlertDialog

Toast AlertDialog

Page 4: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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.

Page 5: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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();

Page 6: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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();}

}

Page 7: 07.3. Android Alert message, List, Dropdown, and Auto Complete
Page 8: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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();

Page 9: 07.3. Android Alert message, List, Dropdown, and Auto Complete
Page 10: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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));

}

Page 11: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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();}

}

Page 12: 07.3. Android Alert message, List, Dropdown, and Auto Complete
Page 13: 07.3. Android Alert message, List, Dropdown, and Auto Complete

Dropdown

• In Android, a dropdown selector is called Spinner.

Page 14: 07.3. Android Alert message, List, Dropdown, and Auto Complete

<?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>

Page 15: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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) {

}}

Page 16: 07.3. Android Alert message, List, Dropdown, and Auto Complete
Page 17: 07.3. Android Alert message, List, Dropdown, and Auto Complete

<?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>

Page 18: 07.3. Android Alert message, List, Dropdown, and Auto Complete

AutoComplete

• Auto Complete text in Android is named AutoCompleteTextView.

Page 19: 07.3. Android Alert message, List, Dropdown, and Auto Complete

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) {}

}

Page 20: 07.3. Android Alert message, List, Dropdown, and Auto Complete
Page 21: 07.3. Android Alert message, List, Dropdown, and Auto Complete

Go on to the next slide