Android Layouts The basic building block for user

Android • • • Layouts The basic building block for user interface is a View Which is created from the View class. The View Group is a subclass of View and provides invisible container. Android Layout Types 1) Linear Layout 2) Relative Layout 3) Table Layout 4) Absolute Layout 5) Frame Layout 6) List View 7) Grid View Ajith G. S: poposir. orgfree. com

Android Layouts • 1) Linear Layout • Android Linear Layout is a view group that aligns all children in either vertically or horizontally. • android: orientation="vertical" Ajith G. S: poposir. orgfree. com

Android Layouts • 2) Relative Layout • Enables to specify how child views are positioned relative to each other. • The position of each view can be specified as relative to sibling elements or relative to the parent. Ajith G. S: poposir. orgfree. com

Android Layouts 3) Table Layout Arranged groups of views into rows and columns. use the <Table. Row> element to build a row in the table. Each row has zero or more cells; each cell can hold one View object. • Table Layout containers do not display border lines for their rows, columns, or cells. • • Ajith G. S: poposir. orgfree. com

Android • • Layouts 4) Absolute Layout Can specify exact locations (x/y coordinates) of its children. Absolute layouts are less flexible. Eg • <Button • android: text="OK" • android: layout_x="50 px" • android: layout_y="361 px" /> Ajith G. S: poposir. orgfree. com

Android Layouts • 5) Frame Layout • Frame Layout is designed to block out an area on the screen to display a single item. • Generally, Frame. Layout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. Ajith G. S: poposir. orgfree. com

Android Layouts • 6) List View • Android List View is a view which groups several items and display them in vertical scrollable list. • The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database. Ajith G. S: poposir. orgfree. com

Android Layouts • 7) Grid View • Android Grid. View shows items in two-dimensional scrolling grid (rows & columns) and the grid items are not necessarily predetermined but they automatically inserted to the layout using a List. Adapter Ajith G. S: poposir. orgfree. com

Android Drawable Resources • A drawable resource represents a graphics that can be drawn on screen. • In Android, these files are stored in res/drawable folder. • To prevent blurred images, drawable resources for different screen resolutions are provided in screen resolution specific drawable folders such as drawable-mdpi, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi. Ajith G. S: poposir. orgfree. com

Android Drawable Resources • Different types of drawables are bitmap, nine-patch, layer list, state list, level list, transition drawable. • Bitmap files are. png, . jpg and. gif. • The preferred bitmap file for android is. png. • Bitmap file needs to be saved in res/drawable folder and can be accessed from xml and java code. • For example if you save a bitmap file god. png in your project, you can access from xml shown below. • <Image. Button android: id="@+id/panda" • android: layout_width="wrap_content" • android: layout_height="wrap_content" • android: src="@drawable/god"/> Ajith G. S: poposir. orgfree. com

Android Drawable Resources • Nine-Patch File • Similar to bitmap, you can use nine-patch files and nine-patch xml. • With stretchable regions to allow image resizing based on content (. 9. png) • Layer List • You can define a list of drawables in xml and use it as drawable resource in your android app. • This drawable is called layer list drawable. • Layer list drawable draws drawable items in the order they are defined in the xml and each drawable item is drawn on top of previous drawable item. • The last drawable item is drawn on the top. Ajith G. S: poposir. orgfree. com

Android Drawable Resources • State List • State list drawable allows to define list of drawables for different states of a view. • An XML file that references different bitmap graphics for different states (for example, to use a different image when a button is pressed). • Level. List • An XML file that defines a drawable that manages a number of alternate Drawables , each assigned a maximum numerical value. Ajith G. S: poposir. orgfree. com

Android Drawable Resources • Transition. Drawable • An XML file that defines a drawable that can cross-fade between two drawable resources. Ajith G. S: poposir. orgfree. com

Android Virtual Device (AVD) • An Android Virtual Device (AVD) is an emulator configuration • that allows developers to test the application by simulating the real device capabilities. • AVD can configure by specifying the hardware and software options. • AVD manager enables an easy way of creating and managing the AVD with its graphical interface. Ajith G. S: poposir. orgfree. com

Android Virtual Device (AVD) • Creating AVD • Go to Window ->AVD Manager and select Virtual Devices. • Click on New to create a Virtual Device, give it some Name and select Target Android Platform from the drop down list • Click “Create AVD” and we are done! • Change Orientation • By default orientation of emulator is vertical • Ctrl+F 11 – to change to vertical Ajith G. S: poposir. orgfree. com

Android Styles • A style is a collection of attributes that specify the appearance for a single View. • A style can specify attributes such as font color, font size, background color, and much more. Ajith G. S: poposir. orgfree. com

Android Styles • Defining Styles • A style is defined in an XML resource that is separate from the XML that specifies the layout. • This XML file contains in res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. • The name of the XML file is arbitrary, but it must use the. xml extension. • You can define multiple styles using <style> tag but each style will have its name that uniquely identifies the style. • Android style attributes are set using <item> tag Ajith G. S: poposir. orgfree. com

Android • • • Styles Eg <? xml version="1. 0" encoding="utf-8"? > <resources> <style name=“My. Font. Style"> <item name="android: capitalize">characters</item> <item name="android: text. Size">12 pt</item> <item name="android: text. Color">#00 FF 00</item>/> </style> </resources> Ajith G. S: poposir. orgfree. com

Android Styles • Using Styles • Once style is defined, you can use it in your XML Layout file using style attribute • <? xml version="1. 0" encoding="utf-8"? > • <Linear. Layout xmlns: android=“xyz. com/apk/res/android" • android: orientation="vertical" > • <Text. View • android: id="@+id/text_id" • style="@style/My. Font. Style" • android: text=“Hello Android" /> • </Linear. Layout> Ajith G. S: poposir. orgfree. com

Android Themes • A theme is a type of style that's applied to an entire app, activity, or view hierarchy— not just an individual view. • When you apply your style as a theme, every view in the app or activity applies each style attribute that it supports. • Themes can also apply styles to non-view elements, such as the status bar and window background. • By adding <item> elements to your custom theme. • These attributes are defined in your styles. xml file. Ajith G. S: poposir. orgfree. com

Android • • • Themes For example, to apply the custom color to the window background, add the following two <item> elements to your custom theme, defined in My. Android. App/res/values/styles. xml file <resources>. . . <style name="My. Custom. Theme". . . > <item name="android: window. Background">@color/my_custom_color</item> <item name="android: color. Background. Cache. Hint">@color/my_custom_color</item> </style>. . . </resources> Ajith G. S: poposir. orgfree. com
- Slides: 21