21

I want to make a screen which contains a top bar and an image. I use TextView for the top bar and ImageView for the image. I want to set padding only to the image view. My xml is given below. The padding action is not working. Can anybody explain why and what to do?

<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:background="@drawable/background" >

    <TextView android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/home"
        android:textSize="20sp"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:shadowColor="#000000"
        android:shadowRadius="1"
        android:shadowDy="-1" 
        android:gravity="center"
        android:background="@drawable/navBar"/>

    <ImageView android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:layout_below="@+id/topBar"        
        android:background="@drawable/image"/>

</RelativeLayout>
CrazyLearner
  • 782
  • 3
  • 11
  • 28

5 Answers5

27

You should use layout_margin instead of padding so it should be as following:

<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:background="@drawable/background" >

<TextView android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/home"
    android:textSize="20sp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:shadowColor="#000000"
    android:shadowRadius="1"
    android:shadowDy="-1" 
    android:gravity="center"
    android:background="@drawable/navBar"/>

<ImageView android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"   
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:layout_below="@+id/topBar"        
    android:background="@drawable/image"/>

and you can specify 1 layout_margin for all directions so instead of using

android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"

you can use:

android:layout_margin="10dip"

hope this is what you are looking for. give me a feedback otherwise ;)

update

you can set cropToPadding also:

<ImageView 
...
   android:cropToPadding="true"
   android:padding="15dp"
   android:scaleType="centerInside"  
...
Siarhei
  • 2,358
  • 3
  • 27
  • 63
Coderji
  • 7,655
  • 5
  • 37
  • 51
20

On the ImageView use

android:src="@drawable/image"

instead of

android:background="@drawable/image"

src tag honors the padding whereas background tag does not. In some cases, esp. on clickable icon, it is a bad idea to use layout_margin instead of padding, as padding belongs to the view and makes the click area bigger!.

Nishanth
  • 1,801
  • 21
  • 25
10

The only solution for this is adding the imageview to another container.

 <FrameLayout
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginRight="10dp"
        android:padding="10dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@drawable/ic_delete_blue" />
    </FrameLayout>
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
0

The padding specifies an extra space inside the view on which we applied Padding. Simply, Padding means Push Inside. When we say push inside we mean in a rectangle type view the content pushed itself according to the dimension specified in the padding attribute. Diagrammatically, the concept of Padding is shown as:enter image description here

Syntax: android:padding=”size in dp”

It’s Working is Shown as below:

   <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:background="#009933"
        android:text="ABCDEF"
        android:textColor="@color/white"
        android:textSize="30sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp"
        android:background="#009933"
        android:text="ABCDEF"
        android:textColor="@color/white"
        android:textSize="30sp" />

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Rehan Khan
  • 1,031
  • 13
  • 10
0

Seems like you need the app:contentPadding="10dp" attribute

Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65