Transcript
Page 1: Android App Development 03 : Widget & UI

Widget & UIAnuchit [email protected]

3Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Page 2: Android App Development 03 : Widget & UI

Widget

Widget has properties, you can use activity design to config the properties or use layout editor to edit XML document.

Page 3: Android App Development 03 : Widget & UI
Page 4: Android App Development 03 : Widget & UI
Page 5: Android App Development 03 : Widget & UI

Text View

XML Attribute● android:textColor="#33333"● android:text="Hello World"● android:textSize="30sp"

Methods● setText● setTypeFace

Ref: http://developer.android.com/reference/android/widget/TextView.html

Page 6: Android App Development 03 : Widget & UI

Edit Text

XML Attribute● android:inputType="text"

Methods● getText● setText

Ref: http://developer.android.com/reference/android/widget/EditText.html

Page 7: Android App Development 03 : Widget & UI

Edit Text inputType

● text● textPersonName● textPassword● numberPassword● textEmailAddress● phone● textPostalAddress● textMultiLine● time● date

Ref: http://developer.android.com/guide/topics/ui/controls/text.html

● number● numberSigned● numberDecimal● AutoCompleteTextView● MultiAutoCompleteTextView

Page 8: Android App Development 03 : Widget & UI

Buttons

XML Attribute● android:style="?android:attr/buttonStyleSmall"● android:text="Button"

Methods● setOnClickListener

Ref: http://developer.android.com/reference/android/widget/Button.html

Page 9: Android App Development 03 : Widget & UI

Checkboxes

Methods● isChecked● setChecked(true)● setOnClickListener

Ref: http://developer.android.com/reference/android/widget/CheckBox.html

Page 10: Android App Development 03 : Widget & UI

Radio Buttons

Use with RadioGroup LayoutMethods

● isChecked● toggle● setOnClickListener

Ref: http://developer.android.com/guide/topics/ui/controls/radiobutton.html

Page 11: Android App Development 03 : Widget & UI

Toggle Buttons

XML Attribute● android:textOn="Vibrate on"● android:textOff="Vibrate off"

Methods● setOnCheckedChangeListener

Ref: http://developer.android.com/guide/topics/ui/controls/togglebutton.html

Page 12: Android App Development 03 : Widget & UI

Workshop: Input Form

Create form input for register new user to the system (fake), use following fields:

● Firstname Lastname (Edit Text)● Birthdate (Edit Text, Datepicker)● Gender (Radio Button)● Subscribe (Checkbox)● Email (Edit Text)● Password (Edit Text)● Submit Button (Button)

Page 14: Android App Development 03 : Widget & UI

Spinner

The choices you provide for the spinner can come from any source, but must be provided through an SpinnerAdapter, such as an ArrayAdapter if the choices are available in an array or a CursorAdapter if the choices are available from a database query.

Ref: http://developer.android.com/guide/topics/ui/controls/spinner.html

Page 15: Android App Development 03 : Widget & UI

Spinner

String resource file:Spinner spinner = (Spinner) findViewById(R.id.spinner);// Create an ArrayAdapter using the string array and a default spinner layoutArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.services, android.R.layout.simple_spinner_item);// Specify the layout to use when the list of choices appearsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Apply the adapter to the spinnerspinner.setAdapter(adapter);

Page 16: Android App Development 03 : Widget & UI

Spinner

Methods● getSelectedItem● getSelectedItemId● getSelectedItemPosition

Listener● setOnClickListener● setOnItemSelectedListener

Page 18: Android App Development 03 : Widget & UI

List View

The display of elements in a lists is a very common pattern in mobile applications. The user sees a list of items and can scroll through them.

Page 19: Android App Development 03 : Widget & UI

List View

Listener● setOnItemClickListener● setOnLongClickListener

Page 21: Android App Development 03 : Widget & UI

Multi Columns List View

Custom layout listview row, use adapter select data show in custom layout

Page 24: Android App Development 03 : Widget & UI

Alert Dialog

A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Ref: http://developer.android.com/guide/topics/ui/dialogs.html

Page 26: Android App Development 03 : Widget & UI

Dialog with Custom Layout

If you want to create your own dialogs, you create a layout file for the dialog. This layout file is assigned to the dialog via the setContentView() method

Page 28: Android App Development 03 : Widget & UI

Picker

Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs.Using these pickers helps ensure that your users can pick a time or date that is valid, formatted correctly, and adjusted to the user's locale.

Ref: http://developer.android.com/guide/topics/ui/controls/pickers.html

Page 29: Android App Development 03 : Widget & UI

Time Picker

In Android, you can use android.widget.TimePicker class to render a time picker component to select hour and minute in a pre-defined user interface, render a dialog box via android.app.TimePickerDialog

Page 31: Android App Development 03 : Widget & UI

Date Picker

In Android you can use android.widget.DatePicker class to render a date picker component to select day, month and year in a pre-defined user interface and render dialog box via android.app.DatePickerDialog.

Page 33: Android App Development 03 : Widget & UI

ActionBar

The ActionBar is located at the top of the activity. It can display the activity title, icon, actions which can be triggered, additional views Views other interactive items. It can also be used for navigation in your application.

Ref: http://developer.android.com/guide/topics/ui/actionbar.html

Page 34: Android App Development 03 : Widget & UI

ActionBar in old devices

ActionBar has introduced in Android 3.0, The ActionBar Sherlock library allow to use the ActionBar on old devices as Android 1.6

Page 35: Android App Development 03 : Widget & UI

ActionBar : Home Button

The ActionBar shows an icon of your application, this is called the home icon. If you select this icon the onOptionsItemSelected() method will be called with the value android.R.id.home. The recommendation is to return to the main Activity in your program.

ActionBar actionbar = getActionBar();actionbar.setHomeButtonEnabled(true);

Page 37: Android App Development 03 : Widget & UI

Workshop: Navigating Up

Like home button, ActionBar can use as up navigation or goto parent activity, to enable up button use setDisplayHomeUpEnabled() method. You should include FLAG_ACTIVITY_CLEAR_TOP in the Intent.

Page 39: Android App Development 03 : Widget & UI

ActionBar: Action View

a custom View can add to ActionBar, use setCustomView method and enable display of custom View via setDisplay methods in ActionBar.DISPLAY_SHOW_CUSTOM flag.

Page 40: Android App Development 03 : Widget & UI

Workshop: Action View

Create an App with ActionBar, has 2 option menus refresh and setting, after push refresh menu the show the progress bar.

Page 42: Android App Development 03 : Widget & UI

ActionBar: Indeterminate progress

ActionBar can show progress bar use requestWindowsFeature in request to use interminate progress bar

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setProgressBarIndeterminateVisibility(true);

Page 44: Android App Development 03 : Widget & UI

ActionBar: Dimming navigation

You can also hide the software navigation button in your Android application to have more space available. If the user touches the button of the screen the navigation button are automatically shown again.

getWindow(). getDecorView(). setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

Page 46: Android App Development 03 : Widget & UI

Notification

Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity.

Ref: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Page 47: Android App Development 03 : Widget & UI

Notification

To create notifications you use the NotificationManager class which can be received from the Context, e.g. an activity or a service, via the getSystemService() method.

Notification noti = new Notification.Builder(this) .setContentTitle("Title")

.setContentText("Body").build();

Page 48: Android App Development 03 : Widget & UI

Notification

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Hide the notification after its selectednoti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti);

Page 49: Android App Development 03 : Widget & UI

Workshop: Notification

Create App with a single button, after push button the notification will appear in the notification bar and use can cancel notification.

Page 51: Android App Development 03 : Widget & UI

End


Top Related