2

I have few TextViews with drawable icons on left side. layout:

         <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="@dimen/detail_padding">                  

                <TextView
                    android:id="@+id/location"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/title_margin"
                    android:drawableLeft="@drawable/ic_location"
                    android:drawablePadding="@dimen/drawable_padding"
                    android:drawableStart="@drawable/ic_location" />

                <TextView
                    android:id="@+id/status"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/title_margin"
                    android:drawableLeft="@drawable/ic_status"
                    android:drawablePadding="@dimen/drawable_padding"
                    android:drawableStart="@drawable/ic_status"/>

                <TextView
                    android:id="@+id/description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/title_margin"
                    android:drawableLeft="@drawable/ic_description"
                    android:drawablePadding="@dimen/drawable_padding"
                    android:drawableStart="@drawable/ic_description"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

Screenshot:

enter image description here

If the text is too big, icon stand in the center of textview. I want to set icon aligned top of textview. Is it possible?

elya_a
  • 155
  • 2
  • 8

4 Answers4

0

You must keep the layout as RelativeLayout

<android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/card_margin">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="@dimen/detail_padding">                  
            <TextView
                android:id="@+id/location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/title_margin"
                android:drawableLeft="@drawable/ic_location"
                android:drawablePadding="@dimen/drawable_padding"
                android:drawableStart="@drawable/ic_location" />
            <TextView
                android:id="@+id/status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/title_margin"
                android:drawableLeft="@drawable/ic_status"
                android:drawablePadding="@dimen/drawable_padding"
                android:drawableStart="@drawable/ic_status"/>
            <TextView
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/title_margin"
                android:drawableLeft="@drawable/ic_description"
                android:drawablePadding="@dimen/drawable_padding"
                android:drawableStart="@drawable/ic_description"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
0

You can set

  android:layout_toRightOf="@+id/imageId"

on the textview, and

  android:layout_alignParentLeft="true"

on the image.

hope this helps.

Aman jangra
  • 268
  • 1
  • 16
0

Just use ImageView

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/detail_padding">

    <TextView
        android:id="@+id/location"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/title_margin"
        android:drawableLeft="@drawable/ic_location"
        android:drawablePadding="@dimen/drawable_padding"
        android:drawableStart="@drawable/ic_location" />

    <TextView
        android:id="@+id/status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/title_margin"
        android:drawableLeft="@drawable/ic_status"
        android:drawablePadding="@dimen/drawable_padding"
        android:drawableStart="@drawable/ic_status" />

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/drawable_padding"
            android:src="@drawable/ic_description" />

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/title_margin" />
    </LinearLayout>
</LinearLayout>

elya_a
  • 155
  • 2
  • 8
0

For doing this you will have to create a custom Drawable that wraps your Drawable, and then manipulate the drawing of your custom Drawable by overriding the method onDraw(Canvas).

There is a sample below. This aligns the image to the top, but you can also make it align to the bottom, left or right of the TextView by implementing the required logic in the onDraw(Canvas)-method. You might also want to build in a margin in the onDraw(Canvas), to make your design implementation pixel perfect.

Sample usage:

GravityTopDrawable gravityDrawable = new GravityTopDrawable(innerDrawable);
// NOTE: next 2 lines are important!
innerDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
gravityDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
mTextView.setCompoundDrawables(gravityDrawable, null, null, null);

Sample code:

public class GravityTopDrawable extends Drawable {

    // inner Drawable
    private final Drawable mDrawable;

    public GravityTopDrawable(Drawable drawable) {
        mDrawable = drawable;
    }

    @Override
    public int getIntrinsicWidth() {
        return mDrawable.getIntrinsicWidth();
    }

    @Override
    public int getIntrinsicHeight() {
        return mDrawable.getIntrinsicHeight();
    }

    @Override
    public void draw(Canvas canvas) {
        int halfCanvas= canvas.getHeight() / 2;
        int halfDrawable = mDrawable.getIntrinsicHeight() / 2;

        // align to top
        canvas.save();
        canvas.translate(0, -halfCanvas + halfDrawable);
        mDrawable.draw(canvas);
        canvas.restore();
    }
}
varunkr
  • 5,364
  • 11
  • 50
  • 99