Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

Similar documents
Meniu. Create a project:

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps.

05. RecyclerView and Styles

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { }

Our First Android Application

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

Android Programs Day 5

Vienos veiklos būsena. Theory

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

Mobila applikationer och trådlösa nät, HI1033, HT2013

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

Create Parent Activity and pass its information to Child Activity using Intents.

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

Android 4: New features for Application Development

Android Application Model I

Mobile Application Development MyRent Settings

Have a development environment in 256 or 255 Be familiar with the application lifecycle

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Mobile Computing Fragments

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

Android Using Menus. Victor Matos Cleveland State University

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

Notification mechanism

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Mobile and Ubiquitous Computing: Android Programming (part 3)

Android Using Menus. Victor Matos Cleveland State University

Q.1 Explain the dialog and also explain the Demonstrate working dialog in android.

Manifest.xml. Activity.java

Intents. Your first app assignment

Mobila applikationer och trådlösa nät, HI1033, HT2012

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

Open Lecture Mobile Programming. Intro to Material Design

Basic UI elements: Android Buttons (Basics) Marco Ronchetti Università degli Studi di Trento

UI Fragment.

Embedded Systems Programming - PA8001

Android Workshop: Model View Controller ( MVC):

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Fragment Example Create the following files and test the application on emulator or device.

Preferences. Marco Ronchetti Università degli Studi di Trento

Starting Another Activity Preferences

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Android Beginners Workshop

Android 4: New features for Application Development

Real-Time Embedded Systems

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Lecture 14. Android Application Development

Programmation Mobile Android Master CCI

Fragments. Lecture 11

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Topics of Discussion

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Arrays of Buttons. Inside Android

Learn about Android Content Providers and SQLite

External Services. CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C. Champion

<uses-permission android:name="android.permission.internet"/>

App Development for Smart Devices. Lec #7: Audio, Video & Telephony Try It Out

EMBEDDED SYSTEMS PROGRAMMING UI and Android

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Practical 1.ListView example

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Tabel mysql. Kode di PHP. Config.php. Service.php

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android Services. Victor Matos Cleveland State University. Services

else if(rb2.ischecked()) {

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.

Solving an Android Threading Problem

Embedded Systems Programming - PA8001

Activities. Repo:

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

Mobile Computing Practice # 2c Android Applications - Interface

Creating a Custom ListView

Android AND-401. Android Application Development. Download Full Version :

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Android HelloWorld - Example. Tushar B. Kute,

Simple Currency Converter

ListView Containers. Resources. Creating a ListView

Android Data Storage

ANDROID USER INTERFACE

Android Navigation Drawer for Sliding Menu / Sidebar

LECTURE NOTES OF APPLICATION ACTIVITIES

Basic GUI elements - exercises

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Android & iphone. Amir Eibagi. Localization

Applications. Marco Ronchetti Università degli Studi di Trento

Developed and taught by well-known Contact author and developer. At public for details venues or onsite at your location.


CS 4330/5390: Mobile Application Development Exam 1

getcount getitem getitemid getview com.taxi Class MainActivity drawerlayout drawerleft drawerright...

Android Application Development. By : Shibaji Debnath

User Interface Design & Development

Tłumaczenie i adaptacja materiałów: dr Tomasz Xięski. Na podstawie prezentacji udostępnionych przez Victor Matos, Cleveland State University.

Introducing the Android Menu System

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Dissecting the User Interface of a Set of Highly Diffused Android Apps

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Transcription:

Action Bar

Introduction The Action Bar is a widget that is shown on top of the screen. It includes the application logo on its left side together with items available from the options menu on the right. When creating an action bar we choose which items of the options menu will be display Items of the options menu that are not displayed will be accessible through a drop down list. The Action Bar can provide tabs for navigation between the fragments.

Introduction

Adding Action Bar There is no need to add the action bar. It is already included by default in all activities that target android 3.0 (at the minimum). The "holographic" theme, which is the default theme when targeting android 3, is the one that creates the action bar. An application is considered to "target" Android 3.0 when either android:minsdkversion or android:targetsdkversion attribute in the <uses-sdk> element was set to "11" or greater.

Removing Action Bar We can remove the action bar from a specific activity by setting its theme to Theme.Holo.NoActionBar. <activity android:theme="@android:style/theme.holo.noactionbar"> We can programmatically show and hide the action bar by calling the hide() and show() methods accordingly.

Adding Action Items Each menu item in our options menu can be an action item in our action bar.

Adding Action Items ActionBarDemoActivity.java public class ActionBarDemoActivity extends Activity /** Called when the activity is first creat */ @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button showbt = (Button) findviewbyid(r.id.showbt); showbt.setonclicklistener(new OnClickListener() public void onclick(view view) ActionBar actionbar = getactionbar(); actionbar.show(); );

Adding Action Items Button hidebt = (Button) findviewbyid(r.id.hidebt); hidebt.setonclicklistener(new OnClickListener() public void onclick(view view) ActionBar actionbar = getactionbar(); actionbar.hide(); ); public boolean oncreateoptionsmenu(menu menu) super.oncreateoptionsmenu(menu); MenuItem add = menu.add(0, 1, 0, "Save"); MenuItem open = menu.add(0, 2, 1, "Open"); MenuItem close = menu.add(0, 3, 2, "Close"); add.setshowasaction(menuitem.show_as_action_if_room); open.setshowasaction(menuitem.show_as_action_if_room); close.setshowasaction(menuitem.show_as_action_if_room); return true;

Adding Action Items main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/hidebt" android:text="hide The Action Bar"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/showbt" android:text="show The Action Bar"></Button> </LinearLayout>

Adding Action Items

Adding Action Items When creating the menu using XML we can mark those menu items we wan to be in our action bar.

Adding Action Items ActionBarXMLActivity.java public class ActionBarXMLActivity extends Activity /** Called when the activity is first creat */ @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); @Override public boolean oncreateoptionsmenu(menu menu) MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.optionsmenu, menu); return true;

