Why android gives warning on using size smaller than 12sp? - android

Well, I am developing app for 7 inch tablet, more specially for nexus 7, and in the XML layout file, i get warning
Avoid using sizes smaller than 12sp: 11sp
if i set the size of any textField to less than 12sp ?
I am adding screen shots for more clarity of the problem

For the default font scaling, 1sp = 1dip = 1/160". A height of 11sp is about 1/15th of an inch, which is pretty tiny.
This is a Lint error. You can override it -- press <Ctrl>-<1>, and the quick-fix list menu should give you the ability to suppress the message.
But, if you try 12sp, you will probably see that it too is very tiny, and that you want a larger font anyway.

You can use tools:ignore="SmallSp" to ignore that warning
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="NileshRathod"
android:textSize="8sp"
tools:ignore="SmallSp"/>

Related

Android how can I reduce the text size based on screen size (and NOT density)

I've got a simple view with some text on it, and all I want to do is come up with a good way of adjusting the text size and padding for smaller devices, my issue though is that I don't want to consider density, just the screen size itself. The idea is to make the apps look the same between a small and large device (including padding etc.)
I've seen a lot of posts about creating value resource directories with qualifiers but this hasn't worked, as any small device with a high DP gets the padding and text size of a larger device.
For a simple example, I want the text size to be 17sp on a Pixel 3 XL and be 14sp on a Nexus 5, but there just doesn't seem like a way to do this because they have similar pixel densities even though they are vastly different in actual screen size. Am I missing something??
only one way to set dynamical change text size without density use android default font size like this
style="#android:style/TextAppearance.DeviceDefault.Small"
style="#android:style/TextAppearance.DeviceDefault.Medium"
style="#android:style/TextAppearance.DeviceDefault.Large"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#android:style/TextAppearance.DeviceDefault.SearchResult.Title"
android:text="Hello"/>

Layout rescaling with different devices

Sorry about this post which seems easy, but I don’t understand my problem.
I am developing an interface with android studio in xml. I use some PictureButton but I want them to have the same presentation with different devices.
I have put different sizes in folders (hdpi, xhdpi, xxhdpi …) But when I use a device like the nexus 6P I have got this : (Picture 1 - Nexus 6P) and when I use the nexus 10 I’ve got that (Picture 2 - Nexus 10P).
The problem is, these devices are in xxhdpi but they haven’t got the same resolution.
For each button I use this xml code :
<ImageButton android:id="#+id/Collecte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="50dip"
android:layout_marginLeft="25dip"
android:background="#android:color/transparent"
android:src= "#drawable/nouvellecampagne" />
I don’t understand why they are not rescaling.
Cordially
You should use layout_width="match_parent" and add adjustViewBounds="true" in imageview and let the height to be wrap_content.
The problem is that you giving hardcore dimen value in width and height in this case image take from hdpi xhdpi anything else but you need to define each and every dimen value in dimen value folder value 21, value small ,value large once you setup i think you can see your best result
and visit:-How to define dimens.xml for every different screen size in android?

How to change Text Size for differrent screen size?

I am making Game with android studio everything is completed but only one issue i have. Text size's not changed. How to make it for auto fit on any screen like phones and tablets.
Try this Demo Git Project Android-autofittextview.It may help you.In your layout add like this to mention size android:textSize="#dimen/_15sdp".
you can use this library
to auto fit your text depend on TextView width.
also you need to set android:textSize in dimens.xml like this:
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/textsize"/>
for have different size for different devices just define dimens.xml for different screen size (for example: Value-large)

making fonts get larger on larger screens

I have searched Stackoverflow and google and what I find is answers for people who want fonts to remain the same size but I want them to get bigger when the screen gets bigger.
Specifically, I want very large fonts in my application. On my phone, they are 1/2 inch high - perfect. My problem is that on my 7 inch tablet they are also 1/2 inch high and I want them to scale up and be about 1 inch high. I have tried sp and dp modes and both just keep the fonts the same physical height, which is not what I want. I see there is something new for tablets with 3.2 and higher but I want to support devices from 2.3 and higher. I have seen complex code that says it auto scales fonts to fit the text in a width but that is not what I need. I want the fonts to be the equivalent of say 100sp on a phone and 200sp on the tablet. I have no graphics and simple relative layouts with very few elements on the screen. I just need to make a couple of the textsizes larger for large screens. I would think it would be simple but I can't find it.
Here is a typical text view entry
<TextView
android:id="#+id/textDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/labelDistance"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="100sp" />
Is there a relatively simple straight forward way to do this?
'dp' or 'dip' dimensions - specially made for been the same on different screens. So, there is not any special dimension for you task.
For implementing different text sizes you have to create several styles, and put them in folders values-xlarge, values-large, values-normal and values-small.
One style will looks like this:
<style name="BigTextStyle">
<item name="android:textSize">50dp</item> <!-- different values in different folders -->
</style>
And in your text view just provide style reference:
<TextView
style="#style/BigTextStyle"
...
/>
I went with the method Here: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension and added one line to the dimens.xls files:
<resources>
<dimen name="padding_small">8dp</dimen>
<dimen name="padding_medium">16dp</dimen>
<dimen name="padding_large">16dp</dimen>
<dimen name="font_size">200sp</dimen>
The above is the file in views-large. In the views file I have 100sp instead of 200sp.
Now the font is large on my tablet. According to the documentation, this may be a problem with some of the new phones, the 5 inch ones and that is why there is some way to deal with this in the newer versions of Android but as I want this to work on older phones and 7 inch tablets, this will solve my problem. The solution above, which led me to this, would also work I am sure, I just went with this as it seemed simpler and was pretty well documented by Google.
In my layout file, I just changed the specific callout of font size to this:
<TextView
android:id="#+id/textDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/labelDistance"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="#dimen/font_size" />

Dynamic Text size change according to tablet

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.

Categories

Resources