making fonts get larger on larger screens - android

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" />

Related

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)

Android - ImageView - Multiple Screen Support - Set height/width as dp or match_parent/wrap_content?

I am currently trying to adjust my Android App so it will look and feel similar on multiple screens/devices.
I know by now that a major part in this is to provide multiple image sizes for every image file according to the x1, x1.5, x2, x3, x4 ratios for mdpi, hdpi, xhpi, xxhdpi and xxxhdpi respectively, and I have finished doing so today.
After doing this, I have defined Density independent Pixel dimensions in the #dimen.xml values resource that correspond with the actual image sizes in pixels of the MDPI resources. Subsequently, i have set the imageviews in question's layout_width and layout_height to this dimension.
I am currently at a loss, however, as to why my application still looks significantly different on an MDPI emulator than it does on an HDPI emulator. To highlight what I mean, I'll provide the following screenshot showing the HDPI and MPDI emulator next to one another (left is HDPI (4" WVGA Nexus S) and right is MDPI (5.4" FWVGA)). I want both of them to look like the MPDI version, but what I've done so far apparently isn't working.
I have three theories of my own as to why this is not working the way I intend it to:
1. I am not supposed to set ImageView layout_width and layout_height to a dp value, but rather match_parent or wrap_content (?) (and change the structure of my .xml layouts in the process).
2. I am not only supposed to define multiple drawable resources, but also multiple layout resources for different screen sizes (?).
3. I have misunderstood the entire idea behind how this is supposed to work (?).
I will also give you an example of one of the list items that can be seen in the first screenshot (#drawable/phone_speed_icon is a 64 x64 pixel resource in MPDI and a 96x96 resource in HDPI, and #dimen/icon_attribute_size is 64dp):
<LinearLayout
android:id="#+id/llSpeed_PreSession"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingEnd="20dp"
android:paddingStart="20dp"
android:weightSum="100">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/icon_attribute_size"
android:layout_weight="20"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="70"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="#dimen/icon_attribute_size"
android:layout_height="#dimen/icon_attribute_size"
android:src="#drawable/phone_speed_icon" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="30"
android:gravity="center_vertical"
android:paddingStart="10dp"
android:text="Speed"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#878787"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:gravity="center">
<android.support.v7.widget.SwitchCompat
android:id="#+id/swSpeed_PreSession"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
In the end, I have four questions that I'd like answered:
1. How do I make the list items in screenshot 1 look the same on MDPI devices as HDPI devices? Does this have anything to do with one of the three theories I mentioned earlier?
2. Why is the header text ("What do you want to measure?") wrapped on one device, but not on the other? They use the sp unit (via android:style/TextApperance.TextApperance.Large)?
3. Shouldn't everything be more spaced out on an HDPI device (if anything) rather than less spaced out? The HDPI emulator looks as if it "has got way less pixels available", if you can understand what I'm saying even a little.
4. How do I make the Fragments on the second screenshot look the same? Or should i not even want this, because the HDPI is (for some reason) physically smaller, which is why the layout is less spread out?
Anyway, I have been at this all day and the more I read the more thouroughly confused I get, so any help is greatly appreciated!
You have the option to create multiple dimens.xml files depending on a variety of factors. Here you'll see what my current project looks like with the various dimens.xml files in Android Studio based on screen width.
However, you can have different requirements for each dimens file you want. For example, you can create dimens files for each density:

Why do these ImageButtons changing position using relative layout?

Learning some Android development through trial and error, however I'm having a slight issue with the rendering of buttons on top of an image view depending on the resolution of the phone.
I have an imageview with two imagebuttons (at the moment) on top. They are relative to the imageview. I will post the xml markup below. The issue is when I run this on the "Nexus 4" in Android studio, everything looks correct. When I debug through my Samsung Galaxy S4, the imagebuttons are off slightly, and I'm not sure why this would be a problem if everything is truly relative to the imageview. I understand that the resolutions are different, but how would one go about making sure that this renders the same on the smallest of screens, as well as the newer 1080p screens that are becoming more popular?
I can post pictures if need be, but I feel that the xml will provide adequate information on the issue.
Any help is appreciated. Thanks in advance!
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/SRMap"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:background="#drawable/sr_map"
android:layout_columnSpan="2"
android:layout_rowSpan="1"
android:layout_column="0"
android:layout_row="0"
android:contentDescription="Map of Summoners Rift"
android:cropToPadding="false"
android:layout_marginBottom="177dp"/>
<ImageButton
android:layout_width="15dp"
android:layout_height="15dp"
android:id="#+id/BlueSideAlert_BlueBuff"
android:background="#drawable/blue_alert_circle"
android:layout_alignTop="#+id/SRMap"
android:layout_alignLeft="#+id/SRMap"
android:layout_marginLeft="90dp"
android:layout_marginTop="128dp"/>
<ImageButton
android:layout_width="15dp"
android:layout_height="15dp"
android:id="#+id/BlueSideAlert_RedBuff"
android:background="#drawable/blue_alert_circle"
android:layout_alignTop="#+id/SRMap"
android:layout_alignLeft="#+id/SRMap"
android:layout_marginLeft="180dp"
android:layout_marginTop="215dp"/>
but how would one go about making sure that this renders the same on the smallest of screens, as well as the newer 1080p screens that are becoming more popular?
See the Docs here about supporting different screen sizes/resolutions. You create separate layout files adjusted to what you need. Simply use different qualifiers for the layout folder name according to the docs and the correct one will be used according to the device that loads it.
You can try and adjust one file to work on different screens but you are a lot better off using different layouts if it will be ran on different screen sizes and resolutions.
For example: you can have a default res/layout folder and a res/layout-large to be used on larger screens
I handled this in a fairly ugly way, but it works across all screen sizes now. Basically say the primary image was 800x800 dp (the image view). The buttons would be smaller than this (20x20 dp), however through transparency, i created 800x800 dp imageviews (note: no longer buttons) with everything transparent but the "icon" (still 20x20 dp). Although slightly larger in terms of file size, it's negligible in my case. Now no matter what the size of the screen, the imageviews all stretch the same amount, thus removing any guesswork from using the different pixel densities.
I realize that this wasn't the most graceful solution, but I cannot see any drawback aside from making some of the layout development more difficult, and the image files are slightly larger. I used Paint.NET in order to create the .png's that I used.
I am creating an app that is relevant to me, and that I figured would be easy to implement using some of the core functionality already provided in android. My idea is a simple League of Legends Jungle Timer Application. This is important to know as I link the pictures so people can understand the context.
Note that both images are the same resolution, there is just no border on the second one.
http://i.stack.imgur.com/Ad3LZ.jpg
http://i.stack.imgur.com/NfhRM.png
Additional details:
For this app I have disabled the "landscape" orientation, I plan on doing this using some of the details that were provided on this page (layouts).
All of these icon ImageViews are relative to the map, which is the first link, so they will always resize correctly. There are 12 of them also, if it matters.
Thanks all for your help!

Making text same size on different devices

I wonder how to make application look the same on different devices. I have read Supporting Multiple Screens many times but I still can't understand how to live.
Here is a sample layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
android:textSize="30sp"
/>
</LinearLayout>
There is Galaxy S II (480x800) and Sensation XE (540x960). They are both hdpi, physical screen size is almost the same. I expected to see the same looking on both devices but in fact text on 540x960 is smaller then on 480x800:
(left is 480x800, right is 540x960)
I tried to specify text size as dimension resource and make separate folder values-w540dp but it took no effect.
How do you guys make your application look the same on different hdpi devices?
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAppearance="?android:attr/textAppearanceSmall"
These atributes may solve your problem here.Otherwise you have to setup your layout dynamically with some ratios(or factors you generate like :textSize=factor*height) using your screen height and width.
Have a look at metrics , if you specify your textsize in dp (Density-independent pixels) it should work. The default size for text should be around 14dp. (TextAppearance.small)
2 Sites that might be helpful as there are some nuggets in both related to your problem, however, neither is an exact 1-2-3 how to correct the issue you're seeing:
http://www.pushing-pixels.org/2011/11/08/deep-dive-into-responsive-mobile-design-part-1.html
http://www.alistapart.com/articles/fluidgrids/

Categories

Resources