Dynamically create CheckedTextView

465 views Asked by At

Below is my code:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:checkMark="@drawable/checking_icon"
    android:orientation="vertical"
    android:padding="8dp"
    android:textSize="18sp" />

For @drawable/checking_icon in the above xml:

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

    <item android:drawable="@drawable/enable_32" android:state_checked="true"/>
    <item android:drawable="@drawable/disable_32"/>

</selector>

How to create the above 2 xmls progrmatically ?

1

There are 1 answers

1
Hussain Mansoor On

You can simply create your CheckedTextView from XML file by inflating it like

LayoutInflater.from(context).inflate(R.layout.myfile, null);

and that should be it. If you want to set the drawable do something like this

checkedTextView.setCheckMarkDrawable(getResources().getDrawable(R.drawable.filename));