Set padding attribute on Button in android

Set padding left,top,right,bottom,start,end in button.

With the use of padding attribute developer can set button name text alignment above the button and set button text location. After setting up padding attribute in button tag button will automatically stretch out as defined padding attribute size. So here is the complete step by step tutorial for Set padding attribute on Button in android.

android-project-download-code-button

How to Set padding attribute on Button in android.

There are seven types of padding attributes available to apply on button tag. Padding is set in sp or dp like 10sp, 20 dp etc.

  1. android:padding=”  “
  2. android:paddingLeft=” “
  3. android:paddingTop=” “
  4. android:paddingRight=” “
  5. android:paddingBottom=” “
  6. android:paddingStart=” “
  7. android:paddingEnd=” “

Code for MainActivity.java file.

package com.android_examples.com.buttontextpadding;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.buttontextpadding.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Button"
 android:padding="50sp"
 />

</RelativeLayout>

Screenshot:

Set padding attribute on Button in android

Click Here To Download Set padding attribute on Button in android project.