28

I have a custom button layout

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <gradient android:startColor="@color/pres1"
                android:endColor="@color/pres2" android:angle="270" />
            <stroke android:width="5dp" android:color="@color/stro3" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="20dp"
                android:right="10dp" android:bottom="20dp" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape>
            <gradient android:endColor="@color/focu1"
                android:startColor="@color/focu2" android:angle="270" />
            <stroke android:width="5dp" android:color="@color/stro2" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="20dp"
                android:right="10dp" android:bottom="20dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient android:endColor="@color/norm1"
                android:startColor="@color/norm2" android:angle="270" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="20dp"
                android:right="10dp" android:bottom="20dp" />
        </shape>
    </item>

</selector>

And for the below lay out

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/all_white">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello"
        android:textColor="@color/all_red" />

    <Button android:id="@+id/mq_categories" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Browse Quiz Categories"
        android:background="@drawable/custom_button" />
    <Button android:id="@+id/mq_random" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Enter Random Quiz"
        android:background="@drawable/custom_button" />

</LinearLayout>

following output is generated

alt text

I need to add some margin between buttons, Any help appreciated..

1
  • Hello, I am looking for the exact colors as these buttons, can you please post the hex code of the colors? Thank you! Commented Dec 8, 2012 at 17:54

3 Answers 3

37

Just do it this way

<Button android:id="@+id/mq_categories"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Browse Quiz Categories"
    android:background="@drawable/custom_button"
    android:layout_marginBottom="5dp"/>

This way you set a margin of 5 density independent pixels on the bottom of your first button.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, but why does the margin disappear? Even if layout_margin is specified in the custom buttom style, it seems to be ignored!
This implies that I have to set margins (or padding) on ALL my button views. Can't this be done with a theme? (Without setting the padding on all views in the app? developer.android.com/guide/topics/ui/themes.html#ApplyATheme )
@Octavian how to set margins programmatically
2

Rather than using LinearLayout use RelativeLayout so that margins can be set in all the way you can. Cheers

2 Comments

Would you please explain what you mean? I don't really understand it.
In Layout main.xml when your are inflating it in UI. There should be LinearLayout. There you can used RalativeLayout & set the margins & placements of buttons by keeping the TextView of as root.
2

I had the same problem and the solution doesn't seems to be out there. After trying couple of things following did the trick for me,

  1. Define Custom Button Layout as done in the question
  2. Inside res>values>styles.xml file, add the new style as below,

    <style name="Widget.Button_Custom" parent="android:Widget">
        <item name="android:layout_margin">5dp</item>
        <item name="android:background">@drawable/button_custom</item>
    </style>
    
  3. Use the style in your layout

,

<?xml version="1.0" encoding="utf-8"?> 
<Button android:id="@+id/mq_categories" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="
        android:background="@drawable/custom_button" 
        style="@style/Widget.Button_Custom"
/>

That should do it. Besides margin, you can define many other properties in the style tag. Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.