Adding Action Items optionsmenu.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:orderincategory="1" android:id="@+id/item1" android:showasaction="ifroom withtext" android:title="@string/save" /> <item android:orderincategory="2" android:id="@+id/item1" android:showasaction="ifroom withtext" android:title="@string/edit" /> <item android:orderincategory="3" android:id="@+id/item1" android:showasaction="ifroom withtext" android:title="@string/about" /> <item android:orderincategory="4" android:id="@+id/item1" android:showasaction="ifroom withtext" android:title="@string/help" /> </menu>

Adding Action Items

The Overflow Menu When creating an action bar with too many items the ones that won't have room will be displayed in a separated overflow menu accessible through the top right corner of the screen.

The Overflow Menu public class ActionBarXMLActivity extends Activity /** Called when the activity is first creat */ @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); @Override public boolean oncreateoptionsmenu(menu menu) MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.optionsmenu, menu); return true;

The Overflow Menu <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:orderincategory="1" android:id="@+id/item1" android:showasaction="ifroom withtext" android:title="@string/save" /> <item android:orderincategory="2" android:id="@+id/item2" android:showasaction="ifroom withtext" android:title="@string/edit" /> <item android:orderincategory="3" android:id="@+id/item3" android:showasaction="ifroom withtext" android:title="@string/about" /> <item android:orderincategory="4" android:id="@+id/item4" android:showasaction="ifroom withtext" android:title="@string/help" /> <item android:orderincategory="5" android:id="@+id/item5" android:showasaction="ifroom withtext" android:title="@string/file" /> <item android:orderincategory="6" android:id="@+id/item6" android:showasaction="ifroom withtext" android:title="@string/run" />

The Overflow Menu <item <item <item <item <item <item <item </menu> android:orderincategory="7" android:id="@+id/item7" android:showasaction="ifroom withtext" android:title="@string/source" /> android:orderincategory="8" android:id="@+id/item8" android:showasaction="ifroom withtext" android:title="@string/navigate" /> android:orderincategory="9" android:id="@+id/item9" android:showasaction="ifroom withtext" android:title="@string/search" /> android:orderincategory="10" android:id="@+id/item10" android:showasaction="ifroom withtext" android:title="@string/project" /> android:orderincategory="11" android:id="@+id/item11" android:showasaction="ifroom withtext" android:title="@string/refactor" /> android:orderincategory="12" android:id="@+id/item12" android:showasaction="ifroom withtext" android:title="@string/window" /> android:orderincategory="13" android:id="@+id/item13" android:showasaction="ifroom withtext" android:title="@string/help" />

The Overflow Menu

The withtext Flag The default behavior when the menu item has both an icon and a text is to show the icon only. If we want to show the text then we should add the withtext flag (when the menu is created using XML). When creating the menu in our code we should use the SHOW_AS_ACTION_WITH_TEXT flag calling the setshowasaction() method.

Action Events The menu items placed in our action bar trigger the same callback methods as any other item the options menu includes. When the user selects a menu item from a fragment the onoptionsitemselected() method is first called for the activity that uses the fragment. If it returns false then there will be a call to the onoptionsitemselected() method defined in the fragment.

