Custom TextView XML Drawable with backround support

I am using Horizontal ListView

(list contains only TextView

) and now I would like to use xml drawable to show line

below the list item user taps

.

As we usually use in Sliding Tabs

, below is a screenshot , in this example the user selected Tab1 and Tab2 and Tab3 are in normal state.

enter image description here

Likewise, I would like to implement custom drawable

mine to get the same effect with mine TextView

, and for that I follow this .

But still I didn't get any success, see TextView property:

<TextView
    android:text="@string/app_name"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"        
    android:layout_margin="5dp"
    android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:background="@drawable/custom_textview"
    android:ellipsize="marquee" />

      

custom_textview.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#ffff7a00" />
                </shape>
            </item>
            <item android:bottom="3dp">
                <shape>
                    <solid android:color="#222222" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

      

+3


source to share


2 answers


Replace your custom_textview.xml with this code



 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ffff7a00" />
        </shape>
    </item>

    <item
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#222222" />
        </shape>
    </item>
</layer-list>

      

0


source


you can do it using simple create row.xml logic

<LinearLayoutadd attributes with orientation vertical>
<TextView/>
<View android:id="@+id/checkId/>
</LinearLayout/>

      

create model (POJO)

public class MyModel{
String name;
boolean isChecked;
public MyModel(String name,boolean isChecked){
this.name=name;
this.isChecked = isChecked;
}
//apply getters and setters
}

      



now create an adapter in the getView method check if the isChecked value is true or false, if true sets the background color of the view whose checkId will still change it to white

Now go to your OnItemClickListener use for loop to make sure the item was clicked

for(int i = 0 ;i < youlist.size;i++){
yourlist.get(i).setIsChecked(false);
}
yourlist.get(positionoflistitem).setIsChecked(true);
youradapter.notifyDataSetChanged();

      

made by you

0


source







All Articles