views:

64

answers:

2

I have a button which I want to occupy 75% of the screen:

Panic

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.

+1  A: 

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.

Octavian Damiean
Wouldn't `/res/layout-normal/layout.xml` equate to 3 pixel widths? (240,320,480)
Chris S
I think I finally understand it, I create the button for a 240 pixel size screen, and it scales it up for me. So a 210 dip width
Chris S
+1  A: 

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.

hackbod
How do you split a circle into 9 pieces that stretch?
Chris S
You can't do this with a 9-patch. Either change your art to be able to scale using a 9-patch, or switch to drawing the graphics with the graphics APIs. The point is, trying to force your way forward with a *specific* design like this that is going to make your life really, really hard is maybe not the best choice.
hackbod