I'm new to android studio... So I've been trying to make a simple app. When I want to put two linear layouts in another one, One of them goes out of the frame!
I don't know if I'm doing this right or not.
Also here are the pictures (the second one is the problem):
1)http://i.imgur.com/2H1hOxk.jpg
2)http://i.imgur.com/5IeZHsC.jpg
thanks
From your pictures, your parent linear layout which contains the other two linear layouts has its orientation set to "horizontal". It must be set to "vertical" to be above each other...
In your parent linear layout, you will find this:
android:orientation="horizontal"
Change it to:
android:orientation="vertical"
For horizontal orientation, you need to set both of the inner LinearLayout widths to 0dp and set their weights to 1.
For veritcal orientation, you need to set both of the inner LinearLayout heights to 0dp and set their weights to 1.
Without the weight attribute, since the first LinearLayout width is set to match_parent, it takes up the entire LinearLayout.
Example:
in your case of a horizontal layout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0d"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0d"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
Related
The attached image contain 3 layouts
Relative layout
Linear layout
Linear layout
Both the linear layouts are of same size and are overlapped.
All i want to know is how to arrange those two linear layouts inside the relative layout so that Linear layout 1 & Linear layout 2 will have 90% of parent height. Also linear layout 1 must be aligned to the top of relative layout while linear layout 2 to the bottom of relative layout.
Any simple working solution will be appreciated.( I'm a newbie to android studio)
From the Android documentation:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
app:layout_heightPercent="90%"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="match_parent"
app:layout_heightPercent="90%"
android:layout_alignParentBottom="true"/>
</android.support.percent.PercentRelativeLayout>
The order in which the LinearLayouts will overlap corresponds to the order in which they are defined.
Here is another Way
Set Linear Layout Height to your desired level (fixed)
Specify android:gravity property for both Linear Layouts (1'st Layout Top & 2'nd Layout Bottom.
Then you can get the required result.
Nice question, What you can do is use PercentRelativeLayout instead of RelativeLayout so you can adjust that 90% height, and then use this property
android:layout_alignParentTop = true
for your first LinearLayout and
android:layout_alignParentBottom = true
for second RelativeLayout
It will stick your LinearLayouts inside RelativeLayout as you want.
See this questions for how to use PercentRelativeLayout here
Sample Layout here
And If you also want Layering effect as you saw in one of pic use
android:elevation property on your both `LinearLayout`.
Code taken from another answer by #Svit for full explaination
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
app:layout_heightPercent="90%"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="match_parent"
app:layout_heightPercent="90%"
android:layout_alignParentBottom="true"/>
</android.support.percent.PercentRelativeLayout>
Below see the XML code and a screenshot.
Summary:
One parent LinearLayout with weightSum = 100
One child LinearLayout with layout_weight = 25
One child LinearLayout with layout_weight = 75
Result:
In Android Studio layout renderer the children ratio is OK (25:75)
In the simulator the ratio between the children is not OK (clearly not 25:75)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<LinearLayout
android:background="#color/md_orange_600"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:layout_gravity="center_horizontal">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_margin="#dimen/default_separator"
android:background="#color/md_green_400"
android:layout_weight="75">
</LinearLayout>
</LinearLayout>
Some notes:
The layout_height is 0dp on both children (as it is recommended for weights)
I colored the background so the area assignment is visible
Found the root problem:
It turns out my main activiy layout has a CoordinatorLayout as a primary container.
CoordinatorLayout does children placement like FrameLayout, therefore my AppBarLayout and the main fragment container (FrameLayout) overlapped.
I just enclosed them both into a vertical oriented LinearLayout and now they don't overlap.
Corollary, my 2 cents for developers out there: It is tempting to bypass problems without understanding their real cause (I could have just add layout_marginTop on my fragment to "shift it down") but in the end, bypass after bypass you end up understanding less and less of your own application. Always look to understand the real cause of the problem you experience, then the solution will be real too.
I'm trying to find documentation or guidelines as to behaviour of a UI component with the layout_width/layout_height set to match_parent within a ViewHolder of layout_width/layout_height of wrap_content.
So for instance, what is the EXPECTED result of the following layout?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
/>
</RelativeLayout>
By the description here, the layout of your example would depend on what you put inside the LinearLayout. That content would expand to fill all available vertical space within the parent RelativeLayout. By default, the LinearLayout would be positioned at the top left of the parent RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_gradient" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<ImageButton
android:id="#+id/buttonLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/log"
android:onClick="log" />
</RelativeLayout>
</FrameLayout>
I was expecting my button to appear in the center of the screen. However, it appears on the TOP center of the screen (that is, the button is center horizontally, but not vertically).
It seems to me that the RelativeLayout is behaving like it was defined with "wrap_content" instead of "fill_parent".
The funny thing is that, if I give an actual value to my RelativeLayout height property (android:layout_height), like:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:gravity="center" >
Then the button behaves correctly (i.e. the button is also centred vertically). But I don`t want to use actual values. I want to use fill_parent! Why doesn't it work with "fill_parent" ??
Does anybody know what's going on?
Thank you in advance!
RelativeLayout requires you to specify the position of the elements in the Layout. I don't see any layout_below or layout_toLeftOf tags. Gravity works on LinearLayouts. In general, LinearLayouts are easier to work with, and they scale much better to different screen sizes. I suggest you replace the RelativeLayout by a LinearLayout, and also the FrameLayout by a LinearLayout. You use a FrameLayout typically if you want to use multiple overlapping layouts, which you don't do.
I recommend you read up on using layouts in the Android sdk reference documentation, like here: http://bit.ly/djmnn7
You specified fill_parent for both the layout_width and layout_height of your RelativeLayout, therefore it fills up it's parent view.
By default, a relative layout arranges it's children to the top-left corner, regardless you use fill_parent for the size.
You should achieve the desired aspect by taking advantage of the RelativeLayout's own attribute set, which helps you arrange the child views relatively to each other or to their parent:
<ImageButton
android:id="#+id/buttonLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/log"
android:onClick="log" />
Using the android:layout_centerInParent you can achieve this. This attribute if set true, centers this child horizontally and vertically within its parent.
What is the best layout to use tp produce a data entry form like this one in android:
Should I use a vertical linear layout,, with a horizental layout for each of the items? or a relative layout. I can't use a table layout because each text entry might have it's own width.
Thanks
As main layout, you can use a linear layout vertical, and for each row, a linear layout horizontal, setting width to fill_parent.
For the 4 first rows (data form container), when setting width, use "0 dip" for width and set a proportion to layout_weight as you need, keep the same value for the 3 next row in order to keep the alignement. Dont forget to set gravity right fo first column
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="50dip"
android:paddingRight="50dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="label"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:hint="value"
android:layout_marginLeft="15dip"/>
</LinearLayout>
</LinearLayout>
Do so, for last row with 0.5 proportion for each column.
Think, that helped you...
relative layout will be useful and use padding attribute to obtain the UI as image
As per my view you can pick linear layout it will be easy to handle
one Linear layout with vertical orientation
and 5 linear layouts with horizontal orientation