1

In the application that I am developing, there exists a toggle button for a particular option. I have utilized a single XML file to cater for all display sizes. The tab displays this screen accurately. Nonetheless, on smaller screens, the toggle overlaps with the text view within the same horizontal line. It is impractical to reduce the text size of this button from the layout as it will also decrease the text size of the tab, and there are other buttons on this screen that display correctly. The text view in question is longer compared to the other text views.

Therefore, I have included code within my fragment to reduce the text sizes of this button exclusively on phones, by verifying the isSmallPortrait resource value. However, I am apprehensive that these modifications could affect the performance of the application. Is this the best approach to carry out such tasks? Are there any other best practices to accomplish this objective? (This fragment is 500 codes long)

textDisplayLastViewed = getView().findViewById(R.id.textDisplayLastViewed);

        if(!getResources().getBoolean(R.bool.isSmallPortrait)){
        }
        else{
            textDisplayLastViewed.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
        }

Ruslan
  • 1,039
  • 1
  • 9
  • 16
di1um
  • 11
  • 4

1 Answers1

0

What you are doing here shouldn't affect performance. But there are other possible issues to consider in my opinion. Reducing text size only for one text field is not a nice UX example, given there are other similar views on the screen with larger text size.

Probably there is a risk of unexpected text truncation on other similar TextViews. Even if it looks fine on you device, user can still increase relative text size from device settings. So 15sp value might appear larger than on your device. Probably it makes sense to decrease text size on all similar views (it is hard to tell without seeing UI).

Not sure if it works for your case, but you can provide different dimens.xml for different screen sizes. E.g. in addition to default main/res/values/dimens.xml you could add main/res/values-sw360dp/dimens.xml. And you just define text size resource <dimen name="text.small">15sp</dimen> in both dimens.xml. Let's say it is 15sp in first one, and 14sp on the other. The app will choose right text size resource depending on screen size. You can read more about this in official documentation https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes

This approach there you manually update text size in runtime is harder to maintain. It is easy to forget to update this piece of code is something changes in you UI.

Also please consider adding android:ellipsize parameter to your TextViews where needed. https://developer.android.com/reference/android/widget/TextView#attr_android:ellipsize

Ruslan
  • 1,039
  • 1
  • 9
  • 16
  • Yes, I have already added android:ellipsize parameter to the TextView. Actually, I couldn't wrap my head around using text sizes in dimens.xml (Although I have used dimensions for other properties such as app bar length etc.) I will use dimens.xml to store text sizes as well. As suggested here https://stackoverflow.com/questions/58084222/how-to-set-text-size-depending-on-the-screen-dimensions – di1um Apr 12 '23 at 09:07