Set text size with dimens - Android studio - android-studio

I want to change text size by screen resolution to phones and i used with dimens like this:
I set by Screen Height and its works fine with phone like naxus 5 or pixel 3XL.
My question is if this is how I should define? What did I do well enough to fit any cell phone and tablet?

The files listed above will most certainly cover any android device or tablet. You can always add more configurations if you eventually find a device that isn't utilizing the correct dimens.xml that you desire. There are so many android devices of all shapes and sizes, it is hard to have a coverall approach by using xml files.
Another option would be to programmatically set the the font size based on a ratio of the screen dimensions which can be found below. Exactly how to best do that would depend on how your app is set up.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Using the height and width above you could change the font size. However, Where exactly to do that in your code depends entirely on how your app is set up.

Related

In android how to set font size according to device screen size

How to change the font size of text displayed at run time in the styles.xml in my application depending on the screen size of the device used.
I had the same problem. I wanted to get the same aspect text ratio on every screen. No dp nor sp worked for me. The only solution that works for me is to create a dummy textbox (not visible) at startup then get the textsize and multiply for some ratio (depending on what you actually want). This value should be used to set text size dynamically to your controls.
At least it works for me because the same aspect remains on every different screen.
you need to add in the text size via dimens.xml files depending on screen size.....take a look at this following Question on SO - Text size and different android screen sizes
You can go through this, I used the below method to scale texts on different screen resolutions and sizes.
To define a range of text sizes and a dimension programmatically through the support library, call the TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(TextView textview, int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) method. Provide the text view on which it needs to be applied,the maximum value, the minimum value, the granularity value, and any TypedValue dimension unit (like TypedValue.COMPLEX_UNIT_SP).

Accommodating Smaller Screen Sizes

I have an application that looks fantastic on a Galaxy 4 which is 360dp wide. When I run the application on a Gingerbread device that is 320dp wide, the text overlaps adjacent buttons.
I tried creating a separate layout for the Gingerbread devices entitled layout-sw320dp and another for the Galaxy entitled layout-sw360dp. Unfortunately, both devices defaulted to the sw320dp layout. I also tried layout-normal, but had the same experience.
Let me know if you have any suggestions for smaller android screens when it comes to fonts.
To solve the problem, just set up a font style for each font size. Use a dimension to set the font size. Then set multiple dimension files by including these files in different directories based on the font size. For instance values-sw300dp would be for files greater than 300dp in width. Your standard values file would apply for other layouts.
Hope this helps.

What Android layout file does the HTC Evo 3d use?

I think the phone uses the layout-hdpi file but this doesn't seem right, mostly because its dimensions(540 x 960) are not what I'm used to seeing(480 x 800) for layout-hdpi phones. So what is the actually layout file used? Also, if it is layout-hdpi phone how can I stretch an icon that has a width of 480 to 540? Would I programmatically have to test the phone's dimensions and then expand the drawable-hdpi file to fit the various device dimensions? I looked at this link but they never specified what layout file was used.
Yes, HTC Evo 3d use drawable-hdpi images. You should have to put the Layout file also. Put the same file into layout-hdpi.
Now create the Emulator of the Resolution 540 X 960. And test the App in it. There should be minor difference between normal hdpi and HTC Evo Screen resolution. Manualy test it.
I am getting same trouble before some days. What i have done is: I am going to check the resolution of the devide screen size at run time. And if the Resolution is compare to that HTC Evo 3d then it will set the height and Width of the All Images at run time that fit to the HTC Screen Resolution.
That was the Best Solution i found till today to Make app for HTC Evo 3d.
For Checking the Resolution at run-time you can use this:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
int width = display.getWidth();
int height = display.getHeight();
if((height==960) && (width == 540)){
// Do change the Image Height or Width or position
}
Enjoy. :)

How do I display same "proportional" text size on different resolutions?

I draw some text on screen using ID3DXFont::DrawText. This text should be displayed the same regardless of screen resolution.
For example, if screen resolution is low, text wrapped and when it is higher text is not wrapped. How can I avoid such situation? I want text size to be connected to screen resolution so if resolution is lower I want the text to be relatively smaller so that no wrapping happens. Is there any way?
Thanks in advance
Below is a logical solution. It doesn't have any of the code or procedures needed to make work in direct X but having done something similar outside of DirectX I wanted to share the logic
Working on whatever default screen resolution you want set the font to be the size needed.
Find the percentage of the screen height the font size you chose takes up.
Then having stored that value when you go to render again, in the final version of the program, calculate the font size based on the screen size and the percentage
What I was working in had functions like GetTextHeight and properties on the font size to allow you to set the height (which in turn set the font size appropriately). So if can find anything similar to this in DirectX than this could be a route for you to take.

More iPhone 4 resolution scaling fun

So I have two images set to fill the screen - one at 320x480 (iPhone 3) and the other at 640x960 (iPhone 4).
img.png (320x480)
img#2x.png (640x960)
In Interface Builder, I have img.png set to fill the view (it shouldn't be filling the iPhone 4 view, though, right?), and when I build and run, it is notably too small.
So, when I do the opposite, setting img#2x.png as the img, a giant blue question mark fills the view. The project still builds and the image fills the screen, but is very pixelated as if it has been rescaled. It shouldn't have been rescaled, though, as it is the higher resolution.
Now using...
img.png (640x960)
img#2x.png (320x480)
I've also tried switching which file takes the #2x suffix, but that has not helped either. With the smaller file taking the #2x suffix, and setting the image view to img.png, it shows up way larger than the view canvas, but way too small when on the phone. With the smaller file still having the #2x suffix, I tried switching the view to the new img#2x.png and once again got the oversized, pixelated question mark in IB and a low res full-screen image when deployed to the phone.
I also made sure that my view size was set to 640x960 in the size inspector. What else should I be doing?
Don't use #2x when specifying the image name. Just use the base name of the image (so in your case, just img). iOS will do the magic behind the scenes to use the 2x version if it's on a retina display and the regular version otherwise (including when you use a xib to lay out your interface).
The displayed dimensions of the image will always be the dimensions of the non-2x image. The retina display just displays 2 pixels for every 1 which is why the scale is 2.0 and you have to double the dimensions of the original image. So your view size in IB should be 320x480.
Edit
If IB is being finicky, especially in Xcode 4, just try deleting the xib and starting over. Sometimes it gets a bit corrupted and doesn't know what to do with itself. I've had to do this on one or two occasions, and it seems that it worked for the OP as well.

Resources