When specifying the size of buttons and toolbars, I've been using the "device independent pixels" (dip) feature of the XML layout files. By using dips, I can specify the physical size of the GUI components, which is good for ensuring components are always readable and clickable for all screen sizes. For example, my app has a horizontal toolbar that is about 50dip high at the top with several 40dipx40dip buttons along it.
However, buttons whose physical size is just big enough to see and click on for a small screen aren't very usable on tablets. For example, my toolbar will contain huge amounts of whitespace, the buttons are hard to see (as you tend to hold the tablet further away from your face) and the buttons are hard to click on in practice (as they are small proportional to the screen)
Are there any resolution independent approaches for dealing with this?
I know I can use "layout_weight" to stretch components so I can specify what percent of the screen to use but this is going to be really cumbersome for lots of buttons (also, my buttons need to be square) if I have to do this mostly manually. I can use different layout files for each device, but this is a pain.
Are there any resolution independent approaches for dealing with this?
Use dimension and drawable resources.
You haven't indicated how your buttons are "40dipx40dip".
If they are set that way via specific heights and widths, instead of having 40dip in a layout or in a style, use #dimen/toolbar_btn. Then in res/values/dimens.xml define that to be 40dip and in res/values-large/dimens.xml (or maybe res/values-large-land/dimens.xml) define that to be, say, 60dip, or whatever works for you.
If the buttons are "40dipx40dip" because of images, then it's merely a matter of creating somewhat larger images for tablets and putting them in, say, res/drawable-large-mdpi/ resource directory.
Related
I read something about supporting multiple screen sizes in Android.
For that most of the answers recommended to create different layouts
(layout-normal, layout-small, and so on). But that would mean that
I have to define all my layouts multiple times. I don't think that
this is a smart solution.
Is there no way to create just one layout and to automatically scale
it for any display size?
There are several parameters in layouts in android that adapt themselves to the size of the screen (fill_parent, center...), however I have not seen in android's layout parameters for everything... for example 1/3 of width.
Finally what I am doing is the following:
One layout per architecture of the interface. For example, in tablets I change the architecture, integrating sometimes 2 layouts in one.
Include, programmatically, changes of sizes. For example, an button I want to be 1/3 of the screen I need to programmatically obtain the width of the screen and change the button size.
Always include several sizes of icons that android automatically selects depending on resolution.
But there is a significant part of the job that needs to be done "by hand".
I'm using API 10.
I want to make my layout looking the same across all devices. I can achieve that by using LinearLayout with weights. It works perfectly. As seen on the image below, this is exactly what I want to achieve and it works perfectly:
But the problems that occur are two:
1)I get a warning of low performance because of nested weights.
2)I can't make animations out of bounds of the parent LinearLayout,
as shown in the image below:
So, I decided to make my layout with RelativeLayout. But the major problem is that I can't get the same layout across all devices, like LinearLayout and usage of weights. I spent two days searching for a solution and reading the ADT documentation. According to the documentation I have to make my ImageViews scaled to a factor of 3:4:6:8:12, to match the ldpi/mdpi/hdpi/xdpi/xxdpi. So I did: I created the corresponding #drawble folders, re-sized my buttons as the document says, and placed them inside the matching folders. The result is this:
This is a mess, it is totally unacceptable, for two reasons:
1) Screens may be of a certain density, for example hdpi, but they
differ from device to device of a certain screen size.
2) When I use the dp units relative to an element, for example: from
top of parent, the value I provide differs from screen size to screen
size. It is not by percentage like in LinearLayout.
So, where do I go from here? I concluded from my reasearch, that the only(or not?) solution is to make different layouts for different densities AND screen sizes. Like res/layout-mdpi-large/my_layout.xml and so on. But how do I calculate the dp for sizes and distances from relative components, based on the screen sizes? Do I have to resort to this list? Are these all the devices?:
Any tips, best practices, guides for the workflow, anything?
I have a FrameLayout that is used to display a camera feed for scanning with ZBar.
I would like it to take up a large proportion of every screen the app runs on. A hardcoded 275dp square looks great on the latest phones but pushed stuff off when for smaller screens.
I am planning on hardcoding a 175dp square and then in code making it grow based on the dimensions of the phones screen.
I'll probably do a switch on various screen sizes and then decide what to resize the frame to.
Is this a good approach?
How would I go about doing this in XML?
A LinearLayout with layout_weight specified for height/width will allow you to simulate a percentage based layout, otherwise you can use fill_parent when you want to use the whole screen width/height.
You can make one xml layout file for ldpi, mdpi and hdpi(xhdpi and tvdpi if you want to) and literally set different xmls according to the screensize. With this you will be able to fit most screens without a problem, but it is not as accurate as percentage. But remember that not all android devices has the usual 16:9 or 16:10(8:5) and therefore percentage may make the square a bit different from screen to screen.
You can make your own layout qualifyers, but the standard ones are in most cases more then enough.
You should also consider makeing only the frame layout in java, and the rest of the layout in xml.
I have a client who wants me to build an Android version for their existing iPhone app. The design of the iPhone doesn't use any native iPhone elements. It's basically some sort of grid with containing images, buttons, text, etc. Of course it was easy to make the iPhone app because of the fixed pixels widths/heights. The basic grid that defines a screen is loaded via a XIB file, and I load the custom buttons in the right containers in the grid by specifying the exact coordinates.
Then comes Android...
Our client wants to target 3 specific tablets (1024x600). They have given us designs for a ~600x980 portrait version of the app. It is not recommended to use AbsoluteLayout in Android. What is the easiest way to make sure that I can scale it on different devices but that it will look like the given design on the 3 target tablets.
One idea I had (which I'm not sure about whether I can implement it) was:
Get screen width in pixels and height
Based on width/height ratio of the design, pad with bars on top/bottom or left/right
Still do an AbsoluteLayout based on this information
I'd rather not do it this way because it sounds involved and counter to the Android way of doing things. Another issue that is created by scaling is the following. There is a bar of buttons that have a lines separating them. These lines are 4 pixels wide. Obviously, when you start scaling, this is going to mess this up completely. I can't seem to find much information about this s
You probably want to start here:
http://developer.android.com/guide/practices/screens_support.html
But quick points are probably
Do not use an absolute layout. Your life will become terrible
Handle sizes in density independent pixels so they will scale properly on different devices
Use ninepatch images so that when images stretch they will stretch in the proper regions maintaining your 2px borders ect.
Take advantage of the different resource folders for images at different densities (drawable-mdpi vs drawable-hdpi) and layouts at different sizes (layout-small vs layout-large). The latter will allow you to have separate layouts for your tablet devices.
Best of luck :-)
I've read the Android documentation:
http://developer.android.com/guide/practices/screens_support.html
but still have some questions.
I'm trying to design a music application which basically has images of the instrument (ImageButton) that play a sound when clicked. However, I'm confused about how to have the ImageButtons scale to fit all the different screen sizes and how to position them.
Which layout is best used for needing to position ImageButtons in specific locations on the screen? (i.e. cymbals on a drum set) FrameLayout, RelativeLayout?
If I only really care about medium and large screens, do I need to create different resources (images) for both as well as a different XML layout to position them?
I'm trying to find the simplest way to do this without having to create a separate layout XML file for positioning/size and separate image resources for each screen.
Any guidance is greatly appreciated. Thanks!
Which layout is best used for needing
to position ImageButtons in specific
locations on the screen? (i.e. cymbals
on a drum set) FrameLayout,
RelativeLayout?
RelativeLayout. In the real world, cymbals are positioned relative to the drums.
If I only really care about medium and
large screens, do I need to create
different resources (images) for both
as well as a different XML layout to
position them?
In terms of the layout, you only need a different layout if the rules would differ. It is very possible that the positions of the cymbals relative to the drums would not change based on screen size.
In terms of the images, I would suspect you'll want separate versions for different screen densities, not for different screen sizes. That way, a cymbal will be (roughly) the same physical size (inches/millimeters), even though that takes more pixels on a high-density screen. If you use the same images for all densities, in some cases, the images may be too small to tap readily.