How can I define the size of the drawables in the style file?

34,352

There is a weak solution, weak in the sense that android:width and android:height still appear four times, but at least the dimension can be adjusted by setting just one parameter.

For each of the four drawables, add

<size android:width="@dimen/my24dp"
      android:height="@dimen/my24dp" />

and also define the dimension

res/values/dimens.xml

<dimen name="my24dp">24dp</dimen>

So the question remains: Can the size not be specified at all in the drawables?

Share:
34,352
Calaf
Author by

Calaf

Updated on July 13, 2022

Comments

  • Calaf
    Calaf almost 2 years

    I would like to specify just once in res/values/styles.xml the size of two checkboxes and their four shape drawables

    1. res/drawable/cb1_checked.xml
    2. res/drawable/cb1_unchecked.xml
    3. res/drawable/cb2_checked.xml
    4. res/drawable/cb2_unchecked.xml.

    This way the size would appear one time in the style rather than four in the drawables.

    Neither of the two attempts below work. Can you suggest a solution? (If you see what's wrong with these two attempts, please do mention it.)

    ----------------------------------------------------------------Attempt 1----------------------------------------------------------------

    Replace

    res/drawable/cb1_checked.xml

    <size android:width="24dp"
          android:height="24dp" />
    

    with

    res/values/styles.xml

    <item name="android:layout_width">24dp</item>
    <item name="android:layout_height">24dp</item>
    

    ----------------------------------------------------------------Attempt 2----------------------------------------------------------------

    Add

    res/values/styles.xml

    <item name="android:background">cb_background</item>
    

    and define a background drawable

    res/drawable/cb_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#00FF00"/>
        <size android:width="24dp"
              android:height="24dp" />
    </shape>
    

    ----------------------------------------------------------------Added----------------------------------------------------------------

    Here is the full set of XML files:

    res/layout/activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/MyLL"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <CheckBox
            android:id="@+id/cb1"
            style="@style/CB_style"
            android:button="@drawable/cb1_selector"
            android:height="@dimen/my24sp" />
    
        <CheckBox
            android:id="@+id/cb2"
            style="@style/CB_style"
            android:button="@drawable/cb2_selector"
            android:height="@dimen/my24sp" />
    
    </LinearLayout>
    

    res/values/styles.xml

    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>
    
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
    
    <style name="CB_style" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:gravity">center</item>
        <item name="android:checked">false</item>
    </style>
    

    res/drawable/cb1_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
              android:constantSize="true">
    
        <item android:state_checked="true"
              android:drawable="@drawable/cb1_checked" />
    
        <item android:state_checked="false"
              android:drawable="@drawable/cb1_unchecked" />
    
    </selector>
    

    res/drawable/cb1_checked.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#FF0000"/>
        <size android:width="24dp"
              android:height="24dp" />
    </shape>
    

    res/drawable/cb1_unchecked.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#880000"/>
        <size android:width="24dp"
              android:height="24dp" />
    </shape>
    

    (and similarly for the cb2 selector and checked/unchecked drawables)

  • longi
    longi almost 11 years
    really? what's the error? after you create the new dimens.xml you should clean your project.. than java is creating the reference id to get the value with your @dimen/checkbox_width
  • Calaf
    Calaf almost 11 years
    There is no error. Nothing appears in either Eclipse or the device. Could you look at the full set of files that I have now added and see if there is something that makes your solution not work on my side?
  • Toby Wilson
    Toby Wilson over 8 years
    I'm doing literally the same thing and this answer works. OP should accept this answer.
  • roroinpho21
    roroinpho21 almost 8 years
    I did the same thing but it isn't work. I have the following error Error:Error: Width (0) and height (0) cannot be <= 0. I have min skd tarket set to 19.
  • Calaf
    Calaf almost 8 years
    @roroinpho21 It sounds simple, but anything one could suggest here would be just a guess. Double-check and post a question.