Having fiddled with this for longer than I care to admit, how does text alignment work within a TextView?
Obviously I'm trying to get the 'N' to centre itself vertically in the TextView.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/titleN"
android:id="#+id/textView"
android:height="114dp"
android:textSize="120dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/textView3"
android:layout_gravity="center"
android:gravity="center"/>
When I remove height it looks like:
The problem is that the TextView's height(114dp) is smaller than the size of the text (120dp) itself, remove the height attribute from the TextView, you don't need this line
android:height="114dp" // you need to remove this attribute from the TextView
What you need is android:gravity="center_vertical"
when the gravity of the textview is set for center_vertical text will remain in the center irrespective of the textView's height
Example:-
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="N"
android:textAppearance="?android:attr/textAppearanceLarge" />
Output
as per #Keshav's link to https://stackoverflow.com/a/6594320/2346980, setting android:layout_marginTop to a negative value corrected the text position inside TextView.
Try using maring attributes, like margin-top or margin-bottom. If that doesnt work try using padding, like padding-top or padding-bottom
Related
How do you set a text leading on a TextView? Or how do you shift a text position relative to a view that it is in?
I want the height of the textview to be less than what wrap_content would set at default without cutting the text from the bottom.
I've tried using the layout_height tag on its xml layout but it would clip the text from the bottom just like the image I've provided on the link below, and I also have tried using the lineSpacingExtra tag but it seems to only work for the distance/leading between two lines.
Here is my xml file:
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:text="clipped text"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lineSpacingExtra="-18dp"
android:text="a text longer than usual to demonstrate line spacing"/>
Image for clarity: https://i.stack.imgur.com/QGV4B.png
Why you can use wrap_content? Or can you share what are you trying to achieve?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clipped text"/>
Did you try to do it using padding or paddingTop ? Try it with different values to get your desired result.
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:paddingTop="0dp"
android:text="clipped text"/>
I have a fixed height in dp and layout_width="wrap_content" for my TextView, and the textSize in sp. I do not want a fixed text size (due to variable screen density), and rather I would like the text to scale vertically to the fixed view bounds, and for the TextView width to wrap to the text. I tried using autoSizeTextType="uniform" but the text started wrapping to the next line, which was weird. Has anyone been able to accomplish this vertical scaling with layout_width="wrap_content" for the TextView?
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/name"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:autoSizeTextType="uniform"
android:text="Some Text"
android:background="#drawable/shape_tag_name"
/>
Have you tried use app:autoSizeTextType="uniform"?
Using android:autoSizeTextType="uniform" only works on API level 26 or higher.
One more thing, try to use android:maxLines="1".
I recommend you to have a look on this tutorial
Add two lines in your textview:
android:maxLines="1"
android:ellipsize="end"
Your code like :
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/name"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/white"
android:autoSizeTextType="uniform"
android:text="Some Text"
android:background="#drawable/shape_tag_name"
/>
I have one textview which size is 50 dp. I got this result like the picture below.
I want to remove bottom spaces in my textView ,because its height depend on the text size. And I want to receive background only into my quotation mark.
Is there any way to remove the unused spaces in my textView?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="top"
android:text="“"
android:fontFamily="#font/arialbd"
android:background="#ff0"
android:textColor="#E8E9EF"
android:includeFontPadding="false"
android:textSize="50dp" />
Try this:
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="top|center"
android:text="“"
android:fontFamily="#font/arialbd"
android:background="#ff0"
android:textColor="#E8E9EF"
android:includeFontPadding="false"
android:textSize="50dp"
android:gravity="top" />
This solution works on my case. To remove bottom padding. Set the layout_height the same size as your textSize or 1dp or 2dp less.
Set includeFontPadding="false". Set android:lineSpacingExtra="0dp"
In your case you have to implement custom textView. check this example for reference.
U can try setting your bottom margin to a negative value.
Something like this:
android:layout_marginBottom="-10dp"
Let me know if its works, that is the simplest way but can be anothers...
i am suppose to place a TextView right of a multiline TextView.
please check the code below .
<RelativeLayout
android:id="#+id/rltest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rlanswer"
android:layout_marginLeft="#dimen/grdiviewspacing"
android:layout_marginRight="#dimen/grdiviewspacing"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/lblanswer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:lines="1"
android:text="#string/strbookmarkmessage"
android:textColor="#color/black"
android:textSize="#dimen/listviewlocationsize"
android:textStyle="bold" />
<TextView
android:id="#+id/btnyesiwillcollectitby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#id/lblanswer2"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/stryesiwillcollectitby"
android:textColor="#color/linkcolor"
android:textSize="#dimen/listviewlocationsize" />
</RelativeLayout>
i have tried the problem with the above code is its not showing the second TextView. where us if i try layout_below - its showing the second text view. can anyone please assists me..
i want something like this - blue textview text to read textview.
Use LinearLayout instead of RelativeLayout and give layout_weight=1 to both the TextView you will get result whatever you want.
If you want to keep using a RelativeLayout, you will have to set the width in dip (instead of wrap_content) for the TextView "lblanswer2", otherwise it will always take the whole place if your text is long.
If using a RelativeLayout is not a necessity, you can follow pratik's answer and implement a LinearLayout, this way your 2 TextViews will share the width equally.
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').