1

I am new to working with multiple screens and need help with the following:

I have 2 devices:

  1. Samsung S4

    • display class: normal
    • smallest width: 360dp
    • resolution: 1080 * 1920
    • Density class: XXHDPI
    • Density: 442dpi
  2. Samsung Tablet:

    • display class: large
    • smallest width: 600dp
    • resolution: 600 * 1024
    • Density class: MDPI
    • Density: 168dpi

My problem is that the values for both devices are getting pulled from the same dimens file.

  • If I have small text, it looks OK on phone, but too small on tablet
  • If I have large text, it looks OK on tablet, but too small on phone

Can you please tell me what the name of the values directory should be for each?

I have tried:

values-sw360dp-xxhdpi values-sw600dp-mdpi

However, this has not made a difference.

I have a method:

public void setTextSize(Context context, TextView view, int textSizeInSp)
{
  float spToPixel= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSizeInSp, context.getResources().getDisplayMetrics());

  view.setTextSize(convertSpToPixels(spToPixel, getApplicationContext()));
}

I have 2 dimens file entries:

values-sw360dp-xxhdpi\dimens

<dimen name="font_english_large">20sp</dimen>

values-sw600dp-mdpi\dimens

<dimen name="font_english_large">32sp</dimen>

I programmatically attempt to change the text size by calling:

setTextSize(this, textView, (int)getResources().getDimension(R.dimen.font_english_large);

Thank you.

Jehanzeib
  • 25
  • 6
  • 1
    Welcome to stackoverflow. Please, could you provide some code of your specific problem. That prove how far did you try and it will help other members to understand your problem better, at the time, you will give them a context of your issue. Please, check these links: https://stackoverflow.com/help/mcve and https://stackoverflow.com/help/how-to-ask – Kenzo_Gilead Sep 03 '17 at 10:58
  • Thanks Elias. I have updated my question to include an excerpt of my code. – Jehanzeib Sep 03 '17 at 17:34

1 Answers1

1

you will need only values and values-large, then add your font sizes in the corresponding files using sp metrics. For example in values/dimens.xml add

<dimen name="randrom_text_font_size">18sp</dimen>

and in values-sw600dp/dimens.xml add something like

<dimen name="randrom_text_font_size">28sp</dimen>

Then in your java code:

textView.setTextSize(0,getResources().getDimension(R.dimen.randrom_text_font_size));

Or in XML

 android:textSize="@dimen/randrom_text_font_size"
Antonis Lat
  • 545
  • 4
  • 9
  • Hello Antonis. Thank you for your reply. I have tried the following: 1. I have renamed values-sw600dp-mdpi to values-sw600dp. 2. I have removed the values-sw360dp-xxhdpi resource directory. But it has not made a difference. I have updated my original question to include some code. – Jehanzeib Sep 03 '17 at 17:34
  • @Jehanzeib You can use this line `textView.setTextSize(12,getResources().getDimension(R.dimen.randrom_text_font_size));` and adjust the 12 in it according to your need. – Lalit Fauzdar Sep 03 '17 at 18:29
  • textView.setTextSize(0,getResources().getDimension(R.dimen.randrom_text_font_size)); always use 0 here if you have decrare your font size in sp in your dimens file – Antonis Lat Sep 03 '17 at 18:39
  • if the above solution does not work for you, create values-large/dimens.xml and put tablet's font size there with the same format and the java code i proposed above. – Antonis Lat Sep 03 '17 at 18:42
  • @AntonisLat Thank you so much! That now works. Just one question; why do I not need to define a separate values directory for the mobile? How does it know to pick up the dimens from the default values directory? – Jehanzeib Sep 03 '17 at 19:42
  • https://developer.android.com/guide/practices/screens_support.html. Check here of how to support multiple screens. Please mark my answer as accepted if it worked for you – Antonis Lat Sep 04 '17 at 07:39