Search code examples
javaandroidandroid-drawableandroid-checkedtextview

How do I put arguments into a drawable file (android studio)?


I want to put all of these argument for this checkedtextview into a drawable file so that if I make more checkedtextviews I can use the same arguments the drawable file.

            <CheckedTextView
                android:id="@+id/DrinkWater"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginBottom="2dp"
                android:background="@drawable/startscreenbuttons"
                android:checked="true"
                android:drawableLeft="@drawable/checkbox"
                android:fontFamily="casual"
                android:onClick="do_drink_water"
                android:paddingLeft="3dp"
                android:paddingTop="3dp"
                android:text="@string/action_drink_water"
                android:textColor="#FFFF"
                android:textSize="20sp" />

Basically, how do I make CheckedTextView empty (except to call the drawable) and put it's arguments into this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

</selector>

Solution

  • Use a style, e.g.

    <style name="MyCheckedTextView" parent="@android:style/Widget.Material.CheckedTextView">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">35dp</item>
        <item name="android:layout_marginBottom">2dp</item>
        <item name="android:background">@drawable/startscreenbuttons</item>
        <item name="android:checked">true</item>
        <item name="android:drawableLeft">@drawable/checkbox</item>
        <item name="android:fontFamily">casual</item>
        <item name="android:paddingLeft">3dp</item>
        <item name="android:paddingTop">3dp</item>
        <item name="android:textColor">#FFFF</item>
        <item name="android:textSize">20sp</item>
    </style>
    

    and apply it to your view

    <CheckedTextView
                android:id="@+id/DrinkWater"
                style="@style/MyCheckedTextView"
                //...
    

    Also put your values in dimens.xml and colors.xml. It allows better layout management later in the project.

    dimens.xml

    <resources>
        <dimen name="dimen_name">5dp</dimen>
    </resources>
    

    colors.xml

    <resources>
        <color name="color_name">#AABBCCDD</color> 
    </resources>