0

I have a linear layout which has 4 childs with different weights. When user click a button at first child , i want to add new child to parent linear layout with different weight ( 0dp height ) . But when i am adding new child all childs height is changing.How to add new view without change height percantage ?

    ------
    view 1 (25%)
    ------  
    ------
    view 2 (25%)
    ------  
    ------
    view 3 (25%)
    ------  
    ------
    view 4 (25%)
    ------  

after adding new layout programmatically

     ------
    view 1 (25%)
    ------  
    ------
    view 2 (25%)
    ------  
    ------
    view  3 (25%)
    ------  
    ------
    view 4 (25%)
    ------   
    ------
    view 5 (25%)
    ------    

total layout will be 125% and scrollable. Thanks

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
dracula
  • 4,413
  • 6
  • 26
  • 31

2 Answers2

1

When you use weights, the sizes of your views are determined dynamically to ensure that they all views fit within the size of the parent - with proportions determined by the weights.

From what you described, a LinearLayout does not sound like the correct solution to your problem. A ListView may be a better approach, which will automatically provide the scrolling functionality when the total size of all items exceeds the size of the ListView itself. ListView is easy to implement and it gives you scrolling and separators between items automatically. Have a look at ListView developer's guide for more information.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • but i have custom views that fill fit the screen by weights. i think i can't do it with ListView – dracula May 13 '13 at 11:05
  • @user2317087 You can have custom views in the ListView with dynamically computed size - that's very simple indeed. – Aleks G May 13 '13 at 11:10
  • i have a login button,which has another views below. and want to slide down a login view at login button click.thats my aim.. thanks – dracula May 13 '13 at 11:11
  • @user2317087 In that case, you should have a ScrollView with a LinearLayout as a child. Set the linearlayout's height to "wrap_content" and add the view to that layout. When you increase the size of your login view from 0 to its full size, the linearlayout height will adjust accordingly and scrollview will allow for scrolling if the linearlayout's size exceeds the display size. – Aleks G May 13 '13 at 11:14
  • yes , you are right. but as i wrote at first , my linear layout's heights are based on weights because of multiple screen support. and when i add new child i have to re-define all weights. – dracula May 13 '13 at 11:34
  • @user2317087 What you are trying to do is not possible with purely defining the sizes/weights in XML. You will need to dynamically set the sizes at runtime. – Aleks G May 13 '13 at 11:44
  • You seem to want to use weights to support multiple screen sizes, but your purposed use of weight appears to be counter-productive to this - each view should take up to 25% of the height? this will look horrible on a ldpi device. ListView with wrap_content heights sounds much nicer – FunkTheMonk May 13 '13 at 13:31
0

Yes you can use Custom style listview for autoscrolling and getting expected look and feel. You can use like this:

<ListView
                android:id="@+id/"
                android:layout_width="200dp"
                android:layout_height="wrap_content" 
                android:divider="@color/off_white"
                android:dividerHeight="2dp"/>
Aleks G
  • 56,435
  • 29
  • 168
  • 265
Bebin T.N
  • 2,539
  • 1
  • 24
  • 28