javaandroidandroid-fragmentsandroid-resolution

Setting a fragment weight attribute for different device resolutions


I'm writing an Android application and for testing I have a 9" tablet with a 1024 * 600 resolution. I only have a few activities and they are all broken into 3 fragments. The top fragment has a weight of .1 and contains the title of the activity. The middle fragment has a weight of .7 and is a scrollview of table rows returned from a query. The bottom fragment has a weight of .2 and contains all the buttons and textviews for user input. I used match_parent and wrap_content with relative layouts where possible. Nothing is hard coded and it looks right.

When I install the app on a 10" tablet with a resolution of 1280 x 800 the bottom fragment takes up more real-estate and there is more empty space around the textviews and buttons. It doesn't look right.

What do I do to accommodate these different resolutions with respect to fragment weights? I read other threads that mentioned creating different folders for images/files and different dimens.xml files. Those threads seemed to address resolutions issues for images and margins.

I want to be able to control how much weight is set based on a screen resolution (low, med, high, etc.).


Solution

  • Look in the official doc for resource specifiers. The things you're looking for are smallest width, available width, and available height. You can bundle resources for certain screen sizes into buckets based on values you define, and use those value in your layouts. Note that the values are in dp, not actual pixels.