Android, Image buttons are not density independent - android

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.

Related

Is there a function in Android Studio to automatically adjust the layout to the display size?

I'm in the process of creating a small questionnaire. Although all views are constrained, the individual text views overlap or slide to the edge.
So far I have only created the design for the layout and have not yet linked any functions with anything. When I display the layout on a larger display, everything is shown as it should be.
Can someone help me and tell me how I can design my layout so that it automatically adjusts to the display size?
I'm really new about programming in Android and don't know all the backgrounds I may have disregarded.
I tried to use the AutoSizeMaxText fuction but I'm not sure if I used it the right way.
As far as I know there is no such thing as an automatic function to support different screen sizes.
What you can do:
Use constraintLayout
Use match_parent and wrap_content instead of hard-coded sizes in your xml
Create different layouts for almost every (if not every) screen resolution:
ldpi, mdpi, hdpi, xhdpi, xxhdpi
Use stretchable nine-patch bitmaps for image resources.
Consider using fragments and double-pane structure for large devices
You may want to chek this library: https://github.com/intuit/sdp
and this one for text size: https://github.com/intuit/ssp
Don't forget to test it in as many devices with different resolutions and pixel density as you can.
You can check the official documentation for this issue: Android developers: Support different screen sizes

sp not handling TextView text well

I have a text view:
<TextView
android:id="#+id/attribution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/creative_commons"
android:textColor="#color/white"
android:textSize="32sp"
android:textStyle="bold" >
</TextView>
On xhdpi it displays nicely, in one line:
Sounds used under the Creative Commons Attribution 3.0 license
However, in hdpi, it looks like this:
Sounds used under the Creative Commons Attribution 3.0
license
I thought sp (as in 32sp) was supposed to handle screen density.
What do I need to do to correct this?
To address a couple of the suggestions:
Using dp makes no difference. This is not surprising since sp is dp + user font preferences
Using singleLine does indeed force it to a single line, which then runs off the screen.
One way to fix this problem is to use Android resource qualifier mechanism. You can specify alternative layout for screens with different sizes. I assume 32sp is too big for devices with smaller display in your case. Here is how you fix it with Android resource qualifier:
Create a folder, res/layout-sw480dp
Keep the same layout file in res/layout folder
Copy the same layout file to the new folder res/layout-sw480dp, and modify the font size to be smaller (any size that looks good for phone)
Please note, "480" is chosen for screen with 480x800 mdpi. You can use "320" for handsets with 240x320 ldpi, 320x480 mdpi, or 480x800 hdpi.
Refer to http://developer.android.com/guide/practices/screens_support.html if you need information in detail.
Hope it helps!
SP is similar to DP, but further modified to text size specifics like user settings.
However, DP/SP are 'brackets' and a number of resolutions and screen sizes fit into these same brackets and the resulting size isn't always going to fit perfectly.
As such you should concentrate on making your layouts adaptive, this is especially true if you're localising your application as text will be different lengths in other languages.

Handling different resolutions within the same configuration group

I have a normal-hdpi-480x800 layout, which includes buttons and similar graphics. I mostly use RelativeLayout, since it's recommended for better performance, and position my elements from the upper left bound relatively to each other. According to screens_support.html this group might also include 600x1024 resolution devices. So, when I test the application on LG L9 (540x960), which also falls into normal-xhdpi, it looks horrible - it seems there's no difference between using pixels and dp. Here's an example of code:
<Button
android:id="#+id/fb_post_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/share_fb_m_l"
android:layout_marginTop="#dimen/share_fb_m_t"
android:background="#drawable/fb_btn" />
Any help ? What I might be doing wrong ?
Thanks.
Just specify different dimensions in folders values-ldpdi, values-mdpi, values-hdpi, values-xhdpi if you need more control over button sizes according to screen density. Like this:
<resources>
<dimen name="share_fb_m_l">47dp</dimen>
<dimen name="share_fb_m_t">57dp</dimen>
</resources>
Update: You can get more control over this:
Specify multiple different images for different screen resolutions and densities. To do so read supporting multiple screens. If you need even more precision in image scaling and quality you can use the fundamental size of screen attribute which is swdp - you can specify the smallest width of the screen where your image should be used. This is a qualifier name for a resource folder.
Place your dimensions in these folders values-sw540dp, values-sw540dp, values-sw600dp - these should serve as subfolders for more control.

Android: Handling screens that are the same dpi level but different

I have gone through all of the Android docs about handling multiple screen sizes but I still haven't been able to find an answer to this question or how to handle this.
If there are two phones that have the same dpi level (such as both being hdpi) I can provide one resource for them and set the layout parameters as such:
<ImageView
android:id="#+id/icon"
android:layout_width="94dp"
android:layout_height="94dp"
>
The "icon" in this instance is large enough so that it will scale down to fit that layout in all cases. In an ideal world, I would assume that the icon would appear the exact same size on all hdpi devices, however when I tested it out on a LG G2x and a HTC Sensation, the image is smaller on the Sensation. So is Android always just using a factor of 1.5x when calculating the size the hdpi image? Is there something I can do to guarantee the size will be the exact same on all hdpi devices? Thanks.
You can use this to help you out with the proyect
http://developer.android.com/training/multiscreen/screensizes.html
The answer was that the system does use a standard multiplier for each dpi level (1.5x for hdpi for example). In order to get around this I just used the .xdpi and .ydpi values of DisplayMetrics and do my calculations off of those real values.
Usually when ever we want to do like this we usually use wrap_content. so once try layout_width = "wrap_content",layout_height = "wrap_content". Such that ANdroid SDK will look.

Scaling ImageButtons for multiple resolutions

I have a button which I want to occupy 75% of the screen:
On a 480x800 resolution screen this would be 360 pixels wide.
On a 280x320 resolution screen this would be 210 pixels wide.
How do I go about specifying this in my layout XML file?
I understand there is a DIP unit of measurement, but does that also work to scale screen images for resolutions? What DIP measurement would I use for this, and do the images need to be saved at 160dpi.
You should really try to avoid making this complicate by thinking about all the different screen sizes, and densities and such.
You say you want your button to occupy 75% of the screen.
Okay, put it in a LinearLayout somewhat like this:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent
android:orientation="horizontal">
<My75PercentButton
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="75">
<View
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25">
</LinearLayout>
This makes the two views have 0 width, but spreads 75% of the available space for them after that into the first, and 25% into the second.
If you want to make your button stretch to fill that space (instead of just leaving empty padding around it), make it a 9-patch.
You will also want to support different screen densities, so provide different image sizes for your button in drawable-ldpi, drawable-mdpi, and drawable-hdpi.
But I will really caution: trying to push yourself into a world where you are designing your graphics for various specific screen sizes is going to end up in a huge world of hurt. You really don't want to do that. There are already a lot of screen sizes Android devices are running; this is only going to increase greatly in the future. If you take advantage of layout managers and other such facilities to adjust for the actual screen size, your app will run well on all of these screens with little to no work on your part. If you try to design your graphic for specific screen sizes, you are going to be doing this over and over and over again until you can't stand it.
You can create different layouts for different screen densities by defining the layouts in their appropriate subdirectories called layout-small for small density screens, layout-normal for all densities and layout-large for high density screens.
The dip-measurement works for this but I'm not quite sure on what value to set I guess you gotta try it and find out which one fits your needs best.
You can find an in depth article here.
EDIT: I've actually mixed them up ... sorry for the confusion. Corrected my answer above.
#HardQuestion: Here are the supported media formats for Android.

Categories

Resources