How to reduce the size of a text view dynamically - android

I am using a textview to display a text which I am fetching dynamically. But the problem is that the size of the fetched data may vary from 10 to 30 characters. But I am using a textview with a fixed width. I can't do anything on that. Is there any way to reduce the textsize or something dynamically in order to fit it to the fixed size textview?

Check this answer, How to dynamically set textview height android
It gives you idea of how to set height of textview as per number of lines of text.

TextViwe txt = (TextView)findViewById(R.id.txt);
txt.setTextSize(Float.parseFloat(textSize));

I think reducing text size according to length of the data is a bad practice. So one solution is you can move text into next line by following code.
textView.setSingleLine(false);
or
android:singleLine="false"
and set textView height as WRAP_CONTENT

Related

Is there a default XML line to resize textSize if the current textSize does not fit the borders of the TextView?

I'm working on a project that displays sentences, but sometimes the sentences are too long to fit at a given textSize. The textSize is at a size that I like, not too small for short sentences, but when the sentences pass the borders of the TextView the text disappears. I know I could make it end in '...' but then the sentence is still not read.
So I am looking for a way to decrease textSize ONLY when the text exceeds the TextView borders.
You could use AutoResizeTextView, which changes text size depending on the textview size.
https://stackoverflow.com/a/5535672/5050338

How to reserve 'n' characters for TextView content to maintain the related view alignment

Its clear from the above shot, what i mean, i'm certain about the number of subscribers will be around 10 digit max, and i would like to reserve a space for 10 characters so that it doesn't alter the other ImageView [birdy]'s alignment. I'm using relative layout for this this view.
Try setting the attribute android:minEms
Setting minEms to an integer value say 5, on an EditText or TextView should ensure it's at least 5 characters wide.
Try changing layout_width property from "wrap_content" to something fixed. I would use something like 100dp. I'm not sure if it's a good approach, though.
If you truly want to have the width of a TextView with 10 characters then XXXdp isn't a option. What you could do is measure what a textview with 10 characters width is and use that for (all( the TextViews in you listitem layout.

Android Auto Font Resizer

I am working on Android textview,case is i have a specific height of textview,i want to adjust the text in the given textview height by adjusting the font size.If a text is of 10 charachers it will covers the whole height and if text is off 400 characters it will decreases the font to adjust in height of textview. I have tried auto text resize class but it does not getting the desire result.
You are looking for FontFit TextView. Check out here.
You just have to use it instaed of normal TextView like this..
TextView tv = new FontFitTextView(context);
Hope this helps
You can also write your own logic for that like
1- : Count no or character in side text watcher
2- : if no of character more then 10 then reduce the size of test using textsize method.
3- : the same logic if character more then 10 then reduce the size of test using textsize method.
or you can also put it on loop then i think you can achieve it also.

Resizing the textview size based on the screen width for all devices

I have problem in resizing the text view .
I have a Layout like this with below components
ImageView| TextView | ImageView
placed in relativelayout(horizontal) and each one width is wrap content and height is fill parent.
PROBLEM
I have a text like this "My name","My name is xyzmnop" now, i have fixed the TextView size to 15sp, so what is happening is in larger devices since Textviews width increases the text "My name is xyzmnop" will fit but in smaller devices since the width is small it will spill out to second line. But i want it to be in same line, as I have the flexibility to decrease the size of the text if the text is lengthy.
please help me with this problem.
i have seen this
Android TextView doesn't resize after changing text size
Android EditText Resize Programatically
Resizing TextView does not shrink its height on Android 3.1
EDIT:
I have also added setSingleLine = "true"; in my xml
Say, tv is your TextView.
Just call tv.setMaxLines(1);
TextView has the setMaxLines (int maxlines) method:
Makes the TextView at most this many lines tall. Setting this value overrides any other (maximum) height setting.
I suppose this might help you if you set the maximum number of lines to 1.
Note: there is also the android:maxlines attribute if you need to set it in your XML layout.
i have solved it using ViewTreeObserver.

Dynamically display a TextView

Is it possible to display a TextView dynamically so that the height of the text can be retrieved in the middle of the Application but not at the end that is done using onGlobalLayout() or onSizeChanged().
My requirement is to display the text in the TextView to a certain height limit. I need to check the height of the text in the TextView so that I can achieve my requirement but using any of the onGlobalLayout() or onSizeChanged(), I am getting the height at the end which is of no use to me because I can't check at the end.
Is there anyway to achieve my requirement?
I think this will help you.
Dynamic Views in Layout

Categories

Resources