Avoid TextView resizing if text is too long - android

I have a TextView that when it has a long text, it resizes to make the text fit inside, breaking the UI.
Is there a way to use a XML attribute to make the TextView not resizable?
I was thinking about using the TextView inside a ScrollView, is there other options?

For this to work, set specific width to textview (It will display textview without scroll)
android:layout_width=""
If you want to show scroll, add scrollview with specific size and inside scroll layout, add textview like
<ScrollView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/scrollView"
android:fillViewport="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
100dp is dummy size here.

Solution :
<TextView
android:elipsize="marquee"
</TextView>
check out this Answer for more options :
Elipsize Examples
this will slide the textView

Try this code:
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:singleLine="false"
android:text="Long multiline text"/>

Related

Android: How to align an ImageView to the right of a TextView and make sure the ImageView is always visible

I need to have an Android layout with two views. The first view is a TextView while the second is an ImageView. The ImageView should always be aligned to the right of the TextView. The TextView should be able to expand to fill any remaining space depending on the size of the text. The ImageView should never be hidden by the TextView if the text is too big. The text should be tail truncated in this case.
Here's a visual of what I'm trying to accomplish:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom | center_vertical">
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:ellipsize="end"
android:singleLine="true"/>
<ImageView
android:id="#+id/myImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:src="#drawable/myImageDrawable"
android:layout_toRightOf="#id/myText"/>
</RelativeLayout>
The above XML does not work because TextView hides the ImageView when its text is too big. How can I fix this code? I'm willing to use a different layout as well. Note that the TextView must be a single line.
The below xml is enough to achieve what you want. Just set singleLine to true and use drawableEnd to set your image. Also, replace the text in my code with yours. That's all.
<TextView
android:singleLine="true"
android:text=" HehhsasasashasgghgahgshagshgahsghagshaghsgahsghaHehhsasasashasgghgahgshagshgahsghagshaghsgahsgha shgasagsasghag shahsghag"
android:layout_width="wrap_content"
android:drawableEnd="#drawable/myImageDrawable"
android:layout_height="wrap_content"/>

TextView in HorizontalScrollView not scrolling automatically when text is added

I have a TextView that takes in Strings and I want the TextView to scroll to the last text added on the right automatically. The ScrollView works but I have to scroll manually to the last text added when it's out of the TextView's field of view. Please help.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:fillViewport="true"
android:scrollbars="none">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:scrollHorizontally="true"
android:textColor="#android:color/black"
android:textSize="24sp" />
</HorizontalScrollView>
yourScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
I solved this problem through the use EditTextView instead of TextView and disabling focusEnabled by setting keyListener to null in order to achieve the behavior of a normal TextView and also scroll automatically like I wanted.

My Scrollable textview does not generate blue light

My Scrollable textview does not generate blue light (any light at all) when reaches the top or bottom of a scrollview. And when it scrolls it does not scroll smoothly like other apps. The app is only has a single textview, but the scroll is not smooth. I really need the blue light to show, please help. Here is my xml and the code I used to make the TextView scrollable.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Scrolable"
android:id="#+id/ScrolabletextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:maxLines = "500"
android:scrollbars = "vertical"
android:overScrollMode="always"
/>
and I added this in the java file
TextView textt;
textt = (TextView) findViewById(R.id.ScrolabletextView);
textt.setMovementMethod(new ScrollingMovementMethod());
The behavior that you are talking about comes with a ScrollView. You can simply wrap your text in a ScrollView to get it.
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Scrolable"
android:id="#+id/ScrolabletextView"
android:maxLines = "500"/>
</ScrollView>

android textview right of a multiline textview

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.

Multiline TextView with width "wrap_content"

I am wondering how to have a TextView display its content on several lines without hardcoding the width in the XML.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Any thought welcome.
EDIT: my problem is that when the text exceeds the width set (because it reaches the end of the screen) a portion of the text is just not displayed. I would expect the text to be split on two lines
Though I cannot reproduce the not wrapping problem, you can fix the positioning problem by using a weight on the first TextView. Using the following XML gives the expected output in the graphical layout view in Eclipse:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Also add
android:minLines="2"
android:scrollHorizontally="false"
You could try
android:inputType="textMultiLine"
in your TextView XML. This worked for me.
I think I had very similar problem. I had a TextView with a text, where I was not sure how much lines will it take. It was encapsulated by a LinearLayout having android:layout_width="match_parent" to ensure my text will fill out all the space horizontally. However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully.
I could achieve the solution by changing the LinearLayout that was containing my TextView into a RelativeLayout. By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text.

Categories

Resources