Search Wikipedia

Search results

Oct 12, 2013

XML layouts and units of measure in android

Knowing about XML layouts and Units of measure


We should know about XML layouts and Units of measure which is particularly important while developing apps for multiple screen sizes. So, I am practicing some XML features . U may also find this useful.

I goto Main layout file and click activity_main.xml under layout. A graphical editor is opened. Here default layout is Linear Layout. Click activity_main.xml to go to main xml source.
There is android orientation in Linear layout and by default it is vertical which means I can add items in vertical fashion.

Opening Activity_main.xml
I drag a button out in graphical editor. There is a lint error in button because the word ' button' is not saved in string resource. So I have to add 'button' word in string res. In easy way I can fix it by  clicking red color button at right side as shown in figure and clicking this a new window will pop up and click fix . Save project by Crtl+s , then that error is fixed.


Fixing the lint error
Here layout width and height is fill_parent which means this layout gonna fill the whole screen.
And in button by default it is wrap_content. It means this button only gonna take as much space that it requires to take up.

Knowing about fill_parent and wrap_content

Now I got back to graphical layout. Then I got to Properties (if it is not their,go to Window->Show View->Other  & General->Property)
Note: Sometimes change in layout items is not updated in property. In this case right click the item and click Show In and click Property

Like in HTML I don't want to use pixel value (px) for android because different android devices have different pixels. So the preferred unit used in android is dp or dip(device independent pixel).
In case of texts , we can use sp (scale independent pixel).

Besides  Linear layout, android provides others layout facilities also.

Relative Layout: Orientation property is not required here. It means items are gonna placed in layout relatively to each other.There is already a button in layout. I am gonna move button in center by changing gravity of button to center.

Frame layout: Here items are overlapped with each other. This layout is not structured to keep things   separated. I am not gonna use this layout quite often.But this layout could be ueful if for say I want to have a bunch of items in center and I want to turn on/off their visibility.

Using Frame layout for placing items in layout


Table layout: It is the most useful layout. It has same concept like in HTML.First of all , I put the Table Row from layouts which is new row in table. Now I am going to add in new button in Table row. Similarly I create another Table row in my layout and put Text view in it.We can put items in row and column like in table in table layout and items can be positioned in both horizontal and vertical way.
It is useful for creating forms.(image)

Using table layout





1 comment:

Did this post help you? Do you have any questions? Drop your thoughts here...

}