Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom button margin

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..

like image 370
Mithun Sreedharan Avatar asked Dec 02 '10 12:12

Mithun Sreedharan


People also ask

How to Set margin of a Button in Android studio?

LayoutParams params = new LayoutParams( LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ); params. setMargins(left, top, right, bottom);

How to Set shape of Button in Android?

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 .

What is layout_ margin in Android?

android:layout_margin. Specifies extra space on the left, top, right and bottom sides of this view.

When to use padding vs margin Android?

Padding is for inside/within components. Eg. TextView , Button , EditText etc. Margin is to be applied for the on-outside of the components.


2 Answers

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.

like image 182
Octavian A. Damiean Avatar answered Sep 19 '22 02:09

Octavian A. Damiean


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!

like image 28
Dhunju_likes_to_Learn Avatar answered Sep 22 '22 02:09

Dhunju_likes_to_Learn