CS499 – Mobile Application Development

Slides:



Advertisements
Similar presentations
Android UserInterfaces Nasrullah Niazi. overView All user interface elements in an Android app are built using View and ViewGroup objects. A View is an.
Advertisements

Programming with Android: Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
 User Interface - Raeha Sandalwala.  Introduction to UI  Layouts  UI Controls  Menus and ‘Toasts’  Notifications  Other interesting UIs ◦ ListView.
User Interface Classes.  Design Principles  Views & Layouts  Event Handling  Menus  Dialogs.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Android Form Elements. Views Provide common UI functionality Form elements: text area, button, radio button, checkbox, dropdown list, etc. Date and time.
By: Jeremy Smith.  Introduction  Droid Draw  Add XML file  Layouts  LinearLayout  RelativeLayout  Objects  Notifications  Toast  Status Bar.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android: Layouts David Meredith
PROG Mobile Java Application Development PROG Mobile Java Application Development Developing Android Apps: Components & Layout.
Android Layouts. Layouts Define the user interface for an activity Layouts are defined in.xml files – within /res/layout folder – different layout can.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Frank Xu Gannon University.  Linear Layout  Relative Layout  Table Layout.
Android Dialog Boxes AlertDialog - Toast
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Programming with Android: Layouts, Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
User Interfaces: Part 1 (View Groups and Layouts).
Application Development for mobile Devices
Presented By: Muhammad Tariq Software Engineer Android Training course.
Import import android.graphics.Bitmap; import android.widget.ImageView;
Copyright© Jeffrey Jongko, Ateneo de Manila University Basic Views and Layouts.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Designing user interfaces using: Simple views 1. Views Basic views – TextView – EditText – Button – ImageButton – CheckBox – ToggleButton – RadioButton.
1 Android Development Lean and mean introduction Based on a presentation by Mihail L. Sichitiu.
CS378 - Mobile Computing User Interface Basics. User Interface Elements View – Control – ViewGroup Layout Widget (Compound Control) Many pre built Views.
Building User Interfaces Basic Applications
BUILDING A SIMPLE USER INTERFACE. In this lesson, you create a layout in XML that includes a text field and a button. In the next lesson, your app responds.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Http :// developer. android. com / guide / topics / fundamentals. html.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CS499 – Mobile Application Development Fall 2012 Programming the Android Platform Application Fundamentals & Quick intro to XML & Anatomy of a Simple Android.
Lab7 – Appendix.
Mobile Computing CSE 40814/60814 Spring 2017.
Android Programming - Features
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
CS499 – Mobile Application Development
Android N Amanquah.
CS240: Advanced Programming Concepts
GUI Programming Fundamentals
Mobile Software Development for Android - I397
Android – Event Handling
Mobile Application Development BSCS-7 Lecture # 8
Android Widgets 1 7 August 2018
Creation of an Android App By Keith Lynn
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android Introduction Hello Views Part 1.
ITEC535 – Mobile Programming
Politeknik Elektronika Negeri Surabaya
HUJI Post PC Workshop 1 Introduction to Android Development Ari Sprung
Android Introduction Hello Views Part 2.
CIS 470 Mobile App Development
CS323 Android Model-View-Controller Architecture
Android Programming Lecture 6
CIS 470 Mobile App Development
Building User Interfaces Basic Applications
Android Developer Fundamentals V2
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Korea Software HRD Center
Mobile Programmming Dr. Mohsin Ali Memon.
Android Sensor Programming
Android Sensor Programming
Android Sensor Programming
Presentation transcript:

CS499 – Mobile Application Development Fall 2013 Programming the Android Platform User Interface

User Interfaces in Android Activities typically display a user interface Android provides many classes for this purpose. Views & Layouts Event Handling Menus Dialogs

User Interfaces in Android Part I: Creating the interface Widgets Layouts & Views Part 2: Wiring the interface Basic wiring Listening for events

Widgets Many predefined interactive UI components (aka widgets) Buttons radio toggle Text field (editable or not) Check box Rating bar …

Widgets

Widgets

Widgets MapView WebView

Widgets Spinner provides a scrollable list of elements User can select one item at a time Items added to spinner with a ListAdapter

Simple UI: text box & button <?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:id="@+id/text" android:layout_width="wrap_content“ android:layout_height="wrap_content" android:text="I am a TextView" /> <Button android:id="@+id/button“ android:layout_width="wrap_content" android:layout_height="wrap_content“ /> </LinearLayout>

TextView <TextView android:id="@+id/text” android:text="I am a TextView" android:layout_width="wrap_content“ android:layout_height="wrap_content" android:textColor=“red” android:gravity=“center_horizontal” android:background=“#aa0000” /> http://developer.android.com/reference/android/widget/TextView.html http://developer.android.com/guide/topics/ui/controls/text.html

EditText <EditText android:id="@+id/text" android:layout_width="wrap_content“ android:layout_height="wrap_content" android:hint=“Enter a number" android:digits=“true” /> http://developer.android.com/reference/android/widget/EditText.html

