Академический Документы
Профессиональный Документы
Культура Документы
Lecture 3: User interface components: Part II The List View Working with images ViewGroups Form Layout Form Items Designing a multi-screen application
Showing a ListView
Create a list of values to show:
private String[] items = {First, Second, };
A ListView Displayed
Display on screen
setContentView(list);
Intents
Intent: Description of an action to be performed
A message that you can send to the system to launch another activity
Starting Activity
Create Intent object Add any additional data needed Invoke startActivity() with the Intent object as parameter.
Loading images
Images loaded from files, etc. are immutable Images Only image format that is preferred is PNG We saw sample code to load Bitmap in Lesson 2.
private Bitmap loadImage(String filename) throws IOException{ AssetManager am = this.getAssets(); InputStream img_stream = am.open(filename); return BitmapFactory.decodeStream(img_stream); }
Manipulating images
Creating a mutable image:
Bitmap myPic; Bitmap.Config cfg = Bitmap.Config.ARGB_8888; myPic = Bitmap.createBitmap(width,height,cfg);
Example
ViewGroups
Can be used to create complex user interfaces. Acts as a container for widgets (user interface elements).
Components display data and input/edit data. A ViewGroup may contain other ViewGroups Also called Layouts
Create Item(s)
txtUname = new EditText(); txtPasswd = new EditText();
Add Buttons, and finally display Form. See LoginForm project for code details
Hiearchical Description
LinearLayout (ViewGroup)
Widgets on ViewGroups
Multiple items on Layout, not all necessarily visible at the same time. User can navigate from item to item by scrolling or using the up/down/left/right navigation buttons. Items can be selected/edit similar to List, EditText, etc.
TextView (View)
EditText (View)
EditText (View)
LinearLayout (ViewGroup)
Button (View)
Button (View)
Button (View)
Widgets on Layouts
Can addView() and removeView() items as needed from Layout Can access/set item properties using set/get methods.
TextView
Used to display non-editable text on screen. Typical usage:
String message = "Enter info TextView myLabel = new TextView(this); myLabel.setText(message);
ImageView
Displays an image (perhaps from a Bitmap object) If the Image is mutable, any change made to it is reflected on display. Same usage as in Lecture 2, but can be added to ViewGroup using addView().
EditText
Allows user to input/edit text. Can display multi-line text. Usage:
txtLogin = new EditText(this);
Can alter input method using setInputType() method Use the setHint() method to provide a hint of what is expectednot a label, the hint vanishes when the user enters text username.setHint("Username");
Spinner
Allows the user to select from a list of choices. Similar in intent to ListView, but is a drop down. Was used in FacultyLookup app. Typical usage:
String names[] = {Adam, Bob, Charlie }; Spinner nameChoices = new Spinner(this); ArrayAdapter<CharSequence> adapter; adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, names); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); nameChoices.setAdapter(adapter);
Email addresses:
InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
ProgressBar
Visual indication of progress in some task
How much of a file has been downloaded How many steps in a multi-step activity have been completed
LinearLayout
Items on a LinearLayout are laid out like words on a page: Horizontal from left to right, but one row high. If not all Items can fit in display, user can scroll down/up to see non-visible Items. Can change orientation to vertical
Non-interactive Can be made indeterminate when length of task is unknown: Cyclic animation Default is spinning wheel (indeterminate) To make horizontal:
progress = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
Display pizza
Yes
Heat level
Login Form
Toppings Form
Pepper Form
Next lesson
All about Drawing