java - Help me change TextView color dynamically? -
i know how change color of textview dynamically. seems there stuff out there quite i'm looking for.. i'm doing here iterating array , if "+" want text greem if "-" want text red, problem i'm getting null pointer error @ ct.settextcolor(.....) have idea why?
here xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listpreferreditemheight" android:padding="6dip"> <imageview android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginright="6dip" android:src="@drawable/icon" /> <linearlayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="fill_parent"> <textview android:id="@+id/tt" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_vertical" /> <textview android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:id="@+id/bt" android:singleline="true" android:ellipsize="marquee" /> </linearlayout> <textview android:id="@+id/pm" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:text="-" android:textsize="30sp" android:textstyle="bold" /> </linearlayout>
and code snippit
int c = count; ind = (count); j=0; textview ct = (textview)findviewbyid(r.id.pm); funkadapter = (new arrayadapter<string>(this, r.layout.rowneg, r.id.pm)); string[] tmpary= new string[count]; (int = 0; < ind; = i+2){ tmpary[j] = dataaryarray[i]; if (tmpary[j].equals("-")){ funkadapter.add(tmpary[j]); ct.settextcolor(color.red); }else{ funkadapter.add(tmpary[j]); ct.settextcolor(color.green); } j++; } setlistadapter(funkadapter);
if code snippet above copied-and-pasted out of oncreate() method, main problem see why it's giving null pointer error because haven't set current activity's view xml layout stuff in it. in other words, findviewbyid() doesn't know find id's you're referring to. try adding first line of oncreate(), or anywhere before call findviewbyid():
setcontentview(r.id.yourlinearlayout);
Comments
Post a Comment