Posts Tagged ‘gui’

Working with Android LinearLayout

June 1, 2010

Now i am officially started doing and learning Android application development. Started reading various hello world applications and tutorials from

Last week started with rather complex Notepad application tutorial which touches various concepts of android framework, like views,databases,passing extra bundles using intents,starting other android Activities for result and posting the data across the Activities using bundle extras.This Notepad tutorial consists of 4 or 5 exercises that you need to run through to complete the tutorial. I find it nice and very easy way to learn Android using these tutorials.Completed the first exercise and launched the emulator to test it, first surprise on its way, application crashes and “Force close” dialog is displayed on my macbook pro. I was not disappointed by this and continued to do next exercise in spite of the dis-encouraging   start, another surprise followed i couldn’t run the application and with low in spirits i stopped this Notepad exercise and started checking other tutorials.

You can find the notepad tutorial at

Yesterday started with linear layout tutorial , very easy tutorial but sadly nothing to code but just to modify the layout xml to arrange the views horizontally/vertically using “android:layout_orientation”, to specify the importance of the view using “android:layout_weight”. I find it easy to follow the most of the LinearLayout and TextView attributes explained in the tutorial except layout_weight.This is my attempt to explain the way i understood it.

a view occupies the screen based on layout_width, layout_height attributes. If view attributes value says to fill the parent using “fill_parent” it occupies the entire parent (width/height).By default layout weight for a view is zero. If layout weight is changed then the amount of real estate that the view occupies changes according to the weight. For example:

If there are two TextViews in a linear layout oriented horizontally

Layout xml snippet:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”;
With the default layout weight views will appear like this
If the view layout weight is assigned a number then the remaining space in the layout is distributed according to the weight. In the above example if the first TextView is assigned weight 1 then the remaining space is assigned to first TextView and the views will appear like this :

If the TextView 1 assigned weight of 2 and TextView 2 is assigned a weight of 3 then the remaining space of the layout is divided into 5 parts and TextView 1 is assigned 2 parts and TextView 2 is assigned 3 parts. In terms of percentage TextView 1 gets ((2/(2+3))*100)40% of remaining space and TextView 2 gets ((3/(2+3))*100) 60% of remaining space.Resulting layout will appear like this :

That’s all for now! Feel free to leave any comments or suggestions .