Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout: on TextView and android:drawableStart -- setting size of the icon?

The Lars Vogel's tutorial on SQLite, own ContentProvider and Loader uses the following layout for the list of ToDo items (check the http://www.vogella.com/articles/AndroidSQLite/article.html#todo_layout, todo_row.xml layout file):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="24dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/reminder" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:lines="1"
        android:text="@+id/TextView01"
        android:textSize="24dp" 
        >
    </TextView>

</LinearLayout> 

So far, so good. It works nicely. The Android Developer Tools (Eclipse) suggests to replace the ImageView by the drawable attribute of the TextView. I tried the following layout definition:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"

        android:layout_marginLeft="4dp"
        android:drawablePadding="8dp"
        android:drawableStart="@drawable/reminder"       

        android:lines="1"
        android:text="@+id/TextView01"
        android:textSize="24sp" 
        >
    </TextView>

</LinearLayout>

I.e. the drawableStart was used instead of the ImageView. The related android:layout_marginLeft and android:drawablePadding seems to work fine.

However, I do not know if it is possible to tell the size of the drawable. The ImageView solution used the android:layout_width/height attributes to tell the wanted icon dimensions. Is there anything similar for the TextView-only solution and the android:drawable...?

Thanks, Petr

like image 797
pepr Avatar asked Mar 11 '13 13:03

pepr


1 Answers

Unfortunately, it's not possible to change the TextView's drawable size using xml. It can be done with Java only.

final LinearLayout layout = <get or create layou here>;
final TextView label = (TextView) layout.findViewById(R.id.label);

final float density = getResources().getDisplayMetrics().density;
final Drawable drawable = getResources().getDrawable(R.drawable.reminder);

final int width = Math.round(30 * density);
final int height = Math.round(24 * density);

drawable.setBounds(0, 0, width, height);
label.setCompoundDrawables(drawable, null, null, null);
like image 72
Vladimir Mironov Avatar answered Nov 17 '22 07:11

Vladimir Mironov