Custom font for Android listview
Here is the custom adapter class and the constructor
class CustomAdapter extends ArrayAdapter<CharSequence>{
Context context;
int layoutResourceId;
CharSequence data[] = null;
Typeface tf;
public CustomAdapter(Context context, int layoutResourceId, CharSequence[] data, String FONT ) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
tf = Typeface.createFromAsset(context.getAssets(), FONT);
}
Put the font you want to use in your assets folder and fill your listview like this:
listAdapter = new CustomAdapter(this, R.layout.custom_list_text, R.array.abra_hotel, "name_of_font.ttf");
custom font in android ListView
You can't do it that way because the text view resource you pass to the ArrayAdapter is inflated each time it is used.
You need to create your own adapter and provide your own view.
An example for your adapter could be
public class MyAdapter extends BaseAdapter {
private List<Object> objects; // obviously don't use object, use whatever you really want
private final Context context;
public CamAdapter(Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Object obj = objects.get(position);
TextView tv = new TextView(context);
tv.setText(obj.toString()); // use whatever method you want for the label
// set whatever typeface you want here as well
return tv;
}
}
And then you could set that as such
ListView lv = new ListView(this);
lv.setAdapter(new MyAdapter(objs));
Hopefully that should get you going.
Related Topics
How to Configure Launcher Activity Programmatically in Android
Android SQLite Selection Args[] with Int Values
Adb Cannot Start Daemon, Createprocess Failure, Error 2
Differencebetween Sendstickybroadcast and Sendbroadcast in Android
Why So Complex to Set Style from Code in Android
Android: Sample Code to Connect to Vpn in Android 4.0 Using Vpnservice API
Post on Facebook Wall Using Facebook Android Sdk Without Opening Dialog Box
Getlaunchintentforpackage Is Null for Some Apps
Read Contents of a Url in Android
Ndk: How Include *.So Files in Androidstudio
Which Port and Protocol Does Google Cloud Messaging (Gcm) Use
Implementing a Simple Dagger2 Sample
Where Is the All Android Broadcast Intent List
Loading All the Images from Gallery into the Application in Android