Reputation: 6604
I had to work on an existing Android application, where all the texts are mentioned in SP unit in layout XML files.
However, the text size of the application is not adjusted along with the system font. ie, if i change the system font size from Settings -> Accessibility -> Font Size
, the application's font size does not change at all.
PFB an excerpt from my layout file, where the TextView's
textSize
is mentioned in SP.
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/find_an_noffice"
android:textSize="12sp"/>
</LinearLayout>
I do not think the issue is related to the layout structure and hence i have used the same layout file in a sample application. This sample application is working as expected. ie, the app's font size is adjusted with the system font.
What can be the possibilities by which my original application was not behaving as the sample app?
Any help would be appreciated.
Upvotes: 4
Views: 1657
Reputation: 6604
I have found the root cause of the issue. My app was creating a new Configuration
which was causing the issue.
The code that causes this issue:
Locale locale = new Locale(isSpanish() ? "es" : "en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
If I change the line Configuration config = new Configuration();
to Configuration config = context.getResources().getConfiguration();
the application font will scale according to the system font size changes.
Hope this may help someone.
Upvotes: 5