Android: Text in TextView not aligned well - android

I have a TextView in RelativeLayout in which I am setting the text from a string and that string contains the text more than 2 lines. But I want to set the Text into two lines. All is working but the problem is the Text in two lines is not aligned well.
Here is how it looks:
This is the text of first line
which contains a dummy text
As you can see I have set left margin of 5 then it only applied on first line but not on the second one. So what is the problem and how can I solve it?
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/image"
android:maxLines="2"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium">
</TextView>

Related

Android minLines and maxLines aren't working together in same TextView

Here I have only 1 item in ListView and I'm showing two different TextViews of different colors at the end of the ListView item.
But the issue is I wanna show maximum 3 lines of each TextView but it's not providing me a good result if length of TextView is small. But it works well if text is large.
When I add android:maxLines="3" and text is small, it destroys my layout like
And when I add android:minLines="2" and text is large, it shows complete text like
Give me a way to overcome this problem. My each TextView looks like:
<TextView
android:id="#+id/tv_previous_story"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:maxLines="3"
android:textColor="#color/text_color"
android:textSize="#dimen/privacy_text_size" />
If you want to fix the number of lines ex: 3 just use android:lines="3" in xml.
Change android:layout_width="match_parent",
<TextView
android:id="#+id/tv_previous_story"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ellipsize="none"
android:maxLines="3"
android:singleLine="false"
android:textColor="#color/black"
android:textSize="#dimen/font_normal_size" />
OR
tv_previous_story.setMaxLines(3);
If you want to fix the TextView text use :
android:ems="10"
or any value you want in place of 10.
android:ems or setEms(n) sets the width of a TextView to fit a text of n 'M' letters regardless of the actual text extension and text size.

Keeping Textview content on 1 line and prevent wrapping

I would like to have a text displayed on an Android widget (remote views) not wrap over its text to the next line, if the text is longer than the Textview's width. Instead of that, I would like to have the Textview cut any extra text. I have tried many things without success.
Here is the XML code for the TextView:
<TextView
android:id="#+id/txtInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:maxWidth="220dp"
android:lines="1"
android:text="Long text long text long text long text"
android:textSize="13sp" />
In your layout XML, use android:maxLines="1" or android:singleLine="true"

Android TextView dynamically re-centering

I have a TextView that I want centered in my layout, so I have the following:
<TextView
android:id="#+id/workoutText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is dynamic"/>
My problem is that I don't want the centering placement to be re-evaluated every time the text changes. When the text changes to "This is longer dynamic text", the center of the text will be re-evaluated thus pushing the text over to the left on the screen. I only want the center to be evaluated based on my initial static text I put in the layout. Is there a way to do this?
I have succeeded in accomplishing my end goal by placing another TextView below with:
<TextView
android:id="#+id/workoutText2"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_centerInParent="true"
android:text="This is longer dynamic text"
android:alpha="0"/>
But this feels a bit hacky and would fail to work if the text dynamically changed to something longer than "This is longer dynamic text".
you should add a gravity proprety to the TextView like this
<TextView
android:id="#+id/workoutText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="This is dynamic"/>

How do I wrap a second text around first text title, both are in text view

I am trying to wrap a second text around the first text, both in relative layout and textview such that :
<RelativeLayout
android:id="#+id/headlineRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:paddingRight="2dp"
foo:customFont="proxima-nova-bold.ttf"
android:textColor="#color/orange"
android:textSize="#dimen/title"
android:gravity="left"
/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:layout_toRightOf="#id/text1"
foo:customFont="proxima-nova-regular.ttf"
android:textColor="#color/green"
android:textSize="#dimen/title"
android:gravity="left"
/>
</RelativeLayout>
So, what I am currently getting is :
TEXT1 (TEXT 2 STUFF IS A SENTENCE WHICH
SHOULD WRAP AROUND)
bUT WHAT i expect is :
TEXT1 (TEXT 2 STUFF IS A SENTENCE WHICH
WHICH SHOULD WRAP AROUND)
Any clue?
I may have missed something here, but what you re getting is what I would expect. Each TextView is a rectangular box into which the text is rendered. If you want, you can overlay the boxes, but then the 'TEXT 1' and 'TEXT 2' would lie on top of each other.
To achieve what you want, you will have to think more carefully about what you are doing, and perhaps have three TextViewss:
<TextView 1><TextView 2>
<..... TextView 3 .....>
Whenever you set the text in TextView 2, set it to only show one line and find out how far it got. Then send the rest of the string to TextView 3.

android arranging text view in a layout

I need to have 3 text in layout. the textView in the center is red in color and the other 2 are black. so i added it to a relativelayout and set the layout as textView2 to the right of textView1 and textView3 to the right of textView2
But when the text in the 3rd text view is bigger it should come textview one. insted I get this.
now what should i do to get somthing like this?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/rel_lay"
android:background="#DFDFDF"
android:padding="2dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:text="TextView"
android:textColor="#000000" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:text="TextView"
android:textColor="#FF0000" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_toRightOf="#+id/textView2"
android:text="TextView when the third text is longer"
android:textColor="#000000" />
</RelativeLayout>
this is the relative layout with 3 textView.
Just use one TextView and set text as per you need. You know which text to set for Red color. So what you need to have is index of starting text and ending text.
Use following code.
SpannableString spanText = new SpannableString("My Name is Chintan Rathod");
spanText.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 5 /*start index*/, 10 /*end index*/, 0);
textView.setText(spanText);
Here, I have passed 5 and 10, but you can pass your own indexes. But make sure index within range which will not fire IndexOutOfBoundException
You might have better luck with using Spanned classes and one TextView. The way you are trying to implement it, TextView #3 would have to fill_parent on the width and be able to know where TextView #2 ends.

Categories

Resources