How to Set Text Size of Textview Dynamically for Different Screens

How to set text size of textview dynamically for different screens

You should use the resource folders such as

values-ldpi
values-mdpi
values-hdpi

And write the text size in 'dimensions.xml' file for each range.

And in the java code you can set the text size with

textView.setTextSize(getResources().getDimension(R.dimen.textsize));

Sample dimensions.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">15sp</dimen>
</resources>

TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for different screens

The difference here is that in the setTextSize(int size) method, the unit type by default is "sp" or "scaled pixels". This value will be a different pixel dimension for each screen density (ldpi, mdpi, hdpi).

getTextSize(), on the other hand, returns the actual pixel dimensions of the text.

You can use setTextSize(int unit, float size) to specify a unit type. The constant values for this can be found in the TypedValue class, but some of them are:

TypedValue.COMPLEX_UNIT_PX   //Pixels

TypedValue.COMPLEX_UNIT_SP //Scaled Pixels

TypedValue.COMPLEX_UNIT_DIP //Device Independent Pixels

How to auto size textview dynamically according to the length of the text in android?

There's now an official solution to this problem. Autosizing TextViews introduced with Android O are available in the Support Library 26 and is backwards compatible all the way down to Android 4.0.

https://developer.android.com/preview/features/autosizing-textview.html

How to dynamically adjust text size and margins for different screen sizes?

You can different size for your tex using values-[smallestWidth].

SW : The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least dps of width available for it UI.

Example:
Let's confider 3 type different layout , small , medium , large

values       : <dimen name="text_size">10sp</dimen>
values-sw375 : <dimen name="text_size">11sp</dimen>
values-sw600 : <dimen name="text_size">18sp</dimen>

Sample Image

How to manage the position of TextViews dynamically when screen size changes

for your requirement you don't have to use 2 text views for this you can place a spannable string builder on just 1 text and put clickable as well as color property and you are done.

Code:

 TextView textView = findViewById(R.id.tvSample);
SpannableStringBuilder stringBuilder =new SpannableStringBuilder(textView.getText());
stringBuilder.setSpan(new ForegroundColorSpan(Color.BLUE),textView.getText().length()-20,textView.getText().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(new ClickableSpan() {
@Override
public void onClick(final View view) {
Toast.makeText(MainActivity.this,"Click",Toast.LENGTH_LONG).show();
}
},textView.getText().length()-20,textView.getText().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);

Here is example of putting different spans on text view

This is how to set two spans on single text view

How to dynamically adapt textview size in constraint layout to different screen sizes?

  1. Remove that LinearLayout;
  2. Make the ConstraintLayout height to match_parent;
  3. You want those controll buttons to be right at the bottom, so set bottom constraint to bottom of a parent;
  4. ProgressBar bottom to top of controll buttons;
  5. Time textviews bottom to top of controll buttons;
  6. Move like that up, so you will leave only song cover to fill remaining height

TextView - setting the text size programmatically doesn't seem to work

Text size 2 will be practically invisible. Try it with 14 at least. BTW, using xml has a lot of advantages and will make your life easier once you need to do anything more complex than 'Hello World'.



Related Topics



Leave a reply



Submit