I am trying to layout 4 views (2 buttons, 2 TextViews) at the top of my activity. I need button_2 to be centered horizontally, TextView_2 to take up the rest of the space to the right-hand side of the view, button_1 to wrap_content and be pinned to the left of the parent and textView_1 to fill the space between button_1 and button_2.
My code so far:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="#+id/textView_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="1"/>
<TextView
android:id="#+id/textView_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="button_1"/>
<TextView
android:id="#+id/textView_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/button_1"
android:layout_toLeftOf="#+id/button_2"
android:layout_alignBottom="#+id/button_2"
android:text="textView_1"/>
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="button_2"/>
<TextView
android:id="#+id/textView_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textView_2"
android:gravity="center"
android:layout_alignBottom="#+id/button_2"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/button_2" />
</RelativeLayout>
Relative Layout will suit to your condition perfectly. Use android:layout_toRightOf, android:layout_toLeftOf, android:layout_alignParentRight and android:layout_alignParentLeft properties wisely.
<RelativeLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/textView_1"
android:layout_toRightOf="#+id/button_1"
android:layout_toleftOf="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello there"/>
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/textView_1"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/textView_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/button_2"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Try this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
remove layout weight in child linear layout and height is change to wrap content for textview and button to fulfull your requirements
Related
I have recyclerview and for that, I use a specific view. In that view, I have 3 textviews and an image button. First textview shows me numbers. Others some dummy text. And when my numbers exceed 10(2 characters) everything except numbers moves to the right. How can I handle this?
Here is my xml file:
<RelativeLayout
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="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<LinearLayout
android:id="#+id/linlay"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/buses_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="25dp"
android:textColor="#color/gradStop"
android:layout_marginLeft="15dp"/>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/buses_route_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Bus Route"
android:textColor="#color/gradStart"
android:textSize="20dp"/>
<TextView
android:id="#+id/buses_cycle_duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Duration"
android:textColor="#color/gradStop"
android:textSize="20dp"/>
</LinearLayout>
<ImageButton
android:id="#+id/buses_favourites"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_favorite_border_black_24dp"
android:background="#color/transparent"
android:layout_alignParentRight="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Here is the screenshot how it looks like:
Try giving layout_weight since you are using linear layout
<RelativeLayout
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="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<LinearLayout
android:id="#+id/linlay"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/buses_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight = "0.3"
android:textSize="25dp"
android:textColor="#color/gradStop"
android:layout_marginLeft="15dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight = "0.6"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/buses_route_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Bus Route"
android:textColor="#color/gradStart"
android:textSize="20dp"/>
<TextView
android:id="#+id/buses_cycle_duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Duration"
android:textColor="#color/gradStop"
android:textSize="20dp"/>
</LinearLayout>
<ImageButton
android:id="#+id/buses_favourites"
android:layout_width="0dp"
android:layout_weight = "0.1"
android:layout_height="30dp"
android:src="#drawable/ic_favorite_border_black_24dp"
android:background="#color/transparent"
android:layout_alignParentRight="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
As you can see 30% space of screen width will be taken by your TextView and 60 % will be taken by Other (LinearLayout) and 10% by your ImageView. I am sure you can change the weight according to your design as you get the gist :)
Just set weight 1 to middle Linear layout and set wrap_content for remaining two layout.
<RelativeLayout
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="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<LinearLayout
android:id="#+id/linlay"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/buses_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textSize="25dp"
android:textColor="#000000"
android:layout_marginLeft="15dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight = "1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/buses_route_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Bus Route"
android:textColor="#000000"
android:textSize="20dp"/>
<TextView
android:id="#+id/buses_cycle_duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Duration"
android:textColor="#000000"
android:textSize="20dp"/>
</LinearLayout>
<ImageButton
android:id="#+id/buses_favourites"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"/>
</LinearLayout>
</RelativeLayout>
Your ImageButton attribute android:layout_alignParentRight="true" is not working in linear layout so put outside LinearLayout of ImageButton.
Like this and its wo0rking fine for you.
<RelativeLayout
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="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<LinearLayout
android:id="#+id/linlay"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/buses_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="25dp"
android:textColor="#color/gradStop"
android:layout_marginLeft="15dp"/>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/buses_route_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Bus Route"
android:textColor="#color/gradStart"
android:textSize="20dp"/>
<TextView
android:id="#+id/buses_cycle_duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Duration"
android:textColor="#color/gradStop"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
<ImageButton
android:id="#+id/buses_favourites"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_favorite_border_black_24dp"
android:background="#color/transparent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
</RelativeLayout>
And whenever you used wrap_content it used this property for textView
android:ellipsize="end"
There are millions of questions and answers in related to "layout_gravity Bottom/Top in LinearLayout" on stack overflow but I still haven't solved my problem.
I want to place my EditText which says "Please place me at the very top" at the very top, and TextView which says "Please place me at very bottom" at the very bottom. My dream is very basic but I cannot achieve it!!
Can anyone help me?
This is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:background="#android:color/holo_green_light"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Please place me at very top" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#android:color/background_light"
android:text="TextView"
android:layout_weight="0.19" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_weight="0.70"
android:background="#color/colorAccent"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_gravity="top"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:background="#android:color/background_light"
android:text="Please place me at very bottom" />
</LinearLayout>
</LinearLayout>
And this is my output:
If LinearLayout isn't working, then don't force yourself to use it.
Nested LinearLayouts and weights are bad for performance.
RelativeLayout (or ConstraintLayout) are naturally meant to place things at the anchor points of ViewGroups
I assume you want the text and button centered based on your other attributes
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/holo_green_light"
android:layout_weight="1">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_alignParentTop="true"
android:inputType="textPersonName"
android:text="Please place me at very top" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#android:color/background_light"
android:text="TextView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_red_light"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_centerInParent="true"
android:text="Button"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#android:color/background_light"
android:text="Please place me at very bottom"/>
</RelativeLayout>
</LinearLayout>
Alternatively, you only need android:gravity="bottom" on the second LinearLayout, then all views within are at the bottom.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<!-- Top Gravity -->
<LinearLayout
android:gravity="top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/holo_green_light"
android:orientation="vertical"
android:layout_weight="0.5">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please place me at very top"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="TextView"/>
</LinearLayout>
<!-- Bottom Gravity -->
<LinearLayout
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.50"
android:background="#android:color/holo_red_light"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="62dp"
android:text="Button"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Please place me at very bottom"/>
</LinearLayout>
</LinearLayout>
You have set gravity bottom for your second linear layout to place the views at the bottom.
change this line at first LinearLayout:
android:gravity="center_vertical"
To:
android:gravity="top"
change this line at Second LinearLayout:
android:gravity="center_vertical"
To:
android:gravity="bottom"
Note: Avoid using nested weights it will slow down the performance.
I have created a dialpad using LinearLayout. Here is the code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tv_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tv_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="6" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="8" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="9" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="#" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which looks like this
Now i want the same in RelativeLayout, but i find layout_weight doesn't work in RelativeLayout. I also don't want to use LinearLayout inside RelativeLayout.
Or is there any alternative which can work same like layout_weight works in LinearLayout but for RelativeLayout
You cannot use percentages to define the dimensions of a View inside a RelativeLayout. The best ways to do it is to use LinearLayout and weights, or a custom Layout.
Fore more information you can look at this question
If you want to stay with relative layout you can try to change the locations through the code. get the height and width of the outer layout using:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative);
rl.getWidth();
rl.getHeight();
and then get the params of the layout you want to evenly distribute:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50); //here i created a new one with size 50,50
now you can set params.leftMargin and params.topMargin the way you want with any calculation.
params.leftMargin = rl.getWidth()/4 ;
for example will set the new layout at the 1/4 of the screen width
RelativeLayout is not a good choice because it doesn't provide a way you to evenly distribute children the way LinearLayout does. If you want something other than nested LinearLayouts, you can try using GridLayout (not GridView).
You cannot use layout_weight in RelativeLayout. Looking over your XML, your current layout is poorly structured which will lead to poor performance. I suggest that you use either GridLayout or Use your current LinearLayout but instead of assigning a LinearLayout to each number/character you can do something like this instead (Only for first row, but you get the idea):
`
<LinearLayout
android:id="#+id/LL_topRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_margin="2dp">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="64sp"
android:clickable="true"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="64sp"
android:clickable="true"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="64sp"
android:clickable="true"
android:gravity="center"
android:text="3" />
</LinearLayout>
<!-- Next LinearLayout Here for next row -->
`
Here is my Code
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="wrap_content">
<RadioButton android:id="#+id/preference_question_optionTwo" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
Now here when i apply android:layout_weight="1" on LinearLayout the image does not appear.Without this property image is displayed.
What i want is to divide the layout into two parts. Both parts containing the same code above i.e a radio button and besides that an image and a text
Update This is how i want it
This is what i am getting as output
You didn't share the rest of your code or your intentions on how you want to divide the screen into two: vertically or horizontally.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout android:layout_width="0dp"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_weight="1" >
<RadioButton android:id="#+id/preference_question_optionOne" android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="0dp"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_weight="1" >
<RadioButton android:id="#+id/preference_question_optionTwo" android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
It seems that you got confused by the weight concept. It will be more clear this way, I guess. You should divide the parent's width or height according to those weights. Also don't forget to use it with 0dp.
If you want to divide the parent vertically, use this combination:
android:layout_height="0dp"
android:layout_width="match_parent" <!--or wrap_content-->
android:layout_weight="x"
and for horizontally
android:layout_height="match_parent" <!--or wrap_content-->
android:layout_width="0dp"
android:layout_weight="x"
The layout weight property is for elements within a linear layout. You should give each half of the screen the same weight and put them both within a linear layout.
Here's what you need:
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- first half -->
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="wrap_content">
<RadioButton android:id="#+id/preference_question_optionTwo" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
<!-- first half, same weight as first half -->
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="wrap_content">
<RadioButton android:id="#id/preference_question_optionTwo" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
If You need to divide Your layout in two parts with the same width, you need to set childs width to fill_parent and set the same wieght (> 1) to children.
For example:
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<RadioButton android:id="#+id/preference_question_optionTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
I'm trying to lay some text out on left and right that have a mirroring position as below.
What's happening here is that the last row in the right column ("Some more text") is the widest so it pushes the two rows above further to the right. For the sake of symmetry, the left column is pushed further to the left. The text values are supposed to be dynamic. Does anyone know how the layouts should be designed?
Here's my code (sadly, it doesn't even align to the right!):
<LinearLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
<RelativeLayout android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1">
<RelativeLayout android:id="#+id/description_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
<TextView android:id="#+id/title_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30 DAYS"/>
<TextView android:id="#+id/num_ds_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title_30"/>
<TextView android:id="#+id/num_bs_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/num_ds_30"/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/description_today"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="#id/description_30">
<TextView android:id="#+id/title_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="TODAY"/>
<TextView android:id="#+id/num_ds_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_below="#id/title_today"/>
<TextView android:id="#+id/num_bs_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below="#id/num_ds_today"/>
</RelativeLayout>
</LinearLayout>
You can achieve something like that by nesting LinearLayouts. The outer LinearLayout would have an orientation=horizontal, and the two inner LinearLayout an orientation of vertical with each having a layout_weight=1 and the second (right) LinearLayout would also have gravity="right".
The code would be something like this.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
... />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="right">
<TextView
... />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I think using a RelativeLayout and aligning the TextViews to the left/right is a good approach. Like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/Text1_Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/Text2_Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/Text1_Left" />
<TextView
android:id="#+id/Text3_Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/Text2_Left" />
<TextView
android:id="#+id/Text1_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/Text1_Left"
android:gravity="right" />
<TextView
android:id="#+id/Text2_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/Text1_Right"
android:layout_toRightOf="#id/Text2_Left"
android:gravity="right" />
<TextView
android:id="#+id/Text3_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/Text2_Right"
android:layout_toRightOf="#id/Text1_Left"
android:gravity="right" />
</RelativeLayout>