How to strench child elements in LinearLayout - android

I have LinearLayout, and I am trying to make the child elements to be 100%.
so far I have this
[E1] [E2] [E3]
while I want them to be
[E1]
[E2]
[E3]
what properties should I change?? I tried several such as layout_width

Set LinearLayout orientation to vertical
android:orientation="vertical"
And make all elements fill parent.

You should change orientation property of your LinearLayout to vertical

Related

Android - fragments/custom views and layout width/height

When I create a layout for my fragment or my custom view I must define layout_width and layout_height. When I use this fragment or view in another layout, I must also define layout_width and layout_height.
Let's say that the root element of my fragment/view layout has layout_height="match_parent" and when I use this in another layout I have for example <fragment layout_height="wrap_content".
In this case the layout_height element is basically defined twice, once in the layout of the fragment/view itself and once in the layout where I'm using it, but with different values.
What happens to the value of layout_height of the root element of the fragment/view in this case? I don't understand how this works... does one override the other?
when you are using match_parent for parent layout it matches the parent width/height.Now when you are using another layout inside this parent layout
1.if you are setting wrap_content it will not totally use the height/width of parent as it's set to wrap_content
2.setting child layout width/height to match_parent then the child layout will stretch to entire parent limits

Difference in behavior of android:gravity="center_vertical" for RelativeLayout and LinearLayout

Let's say I have two views that I want to center vertically. The first view is bigger than the second view.
I noticed that if I place theses two views inside a RelativeLayout with properties layout_height="wrap_content" and android:gravity="center_vertical" nothing happens. This is what I get :
In the opposite, if I place these two views inside a LinearLayour with properties layout_height="wrap_content" and android:gravity="center_vertical" the views are centered vertically :
Lastly, if I place these two views inside a RelativeLayout with properties layout_height with a fixed height and android:gravity="center_vertical" I get the same result as the LinearLayout. The views are centered vertically.
I would expect the views to be centered vertically in each case. Do you know the reason for this difference?
LinearLayout handles all its child object based on its orientation (Horizontal or vertical). So when you are saying gravity: "center_vertical". You are actually referencing based on your parent layout.
In case of RelativeLayout,it enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.
In your first case it'll work with RelativeLayout as you expected if you use android:layout_centerVertical="true" to the child view which you want to be centred.
So in case of LinearLayout you need to specify the orientation first (i.e. horizontal/vertical) so that the child views are inflated based on the reference of your parent layout.
While in RelativeLayout, as the name says it all, you can specify the position with respect to the views which are the child of a parent RelativeLayout.
Now the views you want to achieve can be generated in many other ways too.
For example, set your parent layout as LinearLayout. Don't specify any gravity attribute in the parent layout. Hence, you set a layout_gravity attribute to the child to certer_vertical and this should work too.
Well, after some others tests, it seems that the behavior of android:gravity for a RelativeLayout is a bit random. I will just avoid to use is.

scrollview fillviewport linearlayout

Is there any way to have a ScrollView with android:fillViewport="true" and a child LinearLayout filling all the view when the content of the LinearLayout is not hight enough?
So far, in a LinearLayout inside a ScrollView we have to use android:layout_height="wrap_content". Can we add something else for filling all ScrollView?
What's the point? LinearLayout content won't stretch to fill ScrollView anyway. If you want to center LinearLayout in (fillViewPorted) ScrollView, use android:gravity="center" in LinearLayout. If you want to use LinearLayout weights to fill screen, don't use ScrollView, as it's height is potentially infinite. If
Set the android:layout_weight of the contents to 1.0. That will force the contents to use the available empty space when it is shorter than the ScrollView. It only works when android:fillViewport=
"true" is set on the scroll view.
Source: http://www.curious-creature.com/2010/08/15/scrollviews-handy-trick/

How to add more Items in Scrollview in Android?

My android page has 10 EditText and 10 TextView. but there is no space in my screen in the Graphical Layout. i just added 5 only. im using Scroll layout. how to add additional 5 items in the screen without reducing the items height. Is there any coding here.?
<ScrollView>
<LinearLayout>
<TextView/>..
....
..
</LinearLayout>
</ScrollView>
You can add multiple items inside a LinearLayout. Since ScrollView is scrollable it won't affect the dimensions of the Views inside. You can add as many views as you need without worrying about screen size or View size..
You should add a LinearLayout as the only child inside your ScrollView. Then get a reference to that LinearLayout :
mLayout=(LinearLayout)findViewById(R.id.myLayout);
and then add Views dynamically from code using :
EditText et=new EditText(...);
//....
mLayout.addView(et);
A ScrollView can have only one direct child, so you need to put all the other Views in a Layout, such as LinearLayout and put that layout in ScrollView
Make your ScrollView's height as match_parent and the inner LinearLayout's height as wrap_content. The LinearLayout will stretch according to the number of children inside it and if the height exceeds the height of the ScrollView, the overflow can be seen by scrolling. If the ScrollView and inner Layout both have same height or if ScrollView have larger height than inner Layout, the scrolling won't happen for obvious reason.

Hierachy of android:layout_width/ height between containers and views

What is the relationship between android:layout_width & android:layout_height in the container and the views contained within?
For instance if I have a LinearLayout with the layout width and height set to fill_parent, and I have a Button with those values set to wrap_content it uses the value from the Button, like the Button values overide the LinearLayout.
However if i swap them around so that the LinearLayout values are both wrap_content and the Button values are now fill_parent it still uses wrap_content, this time not overriding the values, but using the values supplied with the LinearLayout.
Could someone explain how they relate as this is very confusing to me?
Thanks
Kyros
http://developer.android.com/guide/topics/ui/how-android-draws.html
This article explains it very well

Categories

Resources