I have two relative layout inside linear layout. I want the first relative layout left align and the second relative layout right align. I set the following line in the second relative layout but it is not working.
android:layout_alignParentRight="true"
and my full xml file goes here.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100">
<RelativeLayout
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="45"
android:background="#drawable/listview">
<ImageView
android:id="#+id/leftPanelImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:src="#drawable/sports"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="left panel"
android:layout_marginTop="5dp"
android:layout_below="#id/leftPanelImage"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="45"
android:background="#drawable/listview"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/rightPanelImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:src="#drawable/sports"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="right panel"
android:layout_marginTop="5dp"
android:layout_below="#id/rightPanelImage"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
My current code gives an output like this. Second layout start after first layout. I want it to start from the right from parent.
So my question is how can I set the second relative layout right align.
Thank you.
android:layout_alignParentRight is for children within relative layout. Since your relative layouts are children for linear layout, try using
android:layout_gravity="left"
and
android:layout_gravity="right"
Your layout seems to be very overkill, using LinearLayout weights and RelativeLayout for such a simple layout is not necesseary. Also both these methods requiere double measurement which is slower. I would suggest to always use as few elements as possible – for your layout try something like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:weightSum="100">
<TextView
android:layout_width="0dp"
android:layout_weight="45"
android:layout_height="wrap_content"
android:drawableTop="#drawable/sports"
android:drawablePadding="5dp"
android:text="left panel"/>
<View
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="match_parent"/>
<TextView
android:layout_width="0dp"
android:layout_weight="45"
android:layout_height="wrap_content"
android:drawableTop="#drawable/listview"
android:drawablePadding="5dp"
android:text="right panel"/>
</LinearLayout>
android:layout_alignParentRight and android:layout_alignParentLeft are used to align a child right or left inside a relative layout. Of course, it won't work with LinearLayout as parent. You can change your parent layout to RelativeLayout.
android:layout_alignParentRight attribute is meant to work in children of Relative Layouts. You are using in in a child of a Linear Layout.
You can set it to the childs of the RelativeLayout OR use on the parent view android:gravity="right".
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="45"
android:background="#drawable/listview">
<ImageView
android:id="#+id/rightPanelImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:src="#drawable/sports"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right panel"
android:layout_marginTop="5dp"
android:layout_below="#id/rightPanelImage"/>
</RelativeLayout>
Related
Is it possible to overlay 2 textviews in one linear layout? I want them in the same position but they have to be in one linear layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp">
<Textview
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Textview
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
You don't need Linear layout there if you want it to be overlapping .. you can use RelativeLayout or FrameLayout
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdf"/>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sfgsfg"/>
sadly they can't in LinearLayout, but you can use FrameLayout - without an easy option for placing child Views next to each other (as in LinearLayout) - or RealtiveLayout - which have some params for placing one View next to/above another when needed
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp">
<Textview
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Textview
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Been trying to do this for a while, and nothing is working.
I'm trying to vertical and horizontal center a linear layout inside a relative layout.
The point is to get two text-views perfectly centered inside the relative layout.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textColor="#ff0fff"
android:background="#ffffff"
android:textSize="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textColor="#ff0fff"
android:background="#ffffff"
android:textSize="20dp"
/>
</LinearLayout>
</RelativeLayout>
Any help is appreciated.
set attribute centerInParent to true to center Linear Layout in Relative Layout and gravity to center TextView in your LinearLayout
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center">
In android app development, with Linear Layout, I have two buttons side-by-side in a row. I made such an arrangement with TableRow and center aligned it. But the buttons dont fill completely the extra space. How do I make the buttons fill the extra space to the left and to the right?
Here the xml code:
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_margin="7dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textAlignment="center"
android:id="#+id/save"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAlignment="center"
android:id="#+id/cancel"
/>
</TableRow>
The LinearLayout has the following attributes:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context="com.appname.classname">
Here is the sample screen shot:
Instead of using the TableRow, use an horizontal LinearLayout inside the vertical LinearLayout. Note that the default orientation of LinearLayout is horizontal. So you can do soemthing like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/save"
android:text="#string/save"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
<Button
android:id="#+id/cancel"
android:text="#string/cancel"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
</LinearLayout>
The layout_weight attribute will make each item occupy the available space in that ration. In this case, the buttons will share the screen space in a 1:1 ratio.
Set the android:layout_marginRight of the first button to "-8dip" or even more. The sspace should be smaller or gone.
You can use RelativeLayout, there are no margins there.
I have a following layout :
<LinearLayout //container, should adjust height based on CONTENT view height
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="false"
android:padding="20dp">
<RelativeLayout //this is the CONTENT view height
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5">....</RelativeLayout>
...
<RelativeLayout //this is the button layout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<Button android:layout_width="40sp" android:layout_height="40sp"/>
<Button android:layout_width="40sp" android:layout_height="40sp"/>
</RelativeLayout>
</LinearLayout>
I want the height of the container (LinearLayout) to be adjusted to contain all the views in the RelativeLayout (shown on the left, let's call it CONTAINER).
Then, there are two buttons in the RelativeLayout (shown on the right). I need to align them on top and bottom borders of RelativeLayot, correspondingly. What's really important, is that the height of the buttons' container should be the same (should correspond) to the height of the CONTAINER.
The problem is, if I try to use android:layout_alignParentBottom="true" and android:layout_alignParentTop="true" attributes for the buttons, they will stretch the container height, and it will take the whole screen height.
So, what magic should I use to do the trick? :)
Try to align your right relative layout top and bottom to the left one.
Try something like this:
<RelativeLayout //container, should adjust height based on CONTENT view height
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="false"
android:padding="20dp">
<RelativeLayout //this is the CONTENT view height
android:id="#+id/contentRL"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5"
android:layout_alignParentLeft="true">....</RelativeLayout>
...
<RelativeLayout //this is the button layout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_alignTop="#id/contentRL"
android:layout_alignBottom="#id/contentRL"
android:layout_alignParentRight="true">
<Button android:layout_width="40sp"
android:layout_height="40sp"
android:layout_alignParentTop="true"/>
<Button android:layout_width="40sp"
android:layout_height="40sp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Use this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:clickable="false"
android:padding="20dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="5"></RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>
Ok, based on a hint provided by Damien R. above I successfully accomplished the task by doing following:
Use RelativeLayout as a root with paramters layout_width="wrap_content", layout_height="wrap_content"
Use LinearLayout as a "wrapper" around container RelativeLayouts. This is because I need to lay out these containers using layout_weight attribute.
RelativeLayout layout_height should be fill_parent. No need to use android:layout_alignBottom="#id/..." and android:layout_alignBottom="#id/..." in the RelativeLayout attributes. This will only work if RelativeLayout is a child View of another RelativeLayout, and that's not the case, because I need to use LinearLayout's weight
The code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="false"
android:padding="10dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/ticketbackground"
android:id="#+id/ticket_layout"
>
<RelativeLayout
android:id="#+id/contentRL"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:layout_alignParentLeft="true">
</RelativeLayout>
<!--second column-->
<RelativeLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3">
...
</RelativeLayout>
<!--third column with buttons-->
<RelativeLayout
android:id="#+id/sdfsdf"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2">
<Button...
android:layout_alignParentTop="true" />
<Button...
android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
I have a relative layout, and inside it i place 3 items, 2 imageviews and one scrollview.
My layout is like this...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:baselineAligned="true"
android:background="#drawable/background">
<RelativeLayout
android:id="#+id/logosLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_margin="16dp"
android:gravity="center">
<!-- An image will be placed here -->
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#33ffffff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/up" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="8dp"
android:fadingEdgeLength="32dp"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/hotelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/button_background"
android:contentDescription="#string/hotelBtnDesc"
android:gravity="center"
android:src="#drawable/icon1" />
<TextView
android:id="#+id/hotelBtnDescTxtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/hotelBtnDesc"
android:textColor="#ffffff"
android:textSize="14sp" />
<!-- more scrollview items -->
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/down" />
</RelativeLayout>
</LinearLayout>
The above code produces the view shown here:
You may note that the arrows are not aligned in the center, but are slightly moved to the right. Why is that, and how can I fix it ? Note that i have already used android:layout_centerHorizontal="true" to my imageviews.
Thank you in advance
Its problem with your
android:layout_margin="8dp"
remove it from both scrollview and imageView
and pass it to RelativeLayout direct.
or add
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
in your RelativeLayout.
and pass
android:layout_centerHorizontal="true"
to your scrollview
they are aligned to the center if you put the marginRight into consideration, try adding android:layout_margin="8dp" to the arrows.
u need to set image view that is under the Relative layout set this image view width fill parent.
Try removing android:layout_weight="1" from your RelativeLayout and see if it works.
In your LinearLayout, the attribute android:orientation="horizontal" causing your display to be aligned horizontaly. Therefore your RelativeLayout is in center but since its shared with another layout thats why you cant note the difference. If you completely remove the first RelativeLayout (the one with id logosLayout) then i hope you'll see the second layout in center.
So, first you need to define the hierarchy of layout you require and then adjust your views accordingly.
It seems problem with your android:layout_margin="8dp" for your scrollview only and pass it to RelativeLayout direct but instead of using margin use padding = "8dp".