Layouts And Views
Feb 18, 2016
Layouts And Views
Views & Layouts
Declaring Layouts
• Declare UI elements in XML.• Instantiate layout elements at runtime.
Declare UI elements in XML
Container Properties.
• Orientation• Gravity• Padding• Text• Id
Load the XML Resource
LayoutsDifferent Layouts supported by Android
• Frame Layout: all child views are pinned to the top left corner of the screen
• Linear Layout: each child view is added in a straight line (vertically or horizontally)
• Table Layout: add views using a grid of rows and columns
• Relative Layout : add views relative to the position of other views or to its parent.
• Absolute Layout : for each view you add,
you specify the exact screen coordinate to display on the screen
• Tab Layout : add different tab relative to each other
Views• Text View• Button• Edit Text• Radio button• Checkbox• Spinner• Web View• Image View• Gallery View• Grid View
1. LinearLayout
2. RelativeLayout
res/values/strings.xml
res/values/colors.xml
3. TableLayout
• TableLayout– Root tag for tablelayout.
• TableRow.– Represents individual rows.
4. TabLayout
• TabHost• Root tag for Tablayout.
• TabWidget:• Tab title.
• FrameLayout• Tab body content.
Steps for Tab Creation• Create an Intent to launch an Activity for the tab.
– intent = new Intent().setClass(this, Artists.class);• Initialize a TabSpec for each tab.
– spec = tabHost.newTabSpec("artists")• Add title to TabSpec for each tab.
– tabSpec.setIndicator(“Tab Title", res.getDrawable(R.drawable.icon));
• Add it to the TabHost.– tabSpec.setContent(intent);
• Set initial tab to be displayed– tabHost.setCurrentTab(indexNo);
Binding External Data to Layouts.
• Grid• BaseAdapter
• ListView• ArrayAdapter
• Gallery• BaseAdapter
• SpinnerView• ArrayAdapter
6. Grid
res/layout/grid.xml
7. ListView
Customized Widget
• User defined View• User defined Layout