The Application Icon The application icon appears in the action bar on its left side. This is the default behavior. When the user taps the application bar it responds the same way action items do. The system calls your activity's onoptionsitemselected() method with the android.r.id.home ID. We can override this method and add a condition in order perform the appropriate action, such as to start the main activity of our application instead of getting the user back to the home screen.

The Application Icon If we choose to return the user back to the main activity of our application then we better use the FLAG_ACTIVITY_CLEAR_TOP flag in the Intent object we create. Using this flag if the main activity of our application already exists in the activities task then all activities on top of it will be destroyed and the main activity of our application will be brought to the front and we wont end up with the creation of new instances of the main activity of our application.

The Application Icon @Override public boolean onoptionsitemselected(menuitem item) switch (item.getitemid()) case android.r.id.home: Intent intent = new Intent(this, MainActivity.class); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); return true; default: return super.onoptionsitemselected(item);

The Application Icon When we implement code that when the user taps the action bar icon it takes him back to the previous activity we can indicate about this behavior by calling the setdisplayhomeasupenabled(true) method on our action bar.

The Application Icon public class HomyUpActivity extends Activity /** Called when the activity is first creat */ @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); @Override protected void onstart() super.onstart(); ActionBar actionbar = this.getactionbar(); actionbar.setdisplayhomeasupenabled(true);

The Application Icon @Override public boolean oncreateoptionsmenu(menu menu) MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.optionsmenu, menu); return true;

The Application Icon

Adding Action View We can add a view as an action item. We do so by adding into the item xml element the android:actionlayout attribute assigned with the id of the layout resource we want to display (e.g. @layout/mysearchview). We can alternatively add the android:actionviewclass attribute assigned with the full qualified name of the class that describes the view (e.g. android.widget.searchview) we want to display.

Adding Action View optionsmenu.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> </menu> <item android:orderincategory="4" android:id="@+id/item4" android:showasaction="ifroom withtext" android:title="@string/help" /> <item android:orderincategory="5" android:id="@+id/item5" android:showasaction="ifroom withtext" android:title="@string/file" /> <item android:id="@+id/search" android:title="search" android:showasaction="ifroom" android:actionviewclass="android.widget.searchview" />

Adding Action View HomeUpActivity.java public class HomyUpActivity extends Activity /** Called when the activity is first creat */ @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); @Override protected void onstart() super.onstart(); ActionBar actionbar = this.getactionbar(); actionbar.setdisplayhomeasupenabled(true);

Adding Action View @Override public boolean oncreateoptionsmenu(menu menu) MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.optionsmenu, menu); return true;

Adding Action View

Action Bar Tabs The Action Bar can display tabs that allow the user navigating between different fragments the activity uses. Each one of the tabs can be with an icon and/or a textual title.

Action Bar Tabs public class TabsActivity extends Activity TabsActivity.java /** Called when the activity is first creat */ @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); ActionBar bar = getactionbar(); bar.setnavigationmode(actionbar.navigation_mode_tabs); ActionBar.Tab taba = bar.newtab().settext("a Tab"); ActionBar.Tab tabb = bar.newtab().settext("b Tab"); ActionBar.Tab tabc = bar.newtab().settext("c Tab"); Fragment fragmenta = new AFragmentTab(); Fragment fragmentb = new BFragmentTab(); Fragment fragmentc = new CFragmentTab();

Action Bar Tabs bar.addtab(taba); bar.addtab(tabb); bar.addtab(tabc); protected class MyTabsListener implements ActionBar.TabListener private Fragment fragment; public MyTabsListener(Fragment fragment) this.fragment = fragment; @Override public void ontabselected(tab tab, FragmentTransaction ft) ft.add(r.id.fragment_place, fragment, null);

Action Bar Tabs @Override public void ontabunselected(tab tab, FragmentTransaction ft) ft.remove(fragment); @Override public void ontabreselected(tab tab, FragmentTransaction ft) //...

Action Bar Tabs AFragmentTab.java public class AFragmentTab extends Fragment @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) return inflater.inflate(r.layout.fragment_a, container, false);

Action Bar Tabs BFragmentTab.java public class BFragmentTab extends Fragment @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) return inflater.inflate(r.layout.fragment_b, container, false);

Action Bar Tabs CFragmentTab.java public class CFragmentTab extends Fragment @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) return inflater.inflate(r.layout.fragment_c, container, false);

Action Bar Tabs main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/fragment_place"></linearlayout> </LinearLayout>

Action Bar Tabs