Trying to draw a button

Go To StackoverFlow.com

0

I'm trying to draw a button with an image as background. Here's my xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="13dp"
    android:background="@drawable/button_bg"
    android:text="Break Record"
    android:textColor="#2c4417"
    android:textSize="19dp"
    android:textStyle="bold" >
</Button>

</LinearLayout>

On the graphical layout view, it looks as I intended:
enter image description here

But on the emulator, the button takes the entire width of the screen:
enter image description here

I've read the specification more than once, but couldn't get what my mistake is,
How do I write the button xml so that the button will look on the emulator (and all devices..) as in the Eclipse's Graphical Layout?

Thanks.

2012-04-04 06:49
by Oren A
do you have separate backgrounds for all screens - Adil Soomro 2012-04-04 06:53
If you meant to ask if I have separate background images for all resolutions - answer is no, i currently have 1 background image png. If you meant to ask if the same background applies to all screens in my app- answer is yes - Oren A 2012-04-04 07:05
I meant background for button i.e @drawable/button_bg. you should have different for every density. Have a look at this Dev GuideAdil Soomro 2012-04-04 07:09
Thanks for your comments. I have looked. As recommended, I used dp (rather than px), and wrap_content. And evidently, still haven't achieved "Density independence". That's the problem. - Oren A 2012-04-04 07:14


2

The width of button is due to @drawable/button_bg. If the background image is constant for different densities then hdpi would should button small in size, on the other hand, mdpi and ldpi devices would take more width to show the same button. Confirm that you have different background images and they are relative to their densities.

P.S. Run three different emulators with hdpi, mdpi and ldpi densities respectively and observe the layout.

2012-04-04 06:55
by waqaslam
Thanks, but can't I set the button's width and height in xml rather than have three different files - Oren A 2012-04-04 07:08
if you want to use a background image, then it should be justified with the densities your app will deploy on. I'm not sure if setting a width to button would resolve this, though you should try this also. But i would suggest you to make background images for hdpi mdpi and ldpi - waqaslam 2012-04-04 07:13
Ads