How to Align Text To the Right in Android TextView? - android

I need to right align my text in a TextView like this.
How to achieve this?
Below is my layout.xml file's textview
<TextView
android:id="#+id/sampleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/dummyTextView"
android:layout_alignLeft="#+id/dummyTextView"
android:gravity="right" />

Did you try?
<TextView
android:id="#+id/txtItemDay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|right"
android:text="Your text"
android:textColor="#android:color/white"
android:textSize="22sp" />
The problem is:
android:layout_width="wrap_content"
You could not align text in TextView if you set with is wrap_content, please set fill_parent or match_parent or specified witdh.

You're using android:layout_width="wrap_content" which means its not able to set the gravity because you don't have enough space for the text to align. Try using this instead:
android:layout_width="fill_parent"

Related

Align TextView to vertical center of ImageView despite the text size?

I have an ImageView and I want it to be followed by a TextView that will always align to it's vertical center despite of the number of lines of text.
Here's what i'm talking about: http://i.imgur.com/fsty3hN.jpg
Since I haven't found anything related to align in vertical center the only property that the text has regarding the alignment is toRightOf the ImageView.
Can someone help me on this matter?
Thx
Instead of an ImageView and a TextView, you can use a compound drawable, which is basically a View that will mix both. In this View, you can specify the gravity and where the image will be located :
<TextView
android:id="#+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/your_image"
android:gravity="center"
android:text="#string/text" />
Try using TableLayout and give gravity to the TextView as center
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/eimaget_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/your_image"
/>
<TextView
android:id="#+id/textt_id"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="text is here"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
Hope it works
set the gravity of your layout in which your image and text is present..you can also set the gravity of the textview to be center vertical or center horizontal or center

Align textview in a block

I wat to create an app like this, I use textview to preview number
<TextView
android:id="#+id/generatenumber1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/generatenumber2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
How to align and color them like this, http://s24.postimg.org/sr53ezhtd/sdsd.png ?
I tried android:layout_gravity="center_vertical|right but not work.
Use android:gravity="center", and add padding to the left and right to create the whitespace between items.
Layout_gravity is used by layouts to define how they put the views inside it. Gravity is used for text/data in a view.
Try this,
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:padding="10dip"
android:text="7"
android:textColor="#fff" />

Limitation of TextView on Android

I want to set TextView max line 2 and show "..." in the line middle.
My layout xml as below:
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="abcdefghijklmnopqrstuvwxyz"
android:ellipsize="middle"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
But it show as below:
abcdefgh
ijklmnop
It show 2 lines, but it doesn't show any "..." at middle or end.
How can I modify it? 
android:ellipsize will work only when there's not enough space to display the whole text of the TextView. In your case, you've set android:layout_width to fill_parent and the text is not long enough, so no ellipsize is being used. Change the android:layout_width to some small amount of dip and you'll see the difference. Hope this helps.
Try to set android:ellipsize="end".
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="abcdefghijklmnopqrstuvwxyz"
android:ellipsize="end" <-----change it to end
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
Set the android:ellipsize property on the TextView as required (probably to 'end').

Double center text issue in TextView and RelativeLayout

I've a problem centering a text in an already centered TextView:
That's the layout:
<RelativeLayout>...
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/w0"
android:layout_margin="10dip"
android:background="#color/green1"
/>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="#id/image"
android:layout_alignTop="#id/image"
android:layout_toRightOf="#id/image"
android:text="15º"
android:textSize="60dip"
style="bold"
android:textColor="#color/blue"
android:gravity="center"
android:layout_marginLeft="7dip"
android:background="#color/green1"
/>
<TextView
android:id="#+id/min_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/temperature"
android:layout_toRightOf="#id/temperature"
android:text="M 21º"
android:textSize="20dip"
style="bold"
android:textColor="#color/blue"
android:layout_marginLeft="5dip"
android:background="#color/orange1"
/>
<TextView
android:id="#+id/max_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/temperature"
android:layout_toRightOf="#id/temperature"
android:text="m 10º"
android:textSize="20dip"
style="bold"
android:textColor="#color/blue"
android:layout_marginLeft="5dip"
android:background="#color/orange2"
/>
And that's the displayed layout image:
What I don't understand is why the 15º isn't centered in the TextView.
The problem is due the TextView's forced center using alignTop and alignBottom but I can't see why it doesn't works.
I know I can solve this using some nesting layouts but I prefer to get a solution using only the RelativeLayout or if it isn't possible understand the reason.
Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now.
Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now.
I don't think so. You can only make things relative to the actual box that the View represents. And in the case of TextView the box is larger than the Text. Any relations that you do make are going to use the position of the Box no matter where the text is at or what it looks like.
Try using
android:layout_centerVertical="true"
on your temperature TextView in addition to the gravity you have now.

Centering a TextView while keeping it to the right of a button?

I'm pretty sure I've done this before, but I've forgotten how.
Here's the problem:
I've got a button and a textview, and I want the textview to be centered, while the button is on the left side.
No problem? Just put them in a relativelayout, make the textview centerinparent, and the button alignparentleft.
But now I'm going to dynamically change the text, so it can potentially be written on top of the button! I'll just add toRightOf="#id/button" on the textview. No, now it's no longer centered.
I wish I could provide a screenshot, but it seems the computer is out of memory and can't do that.
Here's some code: http://pastebin.com/3N70Vjre (Since I can't paste xml...?)
<RelativeLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="#+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="text!"
/>
<TextView
android:id="#+id/toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_toRightOf="#id/leftbutton"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
</RelativeLayout>
Try this (unfortunately I'm at work so can't jump into Eclipse to get you some code) -
Change the layout_width of the TextView to fill_parent.
Set the gravity of the TextView to center (so the text centers inside the TextView)
Set the layout_weight of the Button to 1 and the layout_weight of the TextView to 2. Note that you may have to fudge with these numbers to get the layout you're looking for.
This should center the text of the TextView after the Button, though it will not center the TextView itself. You can accomplish that by replacing the TextView with a container (Linear/Relative Layout) and doing the same method as above on the Layout instead of the TextView. You would then put your TextView inside the container and set the container's gravity to "center".
Hope this helps point you in the right direction :)
You can try this (pseudo-code):
<RelativeLayout>
<Button>
<LinearLayout toLeftOf="toptext" type="horizontal">
<TextView gravity="center">
</LinearLayout>
</RelativeLayout>
You might have to have the LinearLayout as width="fill_parent". Not sure if that will work nor not. You can subsequently try some of the things listed here: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
Try declaring the TextView first, then aligning the button to the left of the text view. Keep in mind you may run into issues if the TextView becomes too wide.
EDIT: I see, so you're trying to do something sort of like the iPhone's header with back/next buttons (similar anyway). Try this modification. I still believe you're going to run into issues if the TextView gets large enough to hit the Button, though.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
<TextView
android:id="#+id/toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_alignParentCenter="true"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
<Button
android:id="#+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="text!"
/>
</RelativeLayout>
Try this FrameLayout instead. This may do more what you're expecting:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
<Button
android:id="#+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text!"
/>
</FrameLayout>

Categories

Resources