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
Why Can't One Add/Remove Items from an Arrayadapter
How to Programmatically Uninstall a Package in Android
Failure [Install_Failed_Older_Sdk] Android-L
Android Studio Where to Install Ndk File? (Downloaded It in Zip)
Android: Detect Softkeyboard Open
Should Use "Sp" Instead of "Dp" for Text Sizes
How to Raise a Toast in Asynctask, I am Prompted to Used the Looper
Wrong Fragment in Viewpager Receives Oncontextitemselected Call
How to Detect Android CPU Speed
App Getting Stuck with E/Com.Facebook.Internal.Attributionidentifiers
Android Wear App Not Installing Through Handset
Broadcast Receiver Not Working in Android Oreo
Android M Fingerprintmanager.Ishardwaredetected() Returns False on a Samsung Galaxy S5
Using Onclick Attribute in Layout Xml Causes a Nosuchmethodexception in Android Dialogs
Can Not Set Debuggable Flag in Androidmanifest.Xml
How Are Android Activities Handled with Jetpack Compose and Compose Navigation
How to Add "Android Design Support Library" to Eclipse with Adt-Plugin