AndroidUI - User Interface Classes   Design...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: User Interface Classes   Design Principles   Views & Layouts   Event Handling   Menus   Dialogs   Activities usually display a user interface   Android provides many classes for constructing user interfaces   Key building block for UI components   Views occupy a rectangular space on screen   Responsible for drawing themselves and for handling events   Common operations   Set properties: opacity, background, rotation   Set focus: allow view to take focus, request focus   Attach Listeners: components that should be notified when events occur   Set visibility: show or hide view   Many predefined interactive UI components (aka widgets)   Buttons   Text field   Editable text field   Check box   Radio buttons   Toggle Button   Rating Bar   Can handle event with listeners   Listener interfaces defined in View class   OnClickListener.onClick()   View has been clicked   OnLongClickListener.onLongClick()   View has been pressed & held   OnFocusChangeListener.onFocusChange()   View has received or lost focus   OnKeyListener.onKey()   View has received a key press   Can also handle some events in custom View subclasses   onFinishInflate()   View and all children inflated   onLayout()   View must assign a size and position to all its children   onDraw()   View should render its content   onKeyXXX()   A key has been pressed   onWindowVisibilityChanged()   Window containing view has changed its visibility status Date Picker Time Picker Spinner Auto Complete Gallery   Provides a scrollable list of items   User can select one item at a time   Items added to Spinner with a ListAdapter public void onCreate(Bundle savedInstanceState) { … Spinner spinner = (Spinner) findViewById(R.id.spinner); // sets up data & spinner’s normal view ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.colors, android.R.layout.simple_spinner_item); // sets up spinner’s dropdown view adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); … spinner.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { // do something with selection } … }; … } MapView Web View   An invisible View that contains other views   Used for grouping & organizing a set of views   Base class for layouts & view containers   Child views arranged in a single horizontal or vertical row <LinearLayout … android:orientation="vertical”> <LinearLayout …> <TextView … red block /> … <TextView … yellow block /> </LinearLayout …> <LinearLayout …> <TextView … row one/> … <TextView … row four/> </LinearLayout …> </LinearLayout …>   Child views are positioned relative to each other and to parent view <RelativeLayout…> <TextView android:id="@+id/label” android:text="Type here:” … /> <EditText android:layout_below="@id/label” … Add id field/> <Button android:id="@+id/ok” android:layout_below="@id/entry” android:layout_alignParentRight="true” android:text="OK" … /> <Button android:layout_toLeftOf="@id/ok” android:layout_alignTop="@id/ok” android:text="Cancel" … /> </RelativeLayout>   Child views arranged into rows & columns <TableLayout …> <TableRow> <TextView android:layout_column="1" android:text="Open...” android:padding="3dip" /> <TextView android:text="Ctrl ­Shift ­S" android:gravity="right” android:padding="3dip" /> </TableRow> <View android:layout_height="2dip” android:background="#FF909090" /> … <TableRow> <TextView android:text="X” android:padding="3dip" /> <TextView android:text="Import...” android:padding="3dip" /> … </TableRow> … </TableLayout>   Child views arranged in a two ­dimensional, scrollable grid   Child views added to layout via ListAdapter   Allows multiple Activities to share single content area   Layout is divided into tab & content areas   Each tab is associated with one Activity   Exactly one tab is selected at any given time   Activity corresponding to the selected tab is visible in the content area   ViewGroup containing a scrollable list of selectable items   ListView can filter the list of items based on text input   List items inserted using a ListAdapter   Activities support menus   Activities can   Add items to a menu   handle clicks on the menu items   Options   Primary menu shown when user presses the menu button   Context   View ­specific menu to be shown when user touches and holds the view   Submenu   A menu activated when user touches a visible menu item   Define menu resource in XML file   Store in res/menu/filename.xml   Inflate menu resource using MenuInflater in appropriate onCreate…Menu() methods   Handling item selection in appropriate on… ItemsSelected() methods public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.top_menu, menu); return true; } <menu …> <item android:id="@+id/help” android:icon="@drawable/ic_menu_help” android:title="@string/help" /> <item android:id="@+id/more_help" android:icon="@drawable/ic_menu_help" android:title="@string/more_help" /> // continued … <item android:id="@+id/even_more_help” android:icon="@drawable/ic_menu_help" android:title="@string/even_more_help" > <menu> <item android:id="@+id/give_up" android:title="@string/give_up" /> </menu> </item> </menu> public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.help: // do something return true; case R.id.more_help: // do something return true; case R.id.even_more_help: // do something return true; … public void onCreateContextMenu( ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.help_guide: // do something return true; … } } public void onCreate(Bundle savedInstanceState) { … TextView tv = new TextView(this); tv.setText("Hello, Android"); … registerForContextMenu(tv); }   Many other features supported   Grouping menu items   Binding shortcut keys to menu items   Binding Intents to menu items   Independent subwindows used by Activities to communicate with user   Dialog subclasses   AlertDialog   ProgressDialog   DatePickerDialog   TimePickerDialog private final int ALERTTAG = 0, PROGRESSTAG = 1; … shutdownButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(ALERTTAG); } }); protected Dialog onCreateDialog(int id, Bundle args) { … case ALERTTAG: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(”Do you really want to exit?”) .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); showDialog(PROGRESSTAG);} }) //continued .setNegativeButton("No”,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel();} }); protected Dialog onCreateDialog(int id, Bundle args) { switch (id) { … case PROGRESSTAG: shutdownButton.setEnabled(false); final ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("Activity Shutting Down."); … new Thread(new Runnable() { public void run() { try { Thread.sleep(5000);} catch … dialog.dismiss(); AlertDialogExample.this.finish(); } }).start(); return dialog … } }                       FormWidgetRadioGroup FormWidgetsButton FormWidgetsCheckBox FormWidgetsRatingBar FormWidgetsSpinner FormWidgetsToggleButton ListLayoutExample GridLayoutExample TabLayoutExample HelloAndroidWithMenus AlertDialogExample ...
View Full Document

This note was uploaded on 01/13/2012 for the course CMSC 436 taught by Professor Porter during the Fall '11 term at Maryland.

Ask a homework question - tutors are online