I'm a novice programmer and I'm working on one of the my first projects on Android Studio for a school work.
I have to create an application that consists on a TextView where the user can write some text and on a button that permits to open a sort of menu where the user can choose the text's size. When the user select an option from this list, the written text should change its size.
The problem is that I don't have idea in which way I can change the size in the TextView when I select an option. Is there a specific function that permit to change the size of the written text?
You could use those
This one is for change font in SP size:
TextViewXXX.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
This one is for make text BOLD
TextViewXXX.setTypeface(null, Typeface.BOLD);
Yes, you can use TextView.setTextSize(float). But, TextViews must have scale-independent pixels to support multiple screens and font size configurations (sp). So, you can use TextView.setTextSize(int unit, float size) where the value for unit would be COMPLEX_UNIT_SP and size is the required size of the text.
Related
so I was developing an app with Android Studio and I added a 'textview' box in the screen. However, when I tested the app on my device, the text was too small to read. So I tried searching on the web about how to change the text size, but I could only find videos and posts about how to change the font of the android studio product itself. Please tell me how to change the text size in the textview box. I would appreciate any help.
You can add the following property in the text view's xml and start increasing the number till you reach the font-size you want
android:textSize="20sp"
The sp here inherits the font size the user sets in his/her android device so the same font size may vary on two identical devices
I've been developing an application similar to the Google Keep, and was wondering how actually they allocate the text size to a note (algo)?
Do they just allocate size depending upon the Note length?
Or they also check for Card Dimensions i-e card width etc?
Its a bit confusing, because sometimes notes have equal card size dimensions with font-difference, and sometimes notes have both different size dimensions and text size?
So basically, the Question is, What is the algorithm/method to calculate the text size for a note?
you have to use auto-fit TextView for this & add it in RecycleView or ListView(whatever you want) item layout. here is one of stack answer of AutoFit-TextView and source code from github android-autofittextview
apply max text size in TextView then it automatically adjust if there is more text
Do they just allocate size depending upon the Note length
No,
Or they also check for Card Dimensions i-e card width etc
No
just make you TexView size wrapcontent its allow you to add all text in textview like whatsup chat thread.
Edited
you have to manage like below way
*in Google keep text size depend of words. text length is 10 then maximum text size. text length is 30 then medium textsize and more then 50 then small text*
they are using two typeface one is for title and another is for text message
I am writing an app about eye test. It is necessary to set the standard text size. I used the following code but it showed what I did not expect.
Typeface type=Typeface.createFromAsset(getAssets(),"Optotypes.ttf");
textView2.setTypeface(type);
textView2.setTextSize(TypedValue.COMPLEX_UNIT_MM,25);
textView2.setText(randomLetter);
I expected the textview show a 2.5cm letter but it is not the exact length/height still.
This situation appear also on different device.
The next problem is that the size is different between the original font and ttf I added. (the original font didn't show the text with 2.5cm also.
Is my code wrong or anything else i missed ? Thanks guys . it is important to me.
I think you're missing how Android handles text sizes.
In Android, you should specify text size in SP units, so Android can scale it accordingly to the user's font size preferences. Never specify hardcoded pixels or centimeters.
Check this references for documentation on the subject:
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
http://developer.android.com/reference/android/widget/TextView.html#attr_android:textSize
What is the difference between "px", "dp", "dip" and "sp" on Android?
If you want to set the text size in SP programatically, you can do this
// same as android:textSize="15sp" in XML
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
-- EDIT
Keep in mind that by just setting a certain text size it doesn't mean that every letter will be of that size. Remember that there are multiple letters with multiple sizes. For instance, with a size of 20mm, this is what you get
Because Android needs to accommodate every possible character in a textview with the size you provided. That being said, textSize is not 100% accurate to what you provide to it.
If this is not enough for you, please provide more details of the problem you have at hands.
I have buttons with fixed size, but the text is changed time to time.
Sometimes the text is too long to fit in the button, and for these cases I want to use a smaller textsize.
How can I change the button textsize if text is too long?
(One solution could be to test how many characters that can be used with the normal textsize, and then change textsize if the length is larger than this baseline. But I was hoping for more dynamic approach.)
1)Measure the button.
2)Using the same font, use Paint.getTextBounds() to get the width.
3)Compare the size of the button to the width. You'll probably need to add some extra room on both sides for padding, but this is going to be a bit of an estimate anyway.
4)If the text was too big, reduce the size of the text (on the Paint object) and goto 2.
5)Now that you have a working size, call setTextSize on the button.
Note: if you're doing this for an AlertDialog, you need to do it after the button exists- I've had problems with step 1 depending on where I put this function, but its been so long I forgot the exact issue. I think I had to do it after calling show?
You can Extend Button Class to something similar to AutofitTextView
Putting text into an AlertDialog box as a CharSequence using setMessage() causes the text to display at the default system size.
However, if the text is a TextView object, then it is added using setView() and the resulting output is a tiny font.
I know that I can define a specific font size for a TextView object (that's the point of it after all), but how / where do I tell the project to use the same font size setting as is used by setMessage()? Since setMessage()'s resulting font size depends on the system settings that the user has set up on their device, and I'd like to be able to cater for visually impaired users without the need to resort to a font size menu within my app.
Alternatively, if anyone knows a way of pushing a SpannableString to an AlertDialog using setMessage(), that would be a suitable solution too...
refer to Typography
You can just set the font size to 18sp for Medium size (which is used by setMessage()), and about the user preference, sp itself will scale according to it.
Do this to get the default font size:
float size = new TextView(this).getTextSize();
then set the value to the TextView object you are using.