Reputation: 3556
So I'm totally lost now I have problems with Text size ruing my UI when user sets his font size through Accessibility -> Font size to Huge so for quick fix I decided to change all text sizes to dp instead of sp that text would always be same size but on some places textviews even when textsize is set to dp resizes any idea why??
Here is button with text size set to dp which maintains font size even after changing through accessibility
<Button
android:id="@+id/video_button"
android:textSize="16dp"
android:layout_marginTop="10dp"
android:layout_height="30dp"
android:layout_width="180dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/webshop_button"
android:text="@string/button_3dvideo_text"/>
And here is textView which ignores textSize even after setting it to dp:
<TextView
android:id="@+id/about"
android:gravity="center"
android:textSize="16dp"
android:layout_above="@+id/footer"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color_white"
android:text="@string/about"/>
Upvotes: 2
Views: 2599
Reputation: 2170
I have a solution for this which will make the font size non changeable in your app :
public void adjustFontScale(Configuration configuration) {
configuration.fontScale = (float) 1;
DisplayMetrics metrics = getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
Call this method in onCreate of your every Activity or you can make a Base Activity:
adjustFontScale(getResources().getConfiguration());
You can change the fontScale according to your needs. Hope this helps.
Upvotes: 5
Reputation: 238
May be try to use this Not sure whether its going to help you.
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
float dp = 20f;
float fpixels = metrics.density * dp;
int pixels = (int) (fpixels + 0.5f);
Edit_click.setTextSize(pixels);
Upvotes: 2