I have a number of textviews in a linear layout, which itself is sitting within a linear layout within a relative layout (see structure below). I am positioning the top level linear layout to the right of a button in the view by using android:layout_toRightOf to stop the textview overlapping the button when the text is too long.
What I want to do is centre the text in the textviews relative to the entire view, not the linear layout within which it sits (i.e centre it relative to the screen width). I have tried using layout_gravity set to centre in the textview and the relative layout) but with no effect.
Can anyone suggest how to centre the text in this way ?
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/purple"
android:minHeight="64dip"
android:visibility="visible" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dip"
android:layout_toRightOf="#+id/backButton"
android:gravity="center|center_vertical|fill_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/headerTextLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="48dip"
android:layout_marginRight="48dip"
android:gravity="center|center_vertical|fill_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/headerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text=""
android:textColor="#color/white_color"
android:textSize="18sp"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:typeface="sans"
android:singleLine="true"/>
Try this
<TextView
android:id="#+id/headerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text=""
**android:textAlignment="gravity"**
android:textColor="#color/white_color"
android:textSize="18sp"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:typeface="sans"
android:singleLine="true"/>
Related
How can I layout the two edit txt fields in the middle of the screen.
Current layout is not align to middle of the screen (its on the left).
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<EditText
android:id="#+id/edit_code_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:padding="10dp"
android:textSize="20sp"
/>
<EditText
android:id="#+id/edit_code_2"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:padding="10dp"
android:textSize="20sp" />
</LinearLayout>
You have used android:layout_gravity="center_vertical" so it's vertically centered. If you want it to be horizontally centered you can use
android:layout_gravity="center_horizontal"
or if you want both :
android:layout_gravity="center"
Update with comment :
This solution only works if you replace android:layout_width="match_parent" by android:layout_width="wrap_content" in your LinearLayout
You can make your Linear layout width as wrap_content
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
If its parent layout is Constraint Layout you can Constraint it to left and right side and LinearLayout will remain in middle.
In my application I use the custom listview for tableview effect.
But each and every row not align properlly.
only first element give good alignment.
also try the gravity tag.but same effect found.
my xml code:
<?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="50dp"
android:weightSum="5"
android:layout_marginTop="15dp"
android:id="#+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_id"
android:id="#+id/tv_orderid"
android:textSize="20sp"
android:gravity="left"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:gravity="left"
android:id="#+id/tv_name"
android:textSize="20sp"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mobile_number"
android:id="#+id/tv_mobilenumber"
android:textSize="20sp"
android:gravity="left"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_date_and_time"
android:id="#+id/tv_orderdate_and_time"
android:textSize="20sp"
android:gravity="left"
android:layout_weight="2"/>
</LinearLayout>
can any one help me to align all the rows properlly.
Simple use
android:layout_width="0dp"
instead of
android:layout_width="wrap_content"
for each TextView.
The answer is here where is said:
To create a linear layout in which each child uses the same amount of
space on the screen, set the android:layout_height of each view to
"0dp" (for a vertical layout) or the android:layout_width of each view
to "0dp" (for a horizontal layout). Then set the android:layout_weight
of each view to "1".
I am trying to setup a RelativeLayout where two TextView are shown on one line. The first TextView should be aligned to the left. The second TextView should be aligned to the right. If the second TextView text would expand to overlap the first TextView then I want to ellipsize the text in the middle. I don't want to ellipsize the first TextView.
This is what I have come up with. The problem is the second TextView is directly to the right of the first TextView instead of being aligned to the far right. It seems like I cannot use alignParentRight and toRightOf at the same time.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="[DetailedType]"/>
<TextView
android:id="#+id/Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/Left"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:ellipsize="middle"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="[CustomerName]"/>
</RelativeLayout>
The basic idea to achieve this id to add a dummy invisible view in the horizontal center of your RelativeLayout and the tell the left TextView to be to the left of it and the right TextView to be to the right of it... see below
About your problem with aligning it to the right, you should stretch the TextView to take half of the screen and then set android:gravity="right" on the TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/dummy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/Left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#id/dummy"
android:text="[DetailedType]"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/Right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/Left"
android:gravity="right"
android:ellipsize="middle"
android:singleLine="true"
android:text="[CustomerName]"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>
But you can always use LinerLayout to the job
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/Left"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="[DetailedType]"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/Right"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="middle"
android:singleLine="true"
android:text="[CustomerName]"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
From what I understand you only miss android:gravity="right" on your right view.
I'm a noob to android development and I am having issues constructing a layout. I have a relative layout containing a textview and a linear layout containing two checkboxes. I want the textView to appear to the left and the linear layout to appear on the right of the linear layout flush with the edge. Currently, the textview and linearlayout appear on top of each other. Any help is greatly appreciated.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Rotated Shelf: "
android:layout_gravity="center_vertical"/>
<LinearLayout
android:id="#+id/rotated"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_alignParentRight="true">
<CheckBox
android:id="#+id/rotatedshelfYES"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:layout_toLeftOf="#+id/rotatedshelfNO"
/>
<CheckBox
android:id="#+id/rotatedshelfNO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="75dp"
android:text="No"
android:layout_alignParentRight="true" />
</LinearLayout>
</RelativeLayout>
try like this your textview write with in linear-layout and set android:layout_weight="" it working nice
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/rotated"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_alignParentRight="true" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".5"
android:paddingLeft="5dp"
android:text="Rotated Shelf: " />
<CheckBox
android:id="#+id/rotatedshelfYES"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="Yes" />
<CheckBox
android:id="#+id/rotatedshelfNO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="No" />
</LinearLayout>
</RelativeLayout>
on the LinearLayout add android:layout_toRightOf="#+id/textView2"
If you want to align the right edge of your TextView with the left edge of your LinearLayout (i.e. TextView to the left of LinearLayout) you should use
android:layout_toLeftOf="#+id/rotated"
in your TextView properties.
Use you have to set android:layout_width in linear layout equal to wrap_content not match_parent since match_parent is used when we want to equal the width/height as the parent of the widget/container and wrap_content as name suggest requires space only to write the caption of view/container, no extra space required.
I have an activity with three buttons in the layout. All the buttons are at the bottom. I wanted the button to be extreme left,centre and extreme right. The extreme right and the extreme left buttons are fine. However, the centre button is taking up all the space in between the left and right button. However, I want the middle button to take necessary space and not all the space. Also, I am unable to move the imageButton to the middle of the screen. Any help with these things? Here's my XML layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/rel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backgroundnr"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageButton
android:id="#+id/imageButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tempText2"
android:layout_above="#+id/tempText"
android:gravity="center"
android:src="#drawable/start"
android:background="#null"
android:layout_gravity="center_horizontal"
>
</ImageButton>
<TextView
android:id="#+id/tempText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Touch to Start"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
>
</TextView>
<ImageView
android:id="#+id/bottomlayout"
android:layout_width="fill_parent"
android:layout_height="100px"
android:src="#drawable/bottombar"
android:layout_alignParentBottom="true"
>
</ImageView>
<Button
android:id="#+id/profileButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:drawableTop="#drawable/profiledefault"
android:gravity = "bottom"
android:background="#0000"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/bottomlayout"
android:textColor="#FFFFFF"
android:paddingLeft="10dip"
android:paddingBottom="5dip"
>
</Button>
<Button
android:id="#+id/savedButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Saved"
android:drawableTop="#drawable/favoritesdefault"
android:gravity = "bottom"
android:background="#0000"
android:layout_alignParentBottom="true"
android:textColor="#FFFFFF"
android:paddingBottom="5dip"
android:layout_toRightOf="#+id/profileButtons"
android:layout_toLeftOf="#+id/recentButtons"
android:layout_gravity="center_horizontal"
>
</Button>
<Button
android:id="#+id/recentButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recent"
android:drawableTop="#drawable/recentdefault"
android:gravity = "bottom"
android:background="#0000"
android:layout_alignParentBottom="true"
android:textColor="#FFFFFF"
android:layout_alignParentRight="true"
android:paddingRight="10dip"
android:paddingBottom="5dip"
>
</Button>
</RelativeLayout>
use
android:layout_centerHorizontal="true"
for your ImageButton instead of layout_gravity.
and to rearrange your bottom buttons.
don't use leftOf or rightOf for middle button which is "savedButtons" . just use
android:layout_centerHorizontal="true"
for it. And for the left button "profileButtons" use
android:layout_toLeftOf = "#+id/savedButtons"
for the right button "recentButtons" use
android:layout_toRightOf = "#+id/savedButtons"
In this layout middle button will occupy the center space necessary , and the remaining left and right areas are used for the remaining buttons. you will need alignParentBottom for each of 3 buttons.
HTH.