android - Programatically setColor,font to Textviews -
i have created list view 3 items in each list view item using 2 seperate .'xml's. 1)list_view.xml
<listview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview"> </listview>
2)list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="text here" android:id="@+id/textview" android:layout_aligntop="@+id/textview" android:layout_alignleft="@+id/textview" android:layout_centervertical="true"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="-" android:id="@+id/seperator" android:layout_alignbottom="@+id/textview" android:layout_centervertical="true" android:layout_centerhorizontal="true"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="000" android:id="@+id/price" android:layout_alignparenttop="true" android:layout_centervertical="true" android:layout_alignparentright="true"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="$" android:id="@+id/prefix" android:layout_alignparenttop="true" android:layout_centervertical="true" android:layout_toleftof="@+id/price"/> </relativelayout>
now, have created list view items using 'simpleadapter'. want access items in list view (textview, seperator,...) , change color, size etc programatically.
here code of mainactivity.java
public class mainactivity extends activity { public relativelayout relativelayout; public relativelayout.layoutparams lp; public simpleadapter mlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list_view); listview list = (listview) findviewbyid(r.id.listview); //background color list.setbackgroundcolor(color.yellow); textview itemtext = (textview)findviewbyid(r.id.textview); itemtext.settypeface(typeface.sans_serif); arraylist<hashmap<string, string>> mylist = new arraylist<hashmap<string, string>>(); hashmap<string, string> map = new hashmap<string, string>(); //dummy data //add elements map map.put("text", "click edit0"); map.put("seperator", "-"); map.put("price", "100"); // add map elements list mylist.add(map); //add elements map map = new hashmap<string, string>(); map.put("text", "click edit1"); map.put("seperator", "-"); map.put("price", "102"); //add elements map mylist.add(map); map = new hashmap<string, string>(); map.put("text", "click edit2"); map.put("seperator", "-"); map.put("price", "104"); mylist.add(map); //use adapter set list items mlist = new simpleadapter(this, mylist, r.layout.list_item, new string[] {"text", "seperator", "price"}, new int[] {r.id.textview, r.id.seperator, r.id.price}); list.setadapter(mlist); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; }
}
all worked fine until tried access list view items , change them. code crashes , shows null pointer exception @
textview itemtext = (textview)findviewbyid(r.id.textview); itemtext.settypeface(typeface.sans_serif);
i suspect might because did not setcontentview list_item.xml. there way access elements? if yes how?
for setting color follow bellow code
string text = "<font color=#8469af>by tapping checkout, agree our </font> <font color=#ffffff>terms.</font>"; tvterms.settext(html.fromhtml(text));
Comments
Post a Comment