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.
Related
I set the button's height via XML. Suppose it to be X
<Button
android:layout_height="wrap_contnet"
android:textSize = " <X> dp"
....
....
/>
If I set X to greater than 15 dp, the button's height grows to fit the text. But when I set it to something lower like 2dp, the button's height doesn't change and I can see a thick border around it as shown below:-
As shown above, text size is so small but still height doesn't decrease!
How can I force it to be of appropriate height?
Thanx in advance!
Font size does not depend on button height. If you set it too small to fit the text, you will get the result you observe
EDIT
wrap_content would usually do the trick, however Button class sets some layout parameters like margin, background so it takes more space. You may get rid of Button and use i.e. TextView class, styled by hand to match your design. onClickListener will work perfectly fine with it and you will get result you want and full control over your button
EDIT 2
As this is bit related - there's AutoFitTextView widget on Github that deals with automatic text size scalling: https://github.com/grantland/android-autofittextview
Just Set android:minHeight="xxdp"
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.
I was wondering about what is wrong with my layout.
Here is a screenshot.
Problem being that the text is being cut off in edit text boxes.
I suppose there is something wrong with the text's XY co-ordinates at the time of typing in those edit text boxes; the cursor blinks fine to me.
Here is the pastebin of my XML layout (not putting it here because its quite lengthy).
Any advice will be appreciated. Thanks
The text size of the text inside edittext is large for hardcoded 30dp height of edittext. You can either set edittext height to wrapcontent or reduce the textsize of edittext (10-14sp)so that it fits inside.
It is advised by android to set minimum height of clickable area to at least 48dp.
http://developer.android.com/design/style/metrics-grids.html
I have a layout and a button in it. In onCreate(), I calculate the size of layout and size of button dynamically. Now based on the size (width) of the button I want to set its TextSize. Initially the button textsize is set to 11, but if the button size increases, the text looks very small. So I need to increase the size of text also.
Is their any method/way where I can calcualte the size of text and check with that of the button size. Any ideas for appropriate method or logic to be applied.
NOTE : I want to change the textSize of button based on size of the button and not change buton size based on textSize.
Any help is highly appreciated.
If you get the paint for the button with getPaint() you can use measureText() with your text to get the width the text needs for a given setTextSize() setting. You may want to experiment with some spair space.
There is code using to concept for a different case in this question
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