Custom Font In Android Listview
I'm using a custom font throughout my application (which, incidentally, I've frustratingly found out that you have to apply programmatically by hand to EVERY control!), and I need
Solution 1:
If you don't want to create a new class you can override the getView method when creating your Adapter, this is an example of a simpleAdapter with title and subtitle:
TypefacetypeBold= Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
TypefacetypeNormal= Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");
SimpleAdapteradapter=newSimpleAdapter(this, items,R.layout.yourLvLayout, newString[]{"title",
"subtitle" }, newint[] { R.id.rowTitle,
R.id.rowSubtitle }){
@Overridepublic View getView(int pos, View convertView, ViewGroup parent){
Viewv= convertView;
if(v== null){
LayoutInflatervi= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.yourLvLayout, null);
}
TextViewtv= (TextView)v.findViewById(R.id.rowTitle);
tv.setText(items.get(pos).get("title"));
tv.setTypeface(typeBold);
TextViewtvs= (TextView)v.findViewById(R.id.rowSubtitle);
tvs.setText(items.get(pos).get("subtitle"));
tvs.setTypeface(typeNormal);
return v;
}
};
listView.setAdapter(adapter);
where items is your ArrayList of Maps
hope that helps
Solution 2:
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
publicclassMyAdapterextendsBaseAdapter {
private List<Object> objects; // obviously don't use object, use whatever you really wantprivatefinal Context context;
publicCamAdapter(Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
}
@OverridepublicintgetCount() {
return objects.size();
}
@Overridepublic Object getItem(int position) {
return objects.get(position);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Objectobj= objects.get(position);
TextViewtv=newTextView(context);
tv.setText(obj.toString()); // use whatever method you want for the label// set whatever typeface you want here as wellreturn tv;
}
}
And then you could set that as such
ListViewlv=newListView(this);
lv.setAdapter(newMyAdapter(objs));
Hopefully that should get you going.
Solution 3:
Looks like the constructor is wrong
change it to:
public MyAdapter (Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
}
it worked well for me.
Solution 4:
Try like this for arrayadapters::
TypefacetypeNormal= Typeface.createFromAsset(getAssets(), "roboto_lite.ttf");
timearray = newArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor) {
public View getView(int pos, View convertView, android.view.ViewGroup parent) {
Viewv= convertView;
if (v == null) {
LayoutInflatervi= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.floorrow, null);
}
TextViewtv= (TextView)v.findViewById(R.id.txt);
tv.setText(flor.get(pos));
tv.setTypeface(typeNormal);
return v;
};
};
lv_building.setAdapter(timearray);
Solution 5:
In addition to the response of Moisés Olmedo - an alternative variant without creating a new class:
tf = Typeface.createFromAsset(getAssets(), fontPath);
recordsAdapter = newSimpleCursorAdapter(this, R.layout.item1, cursor, from, to);
recordsAdapter.setViewBinder(newSimpleCursorAdapter.ViewBinder() {
publicbooleansetViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == 1) {
finalTextViewtv= (TextView) view;
tv.setTypeface(tf);
}
returnfalse;
}
});
Post a Comment for "Custom Font In Android Listview"