Android TextView with multiple lines - android

I want a TextView that should be broken into 4 lines. For e.g.
Vishal Vyas
Having
342
Reputation
Note, the gravity should be center_horizontal
I tried following :
<TextView
android:gravity="center_horizontal"
android:id="#+id/lblUserRep"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:text="VishalVyas Having 342 Reputation" >
</TextView>
This works! but produces following output:
VishalVyas
Having
342
Reputation
Problems:
It doesn't work with the space between words Vishal and Vyas.
android:layout_width="70dp" is harcoded and there can be any name with n number of characters instead of VishalVyas.
Please advice.
Added:
It would be fine if I need to write a custom TextView for achieving this but I'll require some guidance.
Thanks in advance.

I think it's wrapping because "Vishal Vyas" is going beyond 70dp. Instead, do wrap_content on the width and use newline characters for lines instead of wrapping (i.e. "Vishal Vyas\n342\nReputation")

You should be able to insert the newline character \n to control where the splits go. Once you do that, you can expand your TextView wider so that it can accommodate a longer user name but still break in the right place.

android:lines="2"
android:minLines="2"
android:singleLine="false"
Even if Android Studio warns that android:singleLine=false is deprecated, keep it and one can have the number of lines they want for their text box depending on the length of their text

I did so:
The Container of TextView:
android:layout_width="match_parent"
android:layout_height="match_parent"
The TextView:
android:layout_width="match_parent"
android:layout_height="wrap_content"
Then,
The Text of the TextView was showed in two lines or more...

Related

How to limit lines on a TextView

I'm trying to limit a TextView to 1 line, the length of the screen. I'm having trouble keeping it from expanding.
I've tried adding the
android:minLines="1"
android:maxLines="1"
attributes to the TextView xml. This does keep the view from expanding, but this does not limit the characters of the text. For example, if I have entered the maximum number of characters in a line and proceed to add a '(' to the string, I can see the top of it printed below the first line.
Basically, I need a way to limit the amount of characters to the width of the screen. I've tried this:
while(txt.getLineCount() > 1) {
txt.setText(txt.getText().subSequence(0,txt.getText().length()-1));
}
which doesn't work at all. I've also tried that same method with comparing the txt.getHeight() to the txt.getTextSize().
I'm out of ideas. What can I do?
If I use a textview this way it will ocupy only 1 line:
<TextView
android:id="#+id/textView"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Try this. This will limit textview to single line. , if the text was more than one line it will display ... at end.
<TextView
android:id="#+id/textView"
android:singleline="true"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

How to set a TextView to display text in one line in Android

I've set a TextView as the display. I want to display the operation being performed in one line, for example:
9856243187 + 2457896123 - 214583
The problem is, when the input reaches the edge of the TextView - it jumps to the next line.
So, is there anyway to avoid this behavior and keep the input in one line, even if it goes out of the screen?
You should use android:maxLines="1" in your TextView tag.
android:singleLine="true" is deprecated.
android:singleLine="true"
is the answer
Update :
android:singleLine attribute has been deprecated since API Level 3, you can use
android:maxLines="1"
tl;dr:
For new users seeing this question, use android:singleLine="true". Ignore deprecation warnings.
Explanation
According to this bug report, setting android:maxLines="1" may cause your application to crash when some specific values are set in android:ellipsize as well. Apparently it affects most Android devices running 4.1 Jellybean through 6.0.1 Marshmallow (being fixed in 7.0 Nougat). Depending on your device, your mileage may vary. However, if you as a developer are targeting a wide range of devices, it is best to use android:singleLine="true" in order to prevent crashes.
add the code in your Textview
android:singleLine="true"
Thanks guys single line did the tricks but i also had to remove the ellipsis:
android:ellipsize="none"
This code worked for me:
android:singleLine="true"
If you want to manually scroll your single line content then you should use TextView inside HorizontalScrollView
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget" />
</HorizontalScrollView>
Set the TextView to singeline.
Take a look at this
Can you please try this one ?
<TextView
android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Hope this will help you.

Android - TextView ellipsize doesn't work properly

Android ellipsize doesn't work as expected in some cases.
What I'm expecting is three dots on the last line, but in cases it adds the 3 lines plus a few characters related to the rest of the text afterward. Is it possible to change this behavior?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:singleLine="false"
android:ellipsize="end"
android:maxLines="2"
android:layout_marginBottom="16dp"/>
According to me android:ellipsize="end" depends on the words in the TextView.
If following word is lengthy then it omits the word and shows ...
If the following word is small then it will fit correctly and last fill ...
Hope it helps.

Limiting a String to 2 lines in android, then cut it and concatenate a String

