14

Hi is there anyway of providing width and height of a drawable defined in drawable.xml in drawable folder.

Example:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/button_focused" /> <!-- focused -->
     <item android:drawable="@drawable/button_normal" /> <!-- default -->
 </selector>

I wanted to specify width and height maybe somehow using "scale" tag inside it which i tried but didnt worked. This is my code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/icon_ratingstar">
        <scale android:scaleWidth="20" android:scaleHeight="20" android:drawable="@drawable/icon_ratingstar" />
    </item>
</selector>
user606669
  • 1,674
  • 7
  • 27
  • 39

4 Answers4

7

layer-list will help: here change values according to your requirement

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <size
            android:width="20dp"
            android:height="20dp" />

        <solid android:color="@android:color/black" />
    </shape>
</item>
<item
    android:width="20dp"
    android:height="20dp"
    android:drawable="@drawable/slider_knob" />

Parth
  • 1,908
  • 1
  • 19
  • 37
2

You can use attribute width and height in API level 23 and higher:

<item
    android:width="18dp" android:height="18dp"
    android:drawable="@drawable/icon_ratingstar"/>

For lower API levels, I do not know a way of specifying width and height directly. The best practice is scaling drawables as far as I know.

Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42
1

With selector and drawable you can do it like this for example:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" >
        <layer-list>
            <item android:drawable="@drawable/your_first_drawable"  
               android:height="25dp" android:width="25dp"/>
        </layer-list>
    </item>
    <item android:state_checked="false" >
        <layer-list>
            <item android:drawable="@drawable/your_second_drawable"  
               android:height="25dp" android:width="25dp"/>
        </layer-list>
    </item>
</selector>
-16

You can specify inside the <item> tag the width and height of the drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
       android:width="100dp"
       android:height="20dp"
       android:drawable="@drawable/button_pressed" /> <!-- pressed -->
 <item android:state_focused="true"
       android:width="100dp"
       android:height="20dp"
       android:drawable="@drawable/button_focused" /> <!-- focused -->
 <item android:drawable="@drawable/button_normal" /> <!-- default -->

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84