� PCA20D04J � ANDROID APPLICATIONS DEVELOPMENT
Sivasankari.R ,Research Scholar, Department of Computer Applications ,SRM IST, Ramapuram
SRM INSTITUTE OF SCIENCE
AND TECHNOLOGY
RAMAPURAM CAMPUS
DEPARTMENT OF COMPUTER Applications
FACULTY OF SCIENCE AND HUMANITIES
Unit 2�
UNIT 2
2
Core Building Blocks
UNIT 2
3
Core Building Blocks
UNIT 2
4
Core Building Blocks
UNIT 2
5
Core Building Blocks
UNIT 2
6
Core Building Blocks
UNIT 2
7
Views and ViewGroups
UNIT 2
8
Views and ViewGroups
UNIT 2
9
Views and View Groups-Linear Layout�
UNIT 2
10
Views and View Groups-Linear Layout�
UNIT 2
11
UNIT 2
12
| |
android:id | This is the ID which uniquely identifies the layout |
android:baselineAligned | This must be a boolean value, either "true" or "false" and prevents the layout from aligning its children's baseline |
android:divider | This is drawable to use as a vertical divider between buttons. You use a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". |
android:gravity | This specifies how an object should position its content, on both the X and Y axes. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc. |
android:orientation | This specifies the direction of arrangement and you will use "horizontal" for a row, "vertical" for a column. The default is horizontal. |
Views and View Groups-Linear Layout�
Views and View Groups-Linear Layout�
<?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:layout_width=”100dp”
android:layout_height=”wrap_content”
android:text=”@string/hello” />
<Button
android:layout_width=”160dp”
android:layout_height=”wrap_content”
android:text=”Button”
android:onClick=”onClick” />
</LinearLayout>
UNIT 2
13
Views and View Groups –Absolute Layout�
UNIT 2
14
Views and ViewGroups – Absolute Layout�
<AbsoluteLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
<Button
android:layout_width=”188dp”
android:layout_height=”wrap_content”
android:text=”Button”
android:layout_x=”126px”
android:layout_y=”361px” />
<Button
android:layout_width=”113dp”
android:layout_height=”wrap_content”
android:text=”Button”
android:layout_x=”12px”
android:layout_y=”361px” />
</AbsoluteLayout>
UNIT 2
15
Views and ViewGroups - TableLayout
UNIT 2
16
Views and ViewGroups - TableLayout
<TableLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_height=”fill_parent”
android:layout_width=”fill_parent” >
<TableRow>
<TextView
android:text=”User Name:”
android:width =”120dp”
/>
<EditText
android:id=”@+id/txtUserName”
android:width=”200dp” />
</TableRow>
<TableRow>
<TextView
android:text=”Password:”
/>
<EditText
android:id=”@+id/txtPassword”
android:password=”true”
/>
</TableRow>
<TableRow>
<TextView />
<CheckBox android:id=”@+id/chkRememberPassword”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Remember Password”
/>
</TableRow>
<TableRow>
<Button
android:id=”@+id/buttonSignIn”
android:text=”Log In” />
</TableRow>
</TableLayout>
UNIT 2
17
Views and View Groups –Relative Layout
UNIT 2
18
Views and View Groups –Relative Layout
<?xml version=”1.0” encoding=”utf-8”?>
<RelativeLayout
android:id=”@+id/RLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
<TextView
android:id=”@+id/lblComments”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Comments”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true” />
<EditText
android:id=”@+id/txtComments” android:layout_width=”fill_parent”
android:layout_height=”170px”
android:textSize=”18sp”
android:layout_alignLeft=”@+id/lblComments”
android:layout_below=”@+id/lblComments”
android:layout_centerHorizontal=”true” />
<Button
android:id=”@+id/btnSave”
android:layout_width=”125px”
android:layout_height=”wrap_content”
android:text=”Save”
android:layout_below=”@+id/txtComments”
android:layout_alignRight=”@+id/txtComments” />
<Button
android:id=”@+id/btnCancel”
android:layout_width=”124px”
android:layout_height=”wrap_content”
android:text=”Cancel”
android:layout_below=”@+id/txtComments”
android:layout_
UNIT 2
19
Views and View Groups –Relative Layout
➤ layout_alignParentTop
➤ layout_alignParentLeft
➤ layout_alignLeft
➤ layout_alignRight
➤ layout_below
➤ layout_centerHorizontal
➤ android:layout_toEndOf
➤ android:layout_toRightOf
➤ android:layout_toLeftOf
➤ android:layout_toStartOf
UNIT 2
20
Views and ViewGroups -FrameLayout�
UNIT 2
21
Views and View Groups –Frame Layout�
<?xml version=”1.0” encoding=”utf-8”?>
<RelativeLayout
android:id=”@+id/RLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
<TextView
android:id=”@+id/lblComments”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello, Android!”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true” />
<FrameLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/lblComments”
android:layout_below=”@+id/lblComments”
android:layout_centerHorizontal=”true” >
<ImageView
android:src = “@drawable/droid”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</FrameLayout>
</RelativeLayout>
UNIT 2
22
Views and View Groups –Scroll View�
UNIT 2
23
Views and View Groups –Scroll View�
ScrollView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical” >
<Button
android:id=”@+id/button1”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Button 1” />
<Button
android:id=”@+id/button2”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Button 2” />
<Button
android:id=”@+id/button3”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Button 3” />
<EditText
android:id=”@+id/txt”
android:layout_width=”fill_parent”
android:layout_height=”600dp” />
<Button
android:id=”@+id/button4”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Button 4” />
<Button
android:id=”@+id/button5”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Button 5” />
</LinearLayout>
</ScrollView>
UNIT 2
24
Views and ViewGroups –ScrollView�
To prevent it from getting the focus, add the following two attributes to the
<LinearLayout> element:
<LinearLayout android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:focusable=”true” android:focusableInTouchMode=”true” >
UNIT 2
25
ADAPTING TO DISPLAY ORIENTATION�
UNIT 2
26
ADAPTING TO DISPLAY ORIENTATION�
UNIT 2
27
ADAPTING TO DISPLAY ORIENTATION�
<RelativeLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android”>
<Button
android:id=”@+id/button1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Top Left”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true” />
<Button
android:id=”@+id/button2”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Top Right”
android:layout_alignParentTop=”true”
android:layout_alignParentRight=”true” />
<Button
android:id=”@+id/button3”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Bottom Left”
android:layout_alignParentLeft=”true”
android:layout_alignParentBottom=”true” />
<Button
android:id=”@+id/button4”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Bottom Right”
android:layout_alignParentRight=”true”
android:layout_alignParentBottom=”true” />
<Button
android:id=”@+id/button5”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Middle”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>
UNIT 2
28
➤ layout_alignParentLeft — Aligns the view to
the left of the parent view
➤ layout_alignParentRight — Aligns the view to
the right of the parent view
➤ layout_alignParentTop — Aligns the view to
the top of the parent view
➤ layout_alignParentBottom — Aligns the view
to the bottom of the parent view
➤ layout_centerVertical — Centers the view
vertically within its parent view
➤ layout_centerHorizontal — Centers the view
horizontally within its parent view
UNIT 2
29
UNIT 2
30
LISTENING FOR UI NOTIFICATIONS
LISTENING FOR UI NOTIFICATIONS
➤ onKeyDown — Called when a key was pressed and not handled by any of the views contained within the activity
➤ onKeyUp — Called when a key was released and not handled by any of the views contained within the activity
➤ onMenuItemSelected — Called when a panel’s menu item has been selected by the user
➤ onMenuOpened — Called when a panel’s menu is opened by the user
LISTENING FOR UI NOTIFICATIONS
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
Toast.makeText(getBaseContext(),
“Center was clicked”,
Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Toast.makeText(getBaseContext(),
“Left arrow was clicked”,
Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Toast.makeText(getBaseContext(),
“Right arrow was clicked”,
Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_UP:
Toast.makeText(getBaseContext(),
“Up arrow was clicked”,
Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Toast.makeText(getBaseContext(),
“Down arrow was clicked”,
Toast.LENGTH_LONG).show();
break;
}
return false;
}
LISTENING FOR UI NOTIFICATIONS
Views in Android
BASIC VIEWS
➤ TextView
➤ EditText
➤ Button
➤ ImageButton
➤ CheckBox
➤ ToggleButton
➤ RadioButton
➤ RadioGroup
BASIC VIEWS
BASIC VIEWS
BASIC VIEWS
TextView View
<?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:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello” />
</LinearLayout>
BASIC VIEWS
<?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” >
<Button android:id=”@+id/btnSave”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”save” />
<ImageButton android:id=”@+id/btnImg1”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:src=”@drawable/ic_launcher” />
<EditText android:id=”@+id/txtName”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<CheckBox android:id=”@+id/chkAutosave”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Autosave” />
ToggleButton android:id=”@+id/toggle1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
BASIC VIEWS
RadioGroup android:id=”@+id/rdbGp1”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical” >
<RadioButton android:id=”@+id/rdb1”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Option 1” />
<RadioButton android:id=”@+id/rdb2”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Option 2” />
</RadioGroup>
</LinearLayout>
ProgressBar View
ProgressBar View
ProgressBar View
<?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” >
<ProgressBar android:id=”@+id/progressbar”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</LinearLayout>
ProgressBar View
ProgressDialog progressBar = new ProgressDialog(this);
progressBar.setCancelable(true);//you can cancel it by pressing back button
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);//initially progress is 0
progressBar.setMax(100);//sets the maximum value 100
progressBar.show();//displays the progress bar
ProgressBar View
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = 0;
progressBar = (ProgressBar) findViewById(R.id.progressbar);
//---do some work in background thread---
new Thread(new Runnable()
{
public void run()
{
//---do some work here---
while (progressStatus < 10)
{
progressStatus = doSomeWork();
}
//---hides the progress bar---
Using Basic Views ❘ 173
handler.post(new Runnable()
{
public void run()
{
//---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
progressBar.setVisibility(View.GONE);
}
});
}
//---do some long running work here---
private int doSomeWork()
{
try {
//---simulate doing some work---
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
return ++progress;
}
}).start();
}
PICKER VIEWS�
PICKER VIEWS -TimePicker
<?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” >
<TimePicker android:id=”@+id/timePicker” android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</LinearLayout>
PICKER VIEWS -TimePicker
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
}
public void onClick(View view) {
Toast.makeText(getBaseContext(),“Time selected:” +timePicker.getCurrentHour() +“:” +timePicker.getCurrentMinute(),
Toast.LENGTH_SHORT).show();
}
PICKER VIEWS -DatePicker View
<?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” >
<DatePicker android:id=”@+id/datePicker”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</LinearLayout>
PICKER VIEWS -DatePicker View
datePicker = (DatePicker) findViewById(R.id.datePicker);
public void onClick(View view)
{
Toast.makeText(getBaseContext(), “Date selected:” + (datePicker.getMonth() + 1) + “/” + datePicker.getDayOfMonth() +
“/” + datePicker.getYear() + “\n” + “Time selected:” + timePicker.getCurrentHour() + “:” + timePicker.getCurrentMinute(),
Toast.LENGTH_SHORT).show());
}
LIST VIEWS
LIST VIEWS - ListView
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---no need to call this---
//setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, presidents));
}
public void onListItemClick(
ListView parent, View v, int position, long id)
{
Toast.makeText(this,
“You have selected “ + presidents[position],
Toast.LENGTH_SHORT).show();
}
String[] presidents = {
“Dwight D. Eisenhower”,
“John F. Kennedy”,
“Lyndon B. Johnson”,
“Richard Nixon”,
“Gerald Ford”,
“Jimmy Carter”,
“Ronald Reagan”,
“George H. W. Bush”,
“Bill Clinton”,
“George W. Bush”,
“Barack Obama”
};
LIST VIEWS - ListView
Customizing the ListView
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lstView = getListView();
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lstView.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, presidents));
}
LIST VIEWS - Spinner View
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent” ndroid:layout_height=”fill_parent”
android:orientation=”vertical” >
<Spinner
android:id=”@+id/spinner1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:drawSelectorOnTop=”true” />
</LinearLayout>
LIST VIEWS - Spinner View
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
<string name=”hello”>Hello World, BasicViews6Activity!</string>
<string name=”app_name”>BasicViews6</string>
<string-array name=”presidents_array”>
<item>Dwight D. Eisenhower</item>
<item>John F. Kennedy</item>
<item>Lyndon B. Johnson</item>
<item>Richard Nixon</item>
<item>Gerald Ford</item>
<item>Jimmy Carter</item>
<item>Ronald Reagan</item>
<item>George H. W. Bush</item>
<item>Bill Clinton</item>
<item>George W. Bush</item>
<item>Barack Obama</item>
</string-array>
</resources>
LIST VIEWS - Spinner View
presidents = getResources().getStringArray(R.array.presidents_array);
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, presidents);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), “You have selected item : “ + presidents[index],Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) { }
Gallery Views
<?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”
<Gallery android:id=”@+id/gallery1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
</LinearLayout>
Gallery Views
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Toast.makeText(getBaseContext(),“pic” + (position + 1) + “ selected”,
Toast.LENGTH_SHORT).show();
}
ImageSwitcher
Image Switcher
GridView�
<GridView
android:id=”@+id/gridview”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:numColumns=”auto_fit”
android:verticalSpacing=”10dp”
android:horizontalSpacing=”10dp”
android:columnWidth=”90dp”
android:stretchMode=”columnWidth”
android:gravity=”center” />
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent,
View v, int position, long id)
{
Toast.makeText(getBaseContext(),
“pic” + (position + 1) + “ selected”,
Toast.LENGTH_SHORT).show();
}
GridView
MENUS
MENUS
options menu
context menu
MENUS
MENUS�
MENUS�
MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”);
{
mnu1.setAlphabeticShortcut(‘a’);
mnu1.setIcon(R.drawable.ic_launcher);
}
MENUS
Arguments of add() method are as follows:
➤ groupId — The group identifier that the menu item should be part of. Use 0 if an item is not in a group.
➤ itemId — A unique item ID
➤ order — The order in which the item should be displayed
➤ title — The text to display for the menu item
Options Menu�
Context Menu