I've found loads of different ways of accomplishing this but I'm not sure what's the best for my scenario.
This is my Java code for the listview:
ListView lv;
lv = (ListView) findViewById(R.id.favList);
This is the xml code for the list:
<ListView
android:id="@+id/favList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent" >
</ListView>
For a text view I would add:
final Typeface fontList = Typeface.createFromAsset(assets, "optima-extra-black.ttf");
lv.setTypeface(fontList);
But this doesn't work for listviews. How do I change my font in this case?
Oke I'm almost there...
I need to access my assets but I can't from within my custom adapter.
I tried using final AssetManager assets = this.getAssets();
but that won't get me any further..
How to tackle this?
class Myadapter extends BaseAdapter {
LayoutInflater lif;
ImageView sideArrow;
TextView tv;
public Myadapter(Context ctx) {
lif = (LayoutInflater) ctx
.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return favarets.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);
tv = (TextView) vi.findViewById(R.id.textFav);
tv.setText(favarets.get(position));
final AssetManager assets = this.getAssets();
final Typeface tvFont = Typeface.createFromAsset(assets, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
return vi;
<? xml version = "1.0" encoding = "utf-8" ?> Below is the code for the Item layout for displaying the item in the ListView. We have added textColor and textSize attributes to the TextView to change the text color and size.
layout. simple_list_item_1 , (a layout built into Android that provides standard appearance for text in a list), and an ArrayList called restaurants .
In android, An adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like listview, gridview, spinner etc.
The list view itself isn't responsible for drawing the items, it uses an adapter to create the list items. This adapter creates a view to display the list item when required. To change the font used to display the list item, you have to change the adapter to return a view with the new font. This can be done in the Adapter.getView method. If you are currently using a standard Adapter implementation, you may need to subclass it (or completely replace it).
I found out the solution :D
public class Myadapter extends BaseAdapter {
AssetManager assetManager = getAssets();
LayoutInflater lif;
ImageView sideArrow;
TextView tv;
public Myadapter(Context ctx) {
lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return favarets.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);
tv = (TextView) vi.findViewById(R.id.textFav);
tv.setText(favarets.get(position));
final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
return vi;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With