Beruflich Dokumente
Kultur Dokumente
Advanced Google
Android Development
Lesson #1
Welcome Back!
Over the next set of sessions, we’re going to look at more
advanced programming topics of the Android OS.
• Additional UI elements.
• Styling UI elements.
• Google Maps and GPS.
• Play audio and video.
• Gestures.
• SQLite database engine.
• File operations.
• Phone Access and SMS.
1
3/10/2012
* Use 32-bit versions of the software, even if you’re running a 64-bit OS.
2
3/10/2012
The above represents a bare minimum. The more you know about
Java, the easier Android development will become.
3
3/10/2012
4
3/10/2012
More Advanced UI
Today, we’ll have a look at some additional UI elements that we
have not had a chance to work with yet.
• Tabs
• Menus
• Alerts & Dialogs
Tabs
Tabs are a common interface element to use when you have lots of
information to display to the user, or when your application performs
numerous tasks, and you want to separate them out from one
another. All tabs can share the same activity, or launch new
activities as necessary.
5
3/10/2012
Tabs
Tabs can use icons, and Google provides a complete specification on
the design and implementation of the icons for all past and current
versions of the SDK.
http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html
Basically, tab icons created for Android 2.0 and higher should go into drawable-xxxx-v5
folder where xxxx is either hdpi, mdpi, or ldpi (high density, medium density, or low
density). Older versions of Android store their icons in drawable-hdpi, drawable-mdpi,
and drawable-ldpi folders.
Tab icons should have two states, selected and unselected. Each image (PNG file) for
the icon should be named in the following way: name_selected.png or
name_unselected.png, where name is the name you want to give your icon.
Tabs
You will also need to create Selector XML file that defines the icon
to use for the normal and selected state of each of your tabs. The
XML file is stored, along with the icons, in the appropriate drawable
folder, and can be used in code to set the tab’s icons.
Example:
<?xml version="1.0" encoding="utf‐8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!‐‐ selected state ‐‐>
<item android:drawable="@drawable/icon1_selected"
android:state_selected="true"
android:state_pressed="false" />
<!‐‐ unselected state (default) ‐‐>
<item android:drawable="@drawable/icon1_unselected" />
</selector>
6
3/10/2012
Tabs Layout
The layout xml file for the tabbed interface will need to host several
layouts in order to work correctly:
TabHost TabHost, LinearLayout and TabWidget
provide the outer structure for your
LinearLayout
tabs, while the FrameLayout provides
TabWidget the area for each tab’s content. Each
tab’s layout can be created separately
FrameLayout and then brought into the tabbed
interface.
Tab 1 Layout
Each tab’s layout uses an id such as
Tab 2 Layout “tab1”, “tab2” etc., so we can
reference this in code.
Tab 3 Layout
Tabs Layout
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout…
</FrameLayout>
</LinearLayout>
</TabHost>
7
3/10/2012
Tabs Code
In our main activity, we need to locate the TabHost object, then
setup our tabs based on what we have provided in XML.
TabHost oTBHost=(TabHost)findViewById(R.id.tabhost);
oTBHost.setup();
TabHost.TabSpec oSpec= oTBHost.newTabSpec(“spec1");
oSpec.setContent(R.id.tab1);
oSpec.setIndicator("Tab 1“, getResources().getDrawable(R.drawable.icon1));
oTBHost.addTab(oSpec);
oSpec=oTBHost.newTabSpec(“spec2");
oSpec.setContent(R.id.tab2);
oSpec.setIndicator("Tab 2", getResources().getDrawable(R.drawable.icon2));
tabs.addTab(spec);
…
Tabs
Within the same activity, you can then proceed with capturing all
additional user input, button click events, and other interactions.
8
3/10/2012
Menus
Menus are a great way to allow your application to branch to other
activities, without taking any space within your current activity.
Menus
Menu XML is stored in res menu folder, with the file name
typically called “menu.xml”.
<item android:id="@+id/mnu1" android:title="Menu 1" android:icon="@drawable/menu_icon1"/>
<item android:id="@+id/mnu2" android:title="Menu 2" android:icon="@drawable/menu_icon2" />
<item android:id="@+id/mnu3" android:title="Menu 3" android:icon="@drawable/menu_icon3" />
<item android:id="@+id/mnu4" android:title="Menu 4" android:icon="@drawable/menu_icon4"/>
<item android:id="@+id/mnu5" android:title="Menu 5" android:icon="@drawable/menu_icon5" />
<item android:id="@+id/mnu6" android:title="Menu 6" android:icon="@drawable/menu_icon6" />
<item android:id="@+id/mnu7" android:title="Menu 7" android:icon="@drawable/menu_icon6"/>
<item android:id="@+id/mnu8" android:title="Menu 8" android:icon="@drawable/menu_icon6" />
<item android:id="@+id/mnu9" android:title="Menu 9" android:icon="@drawable/menu_icon6" />
</menu>
9
3/10/2012
Menus
To initialize the menu from your code, use the following method:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Menus
Add an onOptionsItemSelected routine to deal with menu click
events:
case R.id.mnu1:
Toast.makeText(getApplicationContext(), "Menu 1 clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.mnu2:
Toast.makeText(getApplicationContext(), "Menu 2 clicked!", Toast.LENGTH_SHORT).show();
break;
…
}
return true;
}
10
3/10/2012
Dialogs
Dialog boxes can be used to ask users questions, force an
acknowledgement of something, or can be completely custom, with
their own layout defined in XML.
Dialogs
To create a dialog, you can use the following code:
final AlertDialog.Builder oDiag = new AlertDialog.Builder(this);
oDiag.setIcon(android.R.drawable.ic_dialog_alert);
oDiag.setTitle("Title");
oDiag.setMessage("This is an alert!");
oDiag.setPositiveButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
11
3/10/2012
Custom Dialogs
Custom dialogs should use a layout, defined in res layout folder.
This layout is not different in any way from a standard activity
layout.
You can then create an instance of the dialog class, and tell it to use
that layout. Use the dialog’s show() method to display it. Use
dismiss() to close it.
d = new Dialog(this);
d.setContentView(R.layout.about_dialog);
12