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.
Related
Is it standard to make buttons text and other views text larger in tablet size? I make dimens.xml for different device sizes and make different text sizes for different device sizes. for examle in normal dimens file:
<dimen name="small_text_mobile">14dp</dimen>
and for large size dimens file:
<dimen name="small_text_mobile">18dp</dimen>
and use this dimens for my textViews and buttons , extra..I want to know if it is standard? or no need for that and texts of views like buttons should be in same size in different device sizes?
The unit used (dp) will convert the button and layout sizes to appropriate pixel sizes on the actual device (making them independent of actual device screen size). Therefore, on a small device it will look relatively similar to a large device. However, depending on the situation, it may be more efficient use of screen space to have different dimensions/layouts for different screen sizes. It really depends on the view being used and the level of user experience required for it. A lot of the time dp will get the job done close to what you would want. The Android documentation on supporting multiple screen sizes is excellent in explaining all the different situations and control a developer could have on their layouts.
Depending on the layout, it may be better practice to create multiple layouts to fit small and large screens in different orientations. It depends on the layout and how complex it is. If it looks unnatural on a certain device, I would recommend creating another layout (or applying some changes on the current one) to better fit all devices.
I am doing a lot of work about android layout I still can't create a layout working in every phone. I am not sure about best the way to create a layout so correct me if I am wrong . There are three things to keep in mind :
Screen px (resolution, for example 1080x1920 px)
Screen dimension "inches"
Screen density dp or also called dpi ( dp is a virtual resolution, it's correct?)
To draw a layout working in every phone (my app will works for phones) do I have to create a directory "layout-kindofdensitydpi for every screen density (layout-ldpi,layout-mdpi,layout-hdpi,layout-xhdpi,layout-xxhdpi,layout-xxxhdpi) and draw "manually" or do I have to do something else?
I did a test, I created these 6 directory and drew manually for every resolution. It requires a lot of time, also device with a resolution of 768x1280 my app doesn't respect what I drew, for example spaces aren't respected, the collocation of elements doesn't result correct and frame layout with ImageView inside isn't scaled.
What I have to do? In some Android books isn't mentioned that elements could not resize and usually them explain how to put some text or image without analyse resize in every device.
Thanks in advice
First you should find the appropriate layout type for your UI (RelativeLayout or LinearLayout). Sometimes using a good layout(or nested layouts) can make the UI look good on every screen. I prefer LinearLayout cuz I can simply set layout_weight for components.
Then set different sizes in dimens.xml file for different densities or screen size buckets. Like this:
And you can also use match_parent or wrap_content
Don't forget to set the screen orientation of your activity if it doesn't need to rotate. Having one orientation makes it much easier to design.
If you couldn't make a good layout using tips above you should create multiple layouts to fit every screen size or density (Screen size and density are two different things).
You should find the best way to determine your screens according to.
Screen size bucket (small, normal, large, and xlarge) picks a layout that fits the screen (or the closest), density bucket (ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi) picks a layout according to the density ,or the smallest width (I think it's almost the same thing as density).
480dp is the sw of these two devices
You can make layouts for different screen orientations too.
I don't like creating layouts for different screens for some reason. One of them and the most important is that sometimes same screen sizes have different densities and it makes it hard and time consuming. To create a layout and you should also provide a lot of pictures.
Use
android:layout_width="match_parent"
android:layout_height="match_parent"
to the outer layout to get access of the full screen of the device.
Also, if there are no changes in your UI then you don't need to create different layout folders.
Refer http://developer.android.com/training/multiscreen/screensizes.html
You should have a look at
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/index.html
I would suggest using layout-swXXXdp or layout-large etc instead of layout-KindOfDensitydpi
Create your relative layouts using RelativeLayout and use the weights of LinearLayout. Don't hard code any of the positions in the layout
Is it best practice to vary your text size based on screen size or based on screen density, or both?
I already know to use sp. I have static title text that fits on one line in some screens, and wraps around to the next line on others. I need to avoid the word wrap.
Its good practice to do so. On an 10" Tablet you are better using bigger Text sizes.
You can for example define your Custom values for each type of device using the values-sw720dp
folder an place it there:
<dimen name="HeadlineSize">28sp</dimen>
Also the already System Values like android:style/TextAppearance.Large scale. Means on an Phone will this be smaller(eg. 18sp) than on an Tablet (24dp)
I want to set the text size in my app. I have created different layouts (layout, layout-small, layout-normal, layout-large, layout-xlarge) and for values (values, values-ldpi, values-mdpi, values-nodpi, values-hdpi, values-xhdpi). But the problem is some devices in emulator have different sizes but having same dpi. So I am little confuse how to write different text sizes in dimens depending upon screen sizes not on dpi value. And how its work?
I think depending on device size android pick layout and then depending on dpi its choose values. Is it correct or not?
Edit-
I have one activity in my app to show text. I have scroll on text. And I set the values of text size in values-mdpi. But when I run my app on emulator 3.2"QVGA(ADP2)(320 x 480:mdpi) scroll on text work. But when I run my app on emulator 5.1"WVGA(480 x 800:mdpi) all text on half screen. I want size of text big on large screen although they belong mdpi.
Yes it is correct. Depending on the device screen size and specification, android automatically loads the desired layout and all its elements and fonts.
style="#android:style/TextAppearance.DeviceDefault.Medium"
no need to create all layouts, just see this example it is working for me
Note :textviewtwo.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
http://polamreddyn.blogspot.in/2014/01/adjust-text-size-in-all-android-devices.html
What is the best way to set the text size so it looks the same on all devices? The biggest problem I am having is setting the text size on the buttons so everything fits or isn't too small. I have tried setting the size in the xml, and I have just tried setting it dynamically by getting the screen size and messing with the screen width and height to set the size. I had tested different things on different devices and thought all was well, until I just tried my app on the Galaxy Nexus and half of my words were getting chopped off inside of the button. I made some adjustments and the font is now way too small on tablets.
Using the xml has worked fine for me before, but most of these new 7" tablets use the large layout, so my images and text are really small if I set the height, width, and text size with the xml. Setting the widths and hights dynamically have helped with the buttons and image sizes, but the font still doesn't look quite right.
Is my best bet just to find a happy medium via xml(large, x-large, etc.) or are there other ways?
Have you seen this article: Supporting Multiple Screens ?
It says:
you should prefer the sp (scale-independent pixel) to define text
sizes. The sp scale factor depends on a user setting and the system
scales the size the same as it does for dp.
The same issue is explained here, Different text size for different hardware