Reputation: 206
I had already searching how to set textview size in different screen size from Google a long time already and I still don't get the solution.
I already try to set it as 'dp' or 'sp', but the size still different in different screen size.
I also try to use the code below to change the size but the results still wrong.
final float scale = getResources().getDisplayMetrics().density;
textview = (int) (mysize * scale + 0.5f);
Is that any other way I can use to make sure the ratio is correct in any screen size?
Thank you.
Upvotes: 2
Views: 3962
Reputation: 506
I found the same problem and found a bit tricky solution. Actually we can find this ratio within Android itself. Most probably you know that we can define small, medium and large textviews. Different devices with different screen size have different values for those textviews.
All you have to do is that create temporary textview and get its size. Now you have the ratio that you are looking for. Just try it. It really works.
Here is an example code
TextView tempSmallText = new TextView(getApplicationContext());
tempSmallText.setTextAppearance(getApplicationContext(),android.R.style.TextAppearance_Small);
int smallSize=tempSmallText.getTextSize(); // return text size in pixels
//now you have the ratio for small text views, just like you can get large if you want
int mySmallTextSize = smallSize*2; // if your small text size is wanted to be twice as the standard size
//apply to your textview
myTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX , mySmallTextSize ); // don't forget to put TypedValue.COMPLEX_UNIT_PX. don't just use myTextView.setTextSize(mySmallTextSize );
Hope this helps
Upvotes: 0
Reputation: 75798
I already try to set it as 'dp' or 'sp'. You must use sp
.
You can use this logic .
DisplayMetrics: A structure describing general information about a display, such as its size, density, and font scaling.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
TextViewObj.setTextSize(DeviceTotalWidth/8); // As per your Requirement
You can check How to change TextView font size in android
Upvotes: 2
Reputation: 3070
You can set the textAppearance of TextView to Large, Medium or Small. These styles are availble with the appcompat lib,
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="Grass cutting"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/red"
android:textStyle="bold" />
Size of text will be set according to device.
Upvotes: 0
Reputation: 3851
you'd better use weights in maintaining the ratio. it working without regarding screen sizes and orientations. for instance see the below layout xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2"
android:background="@color/white"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2"
android:background="@color/white"/>
</LinearLayout>
</LinearLayout>
the layout of this code is as below.
in there the ratio in text views and linear layouts are the same in each and every one of screen sizes and in both orientations. you have a very good example on this in android developer site too. guess this is the thing you ask of.
Upvotes: 0