I have a listview populated like this:
lv1 = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("product", "Bread");
map.put("quantity", "1");
map.put("unit", "pcs");
mylist.add(map);
map = new HashMap<String, String>();
map.put("product", "Books for mom");
map.put("quantity", "14");
map.put("unit", "pcs");
mylist.add(map);
map = new HashMap<String, String>();
map.put("product", "Mineral water");
map.put("quantity", "2");
map.put("unit", "l");
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.grlists,
new String[] {"product", "quantity", "unit"}, new int[] {R.id.CPRODUCT, R.id.CQUANTITY, R.id.CUNIT});
lv1.setAdapter(mSchedule);
This is the layout of the listview (grlists.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/CPRODUCT"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingLeft="5dip"
android:background="#EBDDE2"/>
<TextView android:id="@+id/CQUANTITY"
android:layout_width="30dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:textColor="#000000"
android:background="#B4CFEC"/>
<TextView android:id="@+id/CUNIT"
android:layout_width="30dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:textColor="#000000"
android:background="#A5A1A0"/>
</LinearLayout>
This gives me nullpointerexception because it looks for the CPRODUCT textview in the main layout.
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/PopStarAutograph.ttf");
TextView tv1 = (TextView) findViewById(R.id.CPRODUCT);
tv1.setTypeface(tf);
How can i change the font type of the textviews programmatically?
As Dmytro Danylyk did not add his solution as an answer I add it to close this question. However, in the past months I have mastered this area, so I add a bit different answer as he posted.
First of all, we need arrays to show in a listview. This listview is built upon a custom adapter that receives the items from the arrays. In this custom adapter we can do whatever want with the items in each row of a listview, no matter what kind of elements we are talking about. Be it a textview, imageview, or a checkbox, etc.
Let’s imagine we have 5 elements in a list row: 5 textviews. After populating the 5 arrays, we define our adapter as:
And this is how we set our adapter to the listview:
And finally, we need to create our ListViewCustomAdapter (name it as you want):
As you see this adapter has 5 parameters, just like our 5 arrays. That’s all.
Oh, and we also need to define our adapter as
ListViewCustomAdapter adapter;first.You can have this adapter class in a separate class or in your activity, outside the onCreate() method.