Java – How to resize an image in the project selector xml

How to resize an image in the project selector xml… here is a solution to the problem.

How to resize an image in the project selector xml

How to resize a selector item in XML:

<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/check_box_2" />
    <item android:state_checked="true" android:drawable="@drawable/check_box_1" />
    <item android:drawable="@drawable/check_box_2" /> <!-- default state -->
</selector>

My XML is:

<CheckBox
    android:text="llllll"
    android:button="@drawable/custom_checkbox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Solution

You can change it this way:

<item>
    <shape android:shape="rectangle">
        <size android:width="30" android:height="40"/>
        <solid android:color="#000000" />
    </shape>
</item>

Starting at API level 23, you can actually set the width and height of your project

<item
    android:drawable="@drawable/splash_logo"
    android:height="100dp"
    android:width="100dp" />

Note: It is android:width and not android:layout_width

Edit

However, there is another way to achieve the same goal, which is to append an XML: accordingly

Checked.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android=http://schemas.android.com/apk/res/android
    android:shape="rectangle">
    <gradient 
        android:startColor="#ffff0000" 
        android:endColor="#ff000000" 
        android:angle="270" />
    <stroke 
        android:width="4px" 
        android:color="#ffc0c0c0" />
    <size 
        android:height="20dp" 
        android:width="20dp" />
</shape> 

Then attach it as:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@xml/Checked" />
    <item android:state_checked="true" android:drawable="@drawable/check_box_1" />
    <item android:drawable="@drawable/check_box_2" /> <!-- default state -->
</selector>

Note that Checked is in your xml file. You can achieve anything by editing it (accordingly).

Related Problems and Solutions