Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding in ImageView is not Working

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>
like image 465
CrazyLearner Avatar asked Nov 24 '13 07:11

CrazyLearner


People also ask

How to give padding in xml?

Use the setPadding(left, top, right, bottom) method on the view to set the padding.

How padding works in android studio?

The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific number of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.

What is padding top in android?

android:paddingTop =”size in dp”This Attribute is used to specify extra space on the Top Side inside the view as shown in the below output. XML.


2 Answers

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"  
...
like image 170
Coderji Avatar answered Oct 10 '22 20:10

Coderji


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

like image 38
Nishanth Avatar answered Oct 10 '22 20:10

Nishanth