Monday, 15 March 2010

android - Highlight TextView when clicked programmatically -



android - Highlight TextView when clicked programmatically -

i dynamically generate textviews work buttons. want highlight them when pressed. alter text color or background color. have tried utilize selector, doesn't work.

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="#ffffff"/> <item android:state_pressed="true" android:state_enabled="false" android:color="#ffffff" /> </selector>

here loop creating textviews.

int z = 0; (mokgenericdataitem d : data) { if (d.getbuttontext() != null) { final int pageposition = z; textview btn = new textview(getactivity()); btn.setid(z); final int id_ = btn.getid(); btn.settext(d.getbuttontext()); btn.settextsize(typedvalue.complex_unit_sp, 30); btn.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.wrap_content, 1f)); btn.setgravity(gravity.center); mlinearelayoutviewpagerbuttons.addview(btn); btn1 = ((textview) view.findviewbyid(id_)); btn1.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { mviewpager.setcurrentitem(pageposition,false); } }); } z++; }

first of line creating ambiguity taking variable name btn1 (which relating button)and taking reference of textview,

btn1 = ((textview) view.findviewbyid(id_));

anyways,go step step,

create xml label_bg.xml next in drawable folder:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/pressed_color" android:state_pressed="true" /> <item android:drawable="@drawable/normal_color" /> </selector>

in values folder create xml following,named labelcolors.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="pressed_color">#7ec0ee</drawable> <!--custom color pressed state --> <drawable name="normal_color">#00ffffff</drawable> <!--transperent color normal state --> </resources>

now set background of label label_bg.xml

<textview android:id="@+id/yourlabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="760dp" android:layout_margintop="515dp" android:background="@drawable/label_bg" <!--like this--> android:text="labeltext" android:textsize="20dp" />

as dynamically adding views need set background each textview programatically .for phone call setbackgroundresource() on textview object created , set label.xml background

android android-textview

No comments:

Post a Comment