This problem is more complicated than the title.
I have a java class which maps on the values from the server. Now I am setting the textsize programmatically, as follows:
headerButton.setTextSize(Application.getAppResources().getDimensionPixelSize(R.dimen.manage_markets_text));
and setting manage_markets_text in dimensions as 15sp for layout, for values-xlarge: 20sp and for values-large dimension 18sp. Now, For the xlarge tablet it is getting 20sp as desired, However for htcone phone it is mapping on some large font size which I don't even know where it gets from (appears to be like 40-50sp). Now I changed the code, to accept getDimension instead of Pizelsize and I still have the same issue. if I set settextsize(r.dimen.manage_market_text) ,it displays blank page. How do I go about fixing this issue? Any clue?
I would think that you would want to set this in your XML.
<Button
...
android:textSize="#dimen/manage_market_text"
...
/>
Also, using values-xlarge is a bit old fashioned. According to
http://developer.android.com/guide/practices/screens_support.html
You should format your files such more like values-sw600dp.
To answer your original question simply write
Resources res = this.getResources();
headerButton.setTextSize(res.getDimension(R.dimen.manage_market_text))
That should do the trick
Related
None of the previously asked questions and solutions worked for me.
For the methods setTextSize(), when I give hard coded value as below:
textView.setTextSize(16)
the text appears to be of same size on emulator and device.
However, when I give the size in dimensions file:
<resources>
<dimen name="default_text_size">5sp</dimen>
</resources>
and I have tried 2 approaches programatically for setting this size:
defaultTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getContext().getResources().getDimension(R.dimen.default_text_size));
and
defaultTextView.setTextSize(getContext().getResources().getDimension(R.dimen.default_text_size));
Both of them show different size on real device and on emulator.
Please see attached screenshots.
Any idea how to fix this issue? I know that it will change based upon the screen resolution, but this is drastic change. The whole text has shrunk in the upper half of the ListView row in case of emulator. Which is not what I want.
Scale-independent Pixels (sp) - this is like the dp unit, but it is
also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted for
both the screen density and user's preference.
When you set the size unit of text (in TextView) as sp it becomes also dependent on the user/device font size preference. For this reason, on different devices your text may appear different.
If you want your text to appear everywhere same (regardless of the user's font size preference), use dp instead:
<resources>
<dimen name="default_text_size">5dp</dimen>
</resources>
My developed Android app works well on my phone, and I have used linear layout, and set the pixel as dp.
However, there is a setting in my phone, which can change the screen display size, I set it to large, and some of my layout is outside of the screen.
I tried to look up at the official document, but I found that it said usually setting change won't affect.
How can I resolve it?
You can create dimension xml file for different screen density pixels. Refer this link for more details
How to define dimens.xml for every different screen size in android?
I know this has been answered in other questions, but nothing is working for me. I have a text view where I want the text to appear the same size in a variety of density/screen size combinations. I have tried specifying
android:textSize="30dp"
and
android:textSize="30sp"
and have also used
TextView text = (TextView) findViewById(R.id.text);
text.setTextSize(30 * (getResources().getDisplayMetrics().density));
30 is my font size *****
the text doesn't scale properly. It becomes way too small at lower densities and as screen size increases. Are there additional techniques I can use to scale my font?
create layout-large, layout-small, layout-xlarge etc.. versions of your layout xml file.
You can set the size differently in each so that the font will look right across a variety of screen sizes.
After years of constantly having everything slightly off on different devices, I recently discovered that the inches and cm measurements for dimensions are literally that.
So even on devices where 160dp isn't quite an inch, specifying 1inch will give exactly that, on everything, even if they have a custom scaled ROM.
I now specify anything I want very tight control over with these.
If you just want to have separate font sizes for separate screens, you can create dimens in your value.xml for sw600dp and sw720dp folder too and put different values there. You don't need to create separate xml layout for each, if you just need to change font sizes.
In my layout XML files, I reference a lot of parameters through a separate files called dimens.xml.
For example, dimens.xml contains parameters like these:
<dimen name="textSize_normal">20dp</dimen>
<dimen name="buttonTextSize_normal">15dp</dimen>
<dimen name="editTextSize_normal">17dp</dimen>
<dimen name="buttonHeight_normal">37dp</dimen>
<dimen name="margin_normal">5dp</dimen>
And in my main.xml, for example, I would set the text size for a TextView by doing something like this:
android:textSize="#dimen/editTextSize_normal"
It works great.
Now, my questions is, is it possible to set the values for the dimen variables in my dimen.xml file programmatically from my main activity? What I am trying to do is fetch the screen size, and set, for example, the textSize based on a fraction of the height of the screen so that it is easily adaptable to any screen size. I have all that figured out, I just need your help to figure out how to set the dimen variables in my code.
Now, my questions is, is it possible to set the values for the dimen variables in my dimen.xml file programmatically from my main activity?
No.
What I am trying to do is fetch the screen size, and set, for example, the textSize based on a fraction of the height of the screen so that it is easily adaptable to any screen size.
Ignoring that this is an odd UI approach, the way to do that is to delete your android:textSize attributes and to change the text size at runtime using setTextSize() in Java.
I have all that figured out, I just need your help to figure out how to set the dimen variables in my code.
You don't "set the dimen variables". You apply your calculations to the widgets, via setters like setTextSize().
No, you can't edit the Resources files on runtime, they are already compiled and generated.
You should use density independent pixels in all your resources, so that dimensions can adapt to screen size. You don't need to calculate that values at runtime. If you want to have a different layout for different screen sizes, then consider using multiple resource files.
Read this guide.
i guess what you mean is to change the size depending on the device's screen size..
which can be made by creating multiple dimens.xml files in folders like values-hdpi, values-xhdpi, values-xxhdpi, values-mdpi,.. and then on runtime the compiler will choose the dimen depending on the dpi
i actually didn't try this before but i read it
here.. take a look at this:
http://developer.android.com/guide/practices/screens_support.html#qualifiers
With all effort,I finally reached to the end of my first app in android. And thanks to all. But after coming to end, I realized one thing that my app text size is common in all tablet sizes. So I will re frame my question.
Problem: I used my own custom text size in entire app. and some what satisfied with 7 inch tablet. But when I am looking the same thing in 8.9 inches and 10.1 inch tablet its containing lots of white spaces and text size are also relatively small. So is there some way that I can change the text size according to my tablet size??? It may look novice question but I am struggling with this because the same app which look wonderful in 7 inches, is loosing its essence in 8.9 and 10.1 inches. Or there is something else which I could do with my app for the same.
Probable Solution:- As my topic indicates is there some way to change the text size as tablet size changes. Either dynamically or any other way.
EDITED:: As per Cheeta answer, approach is correct but I can't do it for each layout buttons and textfields and other field. There are hundreds of field like this in single app. Can I do it in some one place and call it required attribute. Is custom tag is approach which I am looking for.Is it possible????
Any help will be appreciated.
Thanks in advance.
NOTE I have not reached to a answer but cheetah answer is somewhat leading to correct way. I am marking his answer as correct although there are few alignment issue with his answer. Any other answer is always welcome. Thanks
After checking some resources I finally came up with the following solution:
Within the layout-xml I define the size of the button-text with
a dimension declaration:
android:textSize="#dimen/campaign_textfontsize"
I created a dimens.xml in the "/values"-subdirectory with the corresponding value
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="campaign_textfontsize">18dp</dimen>
</resources>
then I created a second values-folder "/values-sw720dp" and copied the old dimens.xml into it. In this dimens.xml I adapted the fontsize value:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="campaign_textfontsize">24dp</dimen>
</resources>
Based on the current screensize android selects the proper dimens-value.
So without any code, font is adapted to the display- (and display related button-) size.
Hope this helps...
I was Having Same Problem but what i realized sp/db are not efficient for dealing with this kind of problem, I handled my alignment and font size related stuff programtically.
here are the steps, how i handled this problem.
Calculate Screen Width and Height of Your device
Use Set TextView.setTextSize(unit, size), where the unit parameter is TypedValue.COMPLEX_UNIT_PX and the size parameter is percent of the total width of your screen. Normally, for me, .5% of total width for font is suitable. You can experiment by trying other total width percentages, such as 0.4% or 0.3%.
This way your font size will be always suitable for each and every text/screen size.
no need to change textsize dynamically use sp for textsize it will automatically arrange the text size depending on the screen resolutions..like in phone.
android:textSize="10sp"
It's quite an old question,
but another modern solution can be really handy.
With the Support library, 26+ new text auto sizing is available.
In simple words, you define TextView size and text size gets automatically increased/decreased to match it's bounds:
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp" />
More on Autosizing TextViews
Add here the ability to set TextView size in a percentage of the screen with the ConstraintLayout, and you can create layouts which look almost identically on any device.