سؤال

i want to change font type on my list view, i follow this answer,

I declare it like this

 lv = (ListView)findViewById(R.id.listView1);
customadapter.CustomBaseAdapter adapter = 
new customadapter.CustomBaseAdapter(this, rowItems, "Van Helsing.ttf");
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);

But nothing happen with my listview font, it won't change.

this my custom adapter:

public CustomBaseAdapter(Context context, List<RowItem> items, String FONT) {
    this.context = context;
    this.rowItems = items;
    tf = Typeface.createFromAsset(context.getAssets(), FONT);
}

What is the probelem with my codes guys ?

EDITED:

Context context;
List<RowItem> rowItems;
Typeface tf;


public CustomBaseAdapter(Context context, List<RowItem> items, String FONT) {
    this.context = context;
    this.rowItems = items;
    tf = Typeface.createFromAsset(context.getAssets(), FONT);
}

/*private view holder class*/
private class ViewHolder {

    TextView txtMenu;


}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtMenu = (TextView) convertView.findViewById(R.id.namaMenu);

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.txtMenu.setText(rowItem.getNamaMenu()); 
    holder.textMenu.setTypeFace(tf);

    return convertView;
}

@Override
public int getCount() {     
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

My Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/namaMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#CC0033"
    android:textSize="32dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

هل كانت مفيدة؟

المحلول

You need to set the font to the Textview of list item

 textView.setTypeface(tf);

tf is the instance of Typeface of your custom font

Example

 @Override
public View getView(int position, View v, ViewGroup parent)
{
    View mView = v ;
    if(mView == null){
        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.textView);
    textView.setTypeface(tf);
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top