How to set text length in button - android

I want to have a fixed size button
If the text is longer than button width , it should show "tex..."
How do I do this?

Ellipsize can be used but there is a bug. The workaround is to set scrollHorizontally to true and lines to 1. See the following code example:
<Button android:text="Button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:scrollHorizontally="true"
android:lines="1"
android:ellipsize="end">
</Button>

Please try below code in button
android:maxLength="5"

I'd use aString.length(); and if it is greater than whatever int you choose, shorten it with aString = aString.substring(0, aString.length() - int); and make that int aString.length() - the max length you want. then take the new string and append the ... with aString.append("...");

Use a textview instead of a button and use the ellipsize property to get the 3 dots. Also you will need to have a click listener on the textview to make it function as a button.
You will also need to use : android:scrollHorizontally="true" for the "..." to appear ( bug in android :-( )

Related

Android EditText.setError not working in not focusable

How can I display a text error with setError in an EditText not focusable? It's important that users don't modify this EditText, it'll be modified for the application. Maybe I have another option different than focusable=false?
Now, I have it:
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:focusable="false"
android:inputType="text"
android:onClick="onClick" />
Thanks a lot!
Finally I think it's not possible to do... Because the first necessity is to block the text, doing it with focusable=false or with a TextView, and it also blocks the functionality setError.
An editText with focusable = false I can get a right drawable (the default red exclamation mark) but without text. For this reason I finally added the text with a Toast.
It's not completelly that I wanted, but it's the most similar.
Thanks for your help!
Just use TextView and if you have an error somewhere then show image with drawableRight.
Here's the example of doingit programmatically: https://stackoverflow.com/a/7380789/3864698

TextView ellipsize style on Android

I use below layout.xml to show ellipsize:
<TextView
android:id="#+id/my_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true" />
And it can show ellipsize in TextView success.
But the ellipsize style in some devices only show one ".".
Such as below picture between M and k:
And some devices can show "...".
Such as below picture between J and p:
I want to let all devices show as style 2.
How can I modify it?
I think if you use … which represents ellipsize, it should work.
So for your example, it would be...
ABCDEFGHIJ…PQRSTUVWXYZ
Give it a go.
Don't use android:singleLine="true".You can use android:maxLines="1" .It's awsome

What does ellipsize mean in android?

I've added an EditText to my layout, and added a hint, and made it centered horizontally.
When running the application, the hint was invisible. I found that I should make ellipsize value of the TextView to be start:
<EditText
android:id="#+id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:ems="10"
android:gravity="center_horizontal"
android:hint="#string/hint1" />
In Android documentation, I read:
If set, causes words that are longer than the view is wide to be
ellipsized instead of broken in the middle.
The problem is that ellipsize is not found in the dictionary. Can anybody explain to me what benefits we can gain by ellipsize attribute? And what is the difference between start, end, middle?
You can find documentation here.
Based on your requirement you can try according option.
to ellipsize, a neologism, means to shorten text using an ellipsis, i.e. three dots ... or more commonly ligature …, to stand in for the omitted bits.
Say original value pf text view is aaabbbccc and its fitting inside the view
start's output will be : ...bccc
end's output will be : aaab...
middle's output will be : aa...cc
marquee's output will be : aaabbbccc auto sliding from right to left
In my experience, Ellipsis works only if the below two attributes are set.
android:ellipsize="end"
android:singleLine="true"
for the width of TextView, wrap_content or match_parent should both be good.
android:ellipsize added in API Level 1. An ellipsis is three periods in a row. (...) .
In your .xml
<TextView
....
android:text="Hi I am Amiyo, you can see how to ellipse works."
android:ellipsize = "end" />
At this point, the ellipsis will not display yet as a TextView is set to automatically expand on default when new text is entered. You will need to limit TextView in some way. Do this, you can use either add to your TextView a scrollHorizontally, minLines, or maxLines to have the ellipsis display.
To make the ellipse:
at the end: this is how it would.
use: android:ellipsize = "end"
And
in the middle:
use: android:ellipsize = "middle"
And
at the start:
use: android:ellipsize = "start"
And
to have no ellipse
use: android:ellipsize = "none"
Note Please :
Do not use android:singeLine = "true", it is deprecated.
android:maxLines = "1" will not display the three dots (...)
android:lines = "1" will not display the three dots (...)
For more details you can visit here
An ellipsis is three periods in a row...
The TextView will use an ellipsis when it cannot expand to show all of its text. The attribute ellipsized sets the position of the three dots if it is necessary.
Text:
This is my first android application and
I am trying to make a funny game,
It seems android is really very easy to play.
Suppose above is your text and if you are using ellipsize's start attribute it will seen like this
This is my first android application and
...t seems android is really very easy to play.
with end attribute
This is my first android application and
I am trying to make a funny game,...
Here is an example on how ellipsize works without using deprecated android:singleLine="true" in a ConstraintLayout:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="13sp"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="long long long long long long text text text" />
remember if you have a text that is supposed to be in a single line, then change the maxLines to 1.
Note: your text must be larger than the container box for the following to marquee:
android:ellipsize="marquee"
Set this property to EditText. Ellipsize is working with disable EditText
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:singleLine="true"
android:editable="false"
or setKeyListener(null);
Use ellipsize when you have fixed width, then it will automatically truncate the text & show the ellipsis at end,
it Won't work if you set layout_width as wrap_content & match_parent.
android:width="40dp"
android:ellipsize="end"
android:singleLine="true"
As it is already mentioned above if there is no enough space for text (due to the size of the layout or set limitation) ellpsize will add ... dots to it.
Here an example:
Top: with ellipsize (here it is set to 'end'
Bottom: without

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>

android append '…' at the end textview _EDIT

I have a lsit view and i that i need to add some text.
in the adapter, I need to append ... at the end and i used the following
when i give
android:ellipsize="end"
in the only shows 2 line
eg:
asdnfsdfdsf asdfsdfasdf
sdfsd sdfsdf sdfsd ad...
And when i give
android:ellipsize="start"
it is as :
asdnfsdfdsf asdfsdfasdfd
...sdfsd sdfsdf sdfsd ad
The complete code i used:
<TextView android:layout_width="wrap_content" android:background="#016A7D"
android:layout_height="80dp" android:textColor="#000000"
android:maxLines="4"
android:id="#+id/list_report_desciption"
android:ellipsize="end"
/>
And the output i got is:
What exactly will a smarter planet
look like? How's IT changing? A...
actually it contains more than 6 lines
Is there any thing i need to set so that i need to get 3 lines
Thanks
Text ellipsization seems buggy, accroding to this report:
http://code.google.com/p/android/issues/detail?id=10554
A workaround allows ellipsizing one line of text, by setting the android:singleLine attribute to true. But this attribute is deprecated.
Nothing worked for me:-(
I just fixed the length. Cut the string in that length and append '...' after that.
Now its working for me.
I was looking for a simple solution for this myself, I ended up with something like:
private String getTextWithMoreIndicator(TextView textView, String text) {
long maxVisibleChars = textView.getPaint().breakText(text, true, textView.getMeasuredWidth(), null);
if(maxVisibleChars < text.length()) {
text = text.substring(0, (int)maxVisibleChars -3) + "...";
}
return text;
}
The solution you accepted will not scale correctly on all devices.
Reason : the width of "a" is lesser than "A", so, if you are truncating string based on some size (say 15 letters) and adding "..." through code. You might see results not expected.
Now coming to the solution:-
Solutions:
Solution # 1 :
Add the following 3 attributes to your TextView and you'll get the result you want :)
<TextView
android:ellipsize="end"
android:lines="1"
android:scrollHorizontally="true" />
Solution # 2 :
Another workaround might be, you could decide to marquee text (the fancy animation moving your text from right to left). For that you need following attributes in your TextView xml:-
<TextView
android:id="#+id/my_text_view"
android:ellipsize="marquee"
android:lines="1"
android:scrollHorizontally="true"
android:marqueeRepeatLimit="marquee_forever" />
And then in you code you need to get the TextView by its id and put-in the following line:-
TextView myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView.setSelected(true);
#### Edit:- ####
I just figured out that for my solution to work in android versions greater than 2.3.x we need to add the following line in our TextView xml :-
android:singleLine="true"
Although its a deprecated attribute, but you have to add this, otherwise marquee or "..." wont work.
Hope this answers the question :)

Categories

Resources