Button <Button android:id="@+id/mybutton" android:layout_width="wrap_content“ android:layout_height="wrap_content" android:text=“Cancel" /> http://developer.android.com/reference/android/widget/Button.html http://developer.android.com/guide/topics/ui/controls/button.html

RadioButton <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation=“horizontal">     <RadioButton android:id="@+id/radio_yes"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/yes"/>     <RadioButton android:id="@+id/radio_maybe"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/maybe"/>     <RadioButton android:id="@+id/radio_no"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/n0"/> </RadioGroup> http://developer.android.com/reference/android/widget/RadioButton.html http://developer.android.com/guide/topics/ui/controls/radiobutton.html

User Interfaces in Android Part I: Creating the interface Widgets Layouts & Views Part 2: Wiring the interface Basic wiring Listening for events

ViewGroup An invisible View that contains other views Used for grouping and organizing a set of views Base class for layouts and view containers LinearLayout RelativeLayout TableLayout ListView …

ViewGroup & View  Illustration of a view hierarchy, which defines a UI layout. Specified in XML

View 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: hide or show Will come back to this…

Using the layout setContentView(R.layout.main); When you load a layout resource in your app, Android initializes each node of the layout into a runtime object you can use to define additional behaviors, query the object state, or modify the layout. Can do all this in code as well. The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. 

Android:layout android:layout_height, android:layout_width are used for widgets & layouts Difficult to find info/insight into these Three possible values: “fill_parent”, “match_parent”, “wrap_content” “fill_parent”, “match_parent” – equilivent – expand to fit the height (width) of parent layout “wrap_content” – is more ‘fit into available space’ android:layout_weight – allows you to give relative space requirements for the given layout or widget You will need to experiment with these to get the effect you want.

<LinearLayout xmlns:android="http://schemas. android <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical“ … > <LinearLayout android:orientation="horizontal“ … android:layout_weight="1"> <TextView android:text="red“ android:gravity="center_horizontal" android:background="#aa0000“ android:layout_width="wrap_content" android:layout_height="fill_parent“ android:layout_weight="2"/> <TextView android:text="green“ … /> <TextView android:text="blue“ … /> <TextView android:text=“yellow“ … /> </LinearLayout> <LinearLayout android:orientation =“vertical” … > <TextView android:text=“row one” android:textSize=“15pt” … /> <TextView android:text=“row two” android:textSize=“15pt” … /> <TextView android:text=“row three” android:textSize=“15pt” … /> <TextView android:text=“row four” android:textSize=“15pt” … /> LinearLayout

<RelativeLayout xmlns:android="http://schemas. android <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/label“ android:text="Type here:"/> <EditText android:id="@+id/entry" android:background="@android:drawable/editbox_background" android:layout_below="@id/label"/> <Button android:id="@+id/ok" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="OK" /> <Button android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /> </RelativeLayout> RelativeLayout

<TableLayout xmlns:android="http://schemas. android <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent” android:layout_height="fill_parent“ > <TableRow> <TextView android:layout_column="1" android:text="Open...“ android:padding="3dip" /> <TextView android:text="Ctrl-O“ android:gravity="right“ android:padding="3dip" /> </TableRow> <TextView android:layout_column="1“ android:text="Save...“ android:padding="3dip" /> <TextView android:text="Ctrl-S“ android:gravity="right“ android:padding="3dip" /> <TextView android:layout_column="1“ … /> <TextView android:text="Ctrl-Shift-S“ … /> <View android:layout_height="2dip“ android:background="#FF909090" /> <TextView android:text="X“ android:padding="3dip" /> <TextView android:text="Import...“ android:padding="3dip" /> … TableLayout

… <TableRow> <TextView android:text="X“ android:padding="3dip" /> <TextView android:text="Export...“ android:padding="3dip" /> <TextView android:text="Ctrl-E“ android:gravity="right“ android:padding="3dip" /> </TableRow> <View android:layout_height="2dip“ android:background="#FF909090" /> <TextView android:layout_column="1“ android:text="Quit“ android:padding="3dip" /> </TableLayout> TableLayout

Other types: GridView – 2-d scrollable grid TabLayout (not in later OS) – allows multiple activities to share the single content area – one tab per activity – selected tab for the activity with focus

ListView 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 One of the most complicated ViewGroups to use (in my experience) – will return to in more detail a few weeks

User Interfaces in Android Part I: Creating the interface Widgets Layouts & Views Part 2: Wiring the interface Basic wiring Listening for events

Basic Wiring EditText userText = (EditText)findViewById(R.id.intext); String myData = userText.getText().toString(); TextView myText = (TextView)findViewById(R.id.outtext); myText.setText(myData);

