570 likes | 722 Views
cosc 5/4730. GUI: Views , Widgets, and Layouts. UI Design. We can spend an entire semester on just UI design. But I’m not a HCI person. Google has become far more involved in UI design (finally) and release some design specs. http:// developer.android.com/design/index.html
E N D
cosc 5/4730 GUI: Views, Widgets, and Layouts
UI Design • We can spend an entire semester on just UI design. But I’m not a HCI person. • Google has become far more involved in UI design (finally) and release some design specs. • http://developer.android.com/design/index.html • Another great place for android gui design • http://www.behance.net/gallery/Google-Visual-Assets-Guidelines-Part-1/9028077 • http://www.behance.net/gallery/Google-Visual-Assets-Guidelines-Part-2/9084309
DroidDraw • User Interface (UI) designer/editor for programming the Android Cell Phone Platform • DroidDraw standalone executable available: (Mac OS X, Windows, Linux) • http://www.droiddraw.org/ • Remember, this a UI designer. It creates the xml file, then add them to the project.
Activity Lifecycle • An activity has essentially four states: • If an activity in the foreground of the screen (at the top of the stack), it is active or running. • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations. • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
Activities • Android OS keeps track of all the Activity objects running by placing them on a Activity Stack. • When a new Activity starts, it is placed on top of the stack. • The previous Activity is now in background. • If only partially obscured (say dialog box), only onpause() is called • onResume() called when the user can interact with it again • if invisible to the user, then onpause() call, followed by onStop() • OnRestart() called, then onStart() when it becomes visible to the user • Or onDestroy() if the Activity is destroyed
GUI and layout • To display anything on a android screen, it needs a layout. • This is a basic axiom of a android. • An Exception can be a single widget displayed • All of this created in their xml (using eclipse or another xml editor for android). • Note this can be done completely in java, but it is very tedious.
Example Layout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical“ android:gravity="center“ android:layout_height="match_parent“ android:layout_width="match_parent"> <TextView android:layout_height="wrap_content“ android:layout_width="wrap_content“ android:padding="24dp“ android:id="@+id/question_text_view"/> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content“ android:layout_width="wrap_content"> <Button android:layout_height="wrap_content“ android:layout_width="wrap_content“ android:id="@+id/true_button“ android:text="@string/true_button"/> <Button android:layout_height="wrap_content“ android:layout_width="wrap_content" android:id="@+id/false_button“ android:text="@string/false_button"/> </LinearLayout> </LinearLayout>
Activity and xml • The activity “inflates” the xml (the screen layout) and then display objects to the screen. • Generally referred to as views. • The activity is in java and setups all the rest of the pieces of the screen objects (views) • Event listeners. • It can also change/modify the screen objects.
Hello world example HelloWorld.java main.xml <?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World!" /> </LinearLayout> package edu.cs4755.HelloWorld; import android.app.Activity; import android.os.Bundle; public class HelloWorld extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } App displays:
xml • Basic xml layout (one button displayed) <? xml version="1.0" encoding="utf-8"?> <Button android:text="" android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="fill_parent"> </Button> • fill_parent uses all available space, wrap_content uses only as much space as is needed. • id is what we need to find it with the java later. • text would be the default text, in this case, nothing.
xml picture example <?xml version="1.0" encoding="utf-8"?> <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/phone"> </ImageView> • src access the drawable directory for a static picture, named phone. • Note this will only use as much of the screen as needed, because of wrap_content.
xml and views. • Most views can be almost completely configured via xml instead of using java. • This means the java can in many ways, be used only for the events, instead of dealing with display issues. • Example: EditText • android:autoTextboolean, use automatic spelling • android:capitalizeboolean, Cap the first letter of the text entered • android:digits, accept only digits • android:singleLine, allow multi or single line input.
Layout "managers" • Most of the time you need more then one view. So you use a layout to control how the widgets are shown on the screen. • LinearLayout is very simple • <LinearLayoutandroid:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> • Orientation controls placement of the next widget: • vertical: next line • horizontal: to the right of the previous widget • Normally you use several layouts to control how everything is displayed.
multiple Layout example • For presentation reasons lots of info was left off. <?xml version="1.0" encoding="utf-8"?> <LinearLayoutandroid:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayoutandroid:id="@+id/LinearLayout02"android:orientation="horizontal"> <TextView ></TextView> <Button ></Button> </LinearLayout> <LinearLayoutandroid:id="@+id/LinearLayout02" android:orientation="horizontal"> <TextView ></TextView> <ImageView></ImageView> </LinearLayout> </LinearLayout>
Layout Settings • Weight • Inside a layout, then each Widget can use Layout_Weight • This determines, which gets more space • Setting them all to 1, means they share the space equally. • Setting widget1 to 1 and widget2 to 2, means widget2 gets twice as much space as widget1
Layout Settings Weight • Visual example:
Layout Settings (2) • Gravity • A nice way of saying alignment, which is flush on the left side of the screen • Layout_gravity: (Vertical) • left (default) flush on the left side of the screen • center_horizontal center • right is flush on the right side of the screen. • Horizontal layout • Center_vertical Center vertical, instead of on the "baseline" (bottom).
Layout Settings (3) • Padding • how much in pixels space between the widget and the side of the screen/next widget • android:padding="15dp" is about 15 “pixels” all around • Also paddingLeft, paddingRight, paddingTop, and paddingBottom
Relative Layout • More complex and widgets placed based on the previously placed widgets • Except the first one (no other widget yet) • layout_above, layout_below, layout_toLeftOf, layout_toRightOf • With the above, these can be used layout_alignTop, layout_alignBottom, layout_alignLeft, layout_alignRight, layout_alignBaseline • Or placed relative to the container itself • layout_alignParentTop, layout_ParentBottom, layout_alignParentLeft, layout_parentRight, layout_centerHorizontal, layout_centerVertical, layout_CenterInParent • Settings are placed in the Widgets
Relative Example <RelativeLayoutandroid:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5px"> <TextViewandroid:text="Some Text " android:id="@+id/TextView01" android:layout_alignParentBottom="true" > </TextView> <Button android:text="alert" android:id="@+id/Button01" android:layout_above="@id/TextView01" > </Button> </RelativeLayout> • NOTE : Button uses @id/TextView01, no + sign. + sign only needed for the id, when referencing it somewhere, just @id/name
Relative Example (2) <TextView android:id="@+id/headingValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/headingLabel" android:layout_alignBottom="@+id/headingLabel" android:layout_alignLeft="@+id/zAxisValue" android:text="Nothing..." />
Relative Example (3) • Views over the top of each other. This can easy be done with the relative layout. Like the previous example: <ImageView android:id="@+id/ImgV" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/xAxisLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true“ this aligns to the top of the “screen”, android:layout_alignParentTop="true“ since no id was chosen. android:layout_marginLeft="18dp" android:layout_marginTop="15dp" android:text="xAxis" />
Relative and Linear Layouts. • The problem with a relative layout maybe the screen size. The previous example can be redone with a Relative layout and then linear layouts to create the “table” of values. It can then resize as needed. • But it’s also possible you don’t want behavior.
Other Layouts • TableLayout works like html tables (with all the complications like spanning rows) • TableRow is used with it for row layouts • Scrollview work is just like linearLayout, except you get a vertical scrollbars as needed or HorizontalScrollView. It takes one widget, which is likely a LinearLayout. • There is also a GridView, which items are inserted to the layout via a ListAdapter • We come back to the listAdapter later on (with ListViews)
Screen Size and layouts. • In the android directories, there is a res/ • drawable/ • This deals with the screen density of pixels. • The configuration qualifiers you can use for density-specific resources are ldpi (low), mdpi (medium which is the baseline), hdpi (high), and xhdpi (extra high). For example, bitmaps for high-density screens should go in drawable-hdpi/.
Screen Size and layouts. • Scaling: medium is the baseline • Small = mdpi*.75, high=mdpi*1.5 and xhigh=mdpi*2.0 • Pixels: Small=36, medium=48, high=72, and xhigh=96 • For density there are to more • nodpi • Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density. • tvdpi • Which is for TVs and google’s own doc’s say not to use it and use xhdpi instead. • Except the new Nexus 7” tablet is a tvdpi device. • 1.33*mdpi or 100px image should be 133px
Screen Size and layouts. (2) • layout/ • This is deals with the screen size. • Layout/ is the defaultand the only one eclipse creates (or understands directly) • We can have small (~ 426dp x 320dp), normal (470dp x 320 dp) which is the baseline, large (640dp x 480dp), and xlarge (960dp x 720dp) • We can also add land (landscape) and port (portrait) orientation. res/layout/my_layout.xml // layout for normal screen size ("default") res/layout-small/my_layout.xml // layout for small screen size res/layout-large/my_layout.xml // layout for large screen size res/layout-xlarge/my_layout.xml // layout for extra large screen size res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
Screen Size and layouts. (3) • In v3.2 (api 13), the size groups are deprecated for a new method. • This is the problem: 7” tablet is actually in the 5” phone group, which is the large group. • Provides a smallestWidth (independent of orientation) and Width (which is also takes into account orientation) • layout-sw<N>dp and layout-w<N>dp • Where N is the denisty of pixels.
Screen Size and layouts. (4) • Typical configuration: • 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc). • 480dp: a tweener tablet like the Streak (480x800 mdpi). • 600dp: a 7” tablet (600x1024 mdpi). • 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc). • Smallest width (no orientation) res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger) res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger) • Using just width and taking orientation into account res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-w600dp/main_activity.xml # Multi-pane (any screen with 600dp available width or more) • More information: http://developer.android.com/guide/practices/screens_support.html • There are two examples ScreenTest1.zip and ScreenTest2.zip to play with.
Widgets • android.widget • The widget package contains (mostly visual) UI elements to use on your Application screen. • Includes the layouts as well. • To create your own widgets, extend or subclass View. package: android.View • Examples: • TextView, EditText, ProgressBar and SeekBar, Button, RaidoButton, CheckButton, ImageView, and Spinner • This will cover only the basics, since every widget has many attributes. • Listeners will be listed.
TextView • Displays text to the user and optionally allows them to edit it. • A TextView is a complete text editor, however the basic class is configured to not allow editing • see EditText for a subclass that configures the text view for editing. 2 TextViews
Interacting with the widgets HelloWorld.java main.xml <?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World!“ android:id="@+id/textview" /> </LinearLayout> package edu.cs4755.HelloWorld; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloWorld extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextViewmyView; myView= (TextView) findViewById( R.id.textview); myView.setText("Hello World 2!"); } } create a variable and use the id to find it
EditText • EditText inherits from TextView • This is a method to get input. Has several subclasses, autocompeteTextView and ExtractEditText • For a listener, implement the TextWatcher • and use EditText.addTextChangedLister( TextWatcher) • Override three methods • void afterTextChanged(Editable s) • This method is called to notify you that, somewhere within s, the text has been changed. • void beforeTextChanged(CharSequence s, int start, int count, int after) • This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after. • void onTextChanged(CharSequence s, int start, int before, int count) • This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before.
Toast • A toast is a view containing a quick little message for the user • When the view is shown to the user, appears as a floating view over the application. It will never receive • Also very handy for debugging. • Example: • Toast.makeText(getApplicationContext(), “Hi!", Toast.LENGTH_SHORT).show(); • Toast.LENGTH_SHORT or Toast.LENGTH_LONG is how long the message will show on the screen.
Button • inherits from TextView • represents a push-button widget • implement Button.OnClickListener to listener for the push/click. • button.setOnClickListener(View.OnClickListener) • Override • void onClick(View v) • Called when a view has been clicked.
RadioButton • Inherits from CompoundButton (which inherits from Button) • RadioButtons are normally used together in a RadioGroup • RadioGroup is used to create multiple-exclusion scope for a set of radio buttons • Also a Layout, which uses the same attributes as LinearLayout
RadioGroup • Listener for RadioGroup • implement RadioGroup.OnCheckedChangeListener • radioGroup.setOnCheckedChangeListener( ) • Override • void onCheckedChanged(RadioGroup group, intcheckedId) • Called when the checked radio button has changed. • CheckedId is RadioButton that was checked. • clearCheck() clear the selection • check(int id) sets a selection for the id • example id like R.id.RadioButton02
CheckBox • A checkbox is a specific type of two-states button • can be either checked or unchecked • if (checkBox.isChecked()) {checkBox.setChecked(false); } • Listener implement CompoundButton.OnCheckedChangeListener • CheckBox.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) • Override • public void onCheckedChanged(CompoundButtonbuttonView, booleanisChecked)
ToggleButton • Like a checkbox or radio button • Displays checked/unchecked states as a button with a "light" indicator and by default accompanied with the text "ON" or "OFF". • Two states: On and Off • On shows a green button (default behavior) • Off shows a grayed out button (default behavior)
ImageView • To set a static image, this can be setup in xml using • android:src="@drawable/phone" • Where phone is the png image in the drawable directory of the project. • A bitmap can be setup using SetImageBitmap(Bitmap) method • DrawablegetDrawable() method gives you access to the image, where you can use the draw(Canvas ) method to change/draw on the image.
Graphics: Bitmap and Canvas • android.graphics package • A simple way to some animation or “drawing on the screen” is to use a “blank” ImageView ImageVieweImage = (ImageView) findViewById(R.id.eImage); Bitmap eBitmap= eBitmap = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888); Canvas eCanvas= new Canvas(eBitmap); //eCanvas has the graphics draw tools to write on the bitmap eCanvas.drawColor(Color.WHITE); //white screen Paint black = new Paint(Color.BLACK); //black paintbrush eCanvas.drawRect(0,0,x, y, black); eImage.setImageBitmap(eBitmap); eImage.invalidate(); //redraw the ImageView with new picture.
ImageButton • inherits from ImageView • In xml can set the images <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <item <!-- default --> android:drawable="@drawable/button_normal" /> • Uses the listener like the button. View.onClickListener
Progress Bar • A ProgressBar is like a gauge. • The bar can look in one of 4 ways, set in the style tag • android:progressBarStyle This is a medium circular progress bar • android:progressBarStyleHorizontal This is a horizontal progress bar. • android:progressBarStyleLarge This is a large circular progress bar. • android:progressBarStyleSmall This is a small circular progress bar. • The format looks like this: • style="?android:attr/progressBarStyleHorizontal">
Progress Bar (2) • SetMax(int m) uses to set the bar from 0 .. m • Need to call this when it is setup in java • No setmin, zero is always the min. • setProgress(int p) sets to a specific progress p • intgetProgress() returns the current level • incrementProgressBy(int d) increments the progress by d.
SeekBar • SeekBar is inherits ProgressBar and is touchable/ interactive. Otherwise it is the same as ProgressBar • Add a listener SeekBar.OnSeekBarChangeListener • seekBar.onClickListener to add the listener • Must override 3 methods • public void onStartTrackingTouch (SeekBarseekBar) • Use of a touch gesture • public void onStopTrackingTouch (SeekBarseekBar) • touch gesture ended • public void onProgressChanged(SeekBarseekBar, int progress, booleanfromUser) • The seekBar has changed and it's progress • fromUser is true if a user changed it
ArrayAdapter • When we lists some type of "list" displayed, it uses an ArrayAdapter, which also needs a simple layout provided by android (don't need to create this layouts, unless you want to) • andriod.R.layout.X for the layouts. • http://developer.android.com/intl/zh-CN/reference/android/R.layout.html for the full list. • This can be override to make very fancy views, with say icons and text, clickable ratings and text, etc…
Spinner • A spinner needs "items" to fill the drop down box. This is done using an ArrayAdapter • Using a string[] items, we can • ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items); • adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); • Then use setAdapter(adapter); //fills the list • Add listener, setOnItemSelectedListener(this) • implement AdapterView.OnItemSelectedListener • public void onItemSelected(AdapterView<?> parent, View view, int position, long id) • public void onNothingSelected(AdapterView<?> parent)
Spinner example • Closed Spinner (Drop down box) • Open Spinner
Spinner (2) • If instead, use an xml file with values • res/values/ • <string-arry name="spinnerstuff"> • <item>Stuff</item><item>Stuff2</item> • </string-array> • Change Adapter line to • ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.spinnerstuff, android.R.layout.simple_spinner_item);
DatePickerDialog • DatePicker is a builtin dialog to pick a the date • TimePickerDialog picks the time • Also a ColorPickerDialog in the GraphicsActivity