My question may sound a bit weird but I'll try to explain it better here.
I ahve a TextView in android, at the inferior part of my activity. I want to have it limited to 2 lines, which is easily reachable by adding the following line in the TextView xml element:
android:maxLines="2"
Okay, now we've got it limited to 2 lines.
Then, in my Activity, I make:
termsandconditions = (TextView) findViewById(R.id.textView1);
termsandconditions.setText(terms);
Okay, now I've got a big string with the terms and conditions, but limited to 2 lines due to the xml attribute.
Now my question is, how can I cut it after having it limited to 2 lines, and concatenate a string with "Read more"? I don't need it to be in the same textView or whatever, I only want that it looks like:
Terms: blablablalblalbla blal blablalblalblalblalbla lalblalblalblalblalblalb lalblalblalb lalblalblalblalblalb lalblalblalb lalblalblalblalb lalb bla View More.
Thanks and I hope you can understand my problem.
You can use setEllipsize (TextUtils.TruncateAt where) method of TextView or the android:ellipsize XML attribute.
public void setEllipsize (TextUtils.TruncateAt where) Added in API
level 1
Causes words in the text that are longer than the view is wide to be
ellipsized instead of broken in the middle. You may also want to
setSingleLine() or setHorizontallyScrolling(boolean) to constrain the
text to a single line. Use null to turn off ellipsizing. If
setMaxLines(int) has been used to set two or more lines, END and
MARQUEE* are only supported (other ellipsizing types will not do
anything).
Related XML Attributes
android:ellipsize
Might be a bit hacky, but here's my suggestion:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="your_text_view"
android:text="long long long text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="link"
android:text="View More"
android:onClick="addMoreLinesAndHideThisTextView"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Some additional tweaking may be needed, but I think you got the idea.

How to wrap text in textview in Android

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.
I have searched and tried the following:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
But none work..
Can anyone suggest how can I do it.
Constraint Layout
<TextView
android:id="#+id/some_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="#id/textview_above"
app:layout_constraintRight_toLeftOf="#id/button_to_right"/>
Ensure your layout width is zero
left / right constraints are defined
layout height of wrap_content allows expansion up/down.
Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
Ellipses are prob. a good idea with max lines android:ellipsize="end"
0dp width allows left/right constraints to determine how wide your widget is.
Setting left/right constraints sets the actual width of your widget, within which your text will wrap.
Constraint Layout docs
For me this issue only occurred on Android < 4.0
The combination of parameters I used were:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
The maxLines count seemed to be the random final piece that made my TextView wrap.
For the case where the TextView is inside a TableLayout, the solution is to set android:shrinkColumns="1" on the TableLayout. (Replace 1 with the column number the TextView you want to wrap is in. (0-indexed))
AFAICT, no other attributes are needed on the TextView.
For other cases, see the other answers here.
FWIW, I had initially gotten it to sort of work with
<TextView
android:id="#+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
but that resulted in some extra empty space at the bottom of the Dialog it was all in.
Use app:breakStrategy="simple" in AppCompatTextView, it will control over paragraph layout.
It has three constant values
balanced
high_quality
simple
Designing in your TextView xml
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:text="Your Question Display Hear....Your Question Display Hear....Your Question Display Hear....Your Question Display Hear...."
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:breakStrategy="simple" />
If your current minimum api level is 23 or more then in Coding
yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
For more refrence refer this BreakStrategy
You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary
This should fix your problem: android:layout_weight="1".
By setting android:maxEms to a given value together with android:layout_weight="1" will cause the TextView to wrap once it reaches the given length of the ems.
OK guys the truth is somewhere in the middle cause you have to see the issue from the parent's view and child's. The solution below works ONLY when spinner mode = dialog regardless of Android version (no problem there.. tested it in VD and DesireS with Android =>2.2) :
.Set you spinner's(the parent) mode like :
android:spinnerMode="dialog"
Set the textview's(child custom view) properties to :
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
I hope this works for you also.
In Android Studio 2.2.3 under the inputType property there is a property called textMultiLine. Selecting this option sorted out a similar problem for me. I hope that helps.
Just was working on a TextView inside a layout inside a RecyclerView. I had text getting cut off, ex, for Read this message, I saw: Read this. I tried setting android:maxLines="2" on the TextView, but nothing changed. However, android:lines="2" resulted in Read this on first line and message on the 2nd.
Try #Guykun's approach
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
Also, make sure that parents width is not set to wrap content. This is the thing that I was missing.
I had the same problem. Following change made it work -
android:layout_width="wrap_content"
The ellipsis, maxLines, or layout_weight - all didn't make any difference.
Note - The parent width is also set as wrap_content.
All you have to do is to set your textview width.
android:layout_width="60dp"
you can change the width to your choice. Just type long sentence to check if it working like this
android:text="i want to be among world class software engineer"
I am using Android 2.2 and my textview will automatically goto the next line if it exceeds the screen.
If you would like to have the text goto the next line before the end of the screen, just add in (just put in your own dp value). This will be useful if you have a picture on the right of the text.
android:layout_marginRight="52dp"
Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
childParams.setMargins(5, 5, 5, 5);
Label label = new Label(this);
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
label.setLayoutParams(childParams);
As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.
When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.
Just set layout_with to a definate size, when the text fills the maximum width it will overflow to the next line causing a wrap effect.
<TextView
android:id="#+id/segmentText"
android:layout_marginTop="10dp"
android:layout_below="#+id/segmentHeader"
android:text="You have the option to record in one go or segments(if you swap options
you will loose your current recordings)"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
The trick is with the textView width, try to make it dedicated number like:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"/>
I've tried many solutions without any result, I've tried:
android:ellipsize="none"
android:scrollHorizontally="false"
the only one thing triggred the wrap option is the dedicated width
You need to add your TextView in a ScrollView with something like this :
<ScrollView android:id="#+id/SCROLL_VIEW"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="#+id/TEXT_VIEW"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
</ScrollView>

Categories

Resources