Basic Wiring final Spinner spnConversions = (Spinner)findViewById(R.id.conversions); ArrayAdapter<CharSequence> aa; aa = ArrayAdapter.createFromResource(this, R.array.conversions, android.R.layout.simple_spinner_item); aa.setDropDownViewResource(android.R.layout.simple_spinner_item); spnConversions.setAdapter(aa);

User Interfaces in Android Part I: Creating the interface Widgets Layouts & Views Part 2: Wiring the interface Basic wiring Listening for events

Handling View Events Can handle events 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

Handling View Events There are usually multiple ways to do this… creating an adapter and setting setting in the layout onClickListener interface …?? For example, consider a button --

Setting in the layout… in the XML for Button: Button button = (Button)findViewById(R.id.button); button.setText(“Click me”); … public void myOnClick(View v) { /* my action on click */ } in the XML for Button: android:onClick=“myOnClick()”

Creating an adapter… Button button = (Button)findViewById(R.id.button); button.setText(“Click me”); AdapterView.OnClickListener ocl; ocl = new AdapterView.OnClickListener() { public void onClick(View v) { /* my on click action */ } }; button.setOnClickListener(ocl);

onClickListener interface… public class SampleActivity extends Activity implements OnClickListener { … Button button = (Button)findViewById(R.id.button); button.setText(“Click me”); button.setOnClickListener(this); } public void OnClick(View v) { /* my action on click */

Spinner final Spinner spnConversions = (Spinner)findViewById(R.id.conversions); … AdapterView.OnItemSelectedListener oisl; oisl = new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { UCActivity.this.position = position; } public void onNothingSelected(AdapterView<?> parent) { System.out.println("nothing"); }; spnConversions.setOnItemSelectedListener(oisl);

Radio Buttons public void onRadioButtonClicked(View view) { // Is the button now checked? boolean checked = ((RadioButton) view).isChecked(); // Check which radio button was clicked switch(view.getId()) { case R.id.radio_yes: if (checked) // break; case R.id.radio_maybe: if (checked) // break; case R.id.radio_no: if (checked) // break; } } <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation=“horizontal“>     <RadioButton android:id="@+id/radio_yes"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/yes“ android:onClick=“onRadioButtonClicked />     <RadioButton android:id="@+id/radio_maybe"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/maybe“ android:onClick=“onRadioButtonClicked />     <RadioButton android:id="@+id/radio_no"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/n0“ android:onClick=“onRadioButtonClicked /> </RadioGroup>

EditText TextWatcher tw = new TextWatcher(){ public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) { if (etUnits.getText().length() == 0) { btnClear.setEnabled(false); btnConvert.setEnabled(false); } else { btnClear.setEnabled(true); btnConvert.setEnabled(true); } }; etUnits.addTextChangedListener(tw);

Handling View Events Can also handle some events in custom Views onFinishInflate() View and all children inflated onLayout() View must assign a value and position to components onDraw() View should render its content onKeyXXX() A particular key has been pressed onWindowVisibilityChanged() Window containing view has changed its visibility

Toast Quick message to the user – I’ve used this in a manner analogous to a printf for debugging. Never gets focus – designed to be unintrusive. Toast.makeText(getApplicationContext(), “This is my message”, Toast.LENGTH_SHORT).show();

Menus Activities support menus Activities can: Add items to a menu Handle clicks on menu items

Menu Types Options Context Submenu Primary menu shown when user presses the menu button Context View-specific menu to be shown when user touches and hold the view Submenu A menu activated when user touches a visible menu item.

Option Menu and SubMenus

Context Menus

Creating Menus Define menu resource in XML file (/res/menu/filename.xml) Inflate menu resource using MenuInflater in appropriate onCreate…Menu() method Handling item selection in appropriate on …ItemsSelected() methods

HelloAndroidWithMenus

Creating Option Menus public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.top_menu,menu); return true; }

top_menu.xml <menu … > <item android=“@+id/help” android:title=“@string/help” android:icon=“@drawable/ic_menu_help” /> <item android=“@+id/help2” android:title=“@string/help” <item android=“@+id/help3” android:title=“@string/help” android:icon=“@drawable/ic_menu_help” > </item> </menu>

Selecting Option Menu Items public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.help: // do something return true; case R.id.help2: case R.id.help3: }

Dialogs Independent subwindows used by Activities to communicate with the user Dialog subclasses AlertDialog ProgressDialog DatePickerDialog TimePickerDialog

AlertDialog

AlertDialog private final int ALERTTAG=0, PROGRESSTAG = 1; … shutdownButton.setOnClickListener( new OnClickListener() { public void onClick(View v) { showDialog(ALERTTAG); } });

onCreateDialog() 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() { dialog.cancel(); showDialog(PROGRESSTAG); }) .setNegativeButton(“No”, new public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } });

Next week Activities & Intents

Resources http://developer.android.com/guide/topics/appwidgets/index.html http://developer.android.com/guide/topics/ui/controls http://developer.android.com/guide/topics/ui/declaring-layout.html http://developer.android.com/guide/topics/ui/ui-events.html