Professional Documents
Culture Documents
Android
USER INTERFACES
Spinners
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Populating Spinner
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
Static Data Binding using Resource File
});
AutocompleteTextView
Provides user a
suggestion other than
the default dictionary
Programmer can
provide different kind of
sources for suggestion
AutocompleteTextView Example
<AutoCompleteTextView
android:id="@+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Static Data Binding using Resource File
<resources>
<string-array name=“countries_array">
<item>New Calendonia</item>
<item>Bangladesh</item>
<item>New Zealand</item>
<item>England</item>
<item>Papua New Guinea</item>
<item>Japan</item>
<item>Korea</item>
<item>Australia</item>
</string-array>
</resources>
Binding the resource to Widget
// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView =
(AutoCompleteTextView)
findViewById(R.id.autocomplete_country);
// Get the string array
String[] countries =
getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);
CheckBox
<CheckBox android:id="@+id/checkbox_meat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/meat"
android:onClick="onCheckboxClicked"/>
<CheckBox android:id="@+id/checkbox_cheese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheese"
android:onClick="onCheckboxClicked"/>
CheckBox Event Handling
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = (CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_meat:
if (checked)
// Put some meat on the sandwich
else
// Remove the meat
break;
case R.id.checkbox_cheese:
if (checked)
// Cheese me
else
// I'm lactose intolerant
break;
// TODO: Veggie sandwich
}
}
RadioButton
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content" The RadioGroup is a subclass
android:layout_height="wrap_content" of LinearLayout that has a
android:text="@string/pirates" vertical orientation by default.
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
RadioButton Event Handling
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = (RadioButton) view).isChecked();
Used to
select
different
component
s of time
TimePicker Example
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pick_time"
android:onClick="showTimePickerDialog" />
Time Picker Class
public class TimePickerFragment extends DialogFragment implements
OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
}
}
Using Time Picker Class
public class TabTutorialActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
…
}
Used to
select
different
component
s of date
DatePicker Example
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pick_date"
android:onClick="showDatePickerDialog" />
Date Picker Class
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
Using Date Picker Class
public class TabTutorialActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
…
}
Hides content out of the screen and allows the user to drag a handle to bring
the content on screen.
The size of the SlidingDrawer defines how much space the content will occupy
once slid out so SlidingDrawer should usually use fill_parent for both its
dimensions
SlidingDrawer Example
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
….
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:handle="@+id/handle"
android:content="@+id/content"
>
<ImageView
android:id="@id/handle"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:id="@id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
…..
</LinearLayout>
</SlidingDrawer>
</FrameLayout>
Vertical ScrollView
......
</LinearLayout>
</ScrollView>
Horizontal ScrollView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />……
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Tab Selection
• TabHost
• The main container for the tab
buttons and tab contents
Three • TabWidget
component • Implements the row of tab buttons,
which contain text labels and
s optionally contain icons
• FrameLayout
• The container for the tab contents
Tab Components
TabHost
FrameLayout1 FrameLayouts
Tab Layout Example
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
>
Tab Layout Example
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tab1"
android:orientation="vertical"
android:paddingTop="60dp">
<TextView android:layout_width="fill_parent"
android:layout_height="100px"
android:text="This is tab1"
android:id="@+id/txt1"
/>
……
</LinearLayout>
<LinearLayout
..Second Tab Content
</LinearLayout>
</FrameLayout>
</TabHost>
Tab Layout Changing content from Code
</LinearLayout>
Including the file in Tab
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
>
<include
layout="@layout/layout3"/> Including the
</FrameLayout> layout
</TabHost>
Menu
• Options Menu
• Invoked by hardware Menu
Two
button
• Context Menu
• A floating list of menu items that
Types appears when a user touches and
holds a particular item displayed
in the view, which has a menu
associated with it.
Options Menu
The Menu File
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file"
android:title="File"
android:icon="@drawable/ic_launcher" >
<menu>
<item android:id="@+id/create_new"
android:title="Create New" />
<item android:id="@+id/open"
android:title="Open" />
Submenu
</menu>
</item>
<item android:id="@+id/menu_bookmark"
android:icon="@drawable/ic_launcher"
android:title="Bookmark" /> menu.xml file in layout
……
folder
</menu>
Attaching the Options Menu
switch (item.getItemId())
{
case R.id.menu_bookmark:
Toast.makeText(getApplicationContext(), "Bookmark is
Selected", Toast.LENGTH_SHORT).show();
return true;
….
default:
return super.onOptionsItemSelected(item);
}
}
Context Menu
Attaching the Context Menu