Android change the appearance and color of the font

I found many different ways to accomplish this, but am not sure which is better for my scenario.

This is my Java code for the list:

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 does not work for listviews. How to change the font in this case?

Okay, I'm almost there ... I need to access my assets, but I can’t inside my adapter. I tried using final AssetManager assets = this.getAssets(); but it will not improve me.

How to solve 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; 
+4
source share
4 answers

I found a 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; } } 
+6
source

The list view itself is not responsible for drawing items; it uses an adapter to create list items. This adapter creates a view to display the list item when necessary. To change the font used to display the list item, you need to change the adapter to return the view with the new font. This can be done in Adapter.getView . If you are currently using the standard adapter implementation, you may need to subclass (or completely replace it).

+5
source

you need to create a custom adapter .

check this answer

and then custom xml too.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textView" android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/> </LinearLayout> 

then install the custom adapter in listview .

 listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList); mListView.setAdapter(listAdapter); 
0
source

Just install it in the TextView, which you are inflating. Install the font in the adapter or in the xml layout.

0
source

All Articles