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
I need to add some margin between buttons, Any help appreciated..
LayoutParams params = new LayoutParams( LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ); params. setMargins(left, top, right, bottom);
We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line .
android:layout_margin. Specifies extra space on the left, top, right and bottom sides of this view.
Padding is for inside/within components. Eg. TextView , Button , EditText etc. Margin is to be applied for the on-outside of the components.
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.
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,
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>
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With