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)
Related
I have a project when I tried to make app supporting multiple screen size it didn't work the size looks very large in some mobiles and very small in others. So I made a test project I used Smallest screen width I made dimens.xml file for (320dp, 480dp, 600dp, 720dp) and the same for activity_main.xml, I put textView in every xml file like shown in picture to know which file the mobile will read from. the problem is that I have tested it on about 10 mobiles from different screen sizes and versions but all are reading from (320dp) what am I doing wrong?
To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of most view components, instead of hard-coded sizes.
"wrap_content" tells the view to set its size to whatever is necessary to fit the content within that view.
"match_parent" makes the view expand to as much as possible within the parent view.
FOR EXAMPLE:
android:layout_width="match_parent"
android:layout_height="wrap_content"
AND
android:text="" must be hard-coded or resource of string.
Thanks.
I'm new developer for android. I build new program for android, but when I install it in different device (different screen size) , do not change automatically the size of buttons in my program,
for example: i built the program on 4 inches device(emulator), i install it in Samsung galaxy S , every things are in their place, but when i install the program in Samsung galaxy S III, the buttons are smaller then the screen size.
now, how can i fit the size of buttons by devices screen size????
Some code would help a lot in this situation.
However, Please insure you are using DIP instead of pixels. And if you really need to, you can use a Linear Layout and add weights.
<Button
android:id="#+id/button_name"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
You should use Dimension. For reference go through Dimension
FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The <dimen> element's name will be used as the resource ID.
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
and define the size here
<dimen name="font_size">18sp</dimen>
and use it like
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
But you need to define dimension for each screen size.
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" />
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.
I followed the guidelines presented in the Android docs (http://developer.android.com/guide/practices/screens_support.html) and created image buttons at 36x36pixels, 48x48pixels and 72x72pixels then upped these in the ldpi, mdpi,hdpi folders... but when i change the screen resolutions in the xml-layout-creation place in Eclipse... the image button sizes keep changing :(
Is there some other setting I am supposed to change?
My XML is very simple, for example:
<ImageButton android:src="#drawable/level1"
android:layout_width="wrap_content" android:id="#+id/imageButton1"
android:layout_height="wrap_content"
android:onClick="button_clicked1">
(full file: http://pastebin.com/nyh2BMFE)
Please advise!
EDIT:
Ok, added the default icon that Android creates in each directry and that too is not scaling so well like the rest of the buttons.
Changing resolution is not a guarantee that density changes, since density is also a function of the screen size. The safest way would be to create AVDs with the required resolutions AND densities.
You could use a DIP unit i.e. Density Independent Pixels.
If it works then please provide me some feedback.