JazuliGh JazuliGh - 1 month ago 15
Android Question

How To Set Costum Font in ListViewAdapter Android Studio?

Sorry, I'm beginner, and sorry about my english.

I want to set costum font and font style on my listviewAdapter.
I Was tried some modivication on this code, but all fail.

How to set costum font in my ListviewAdapter??

this is my code

package com.istinbat.listview;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {

Context context;
ArrayList<String> dataNo;
ArrayList<String> dataPetunjuk;
ArrayList<String> dataTeks;
ArrayList<String> dataLatin;
ArrayList<String> dataArti;


public ListAdapter(
Context context2,
ArrayList<String> no,
ArrayList<String> petunjuk,
ArrayList<String> teks,
ArrayList<String> latin,
ArrayList<String> arti
)
{

this.context = context2;
this.dataNo = no;
this.dataPetunjuk = petunjuk;
this.dataTeks = teks;
this.dataLatin = latin ;
this.dataArti = arti ;
}

public int getCount() {
TODO Auto-generated method stub
return dataNo.size();
}

public Object getItem(int position) {
TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
TODO Auto-generated method stub
return 0;
}

public View getView(int position, View child, ViewGroup parent) {

Holder holder;

LayoutInflater layoutInflater;

if (child == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);

holder = new Holder();

holder.tvNo = (TextView) child.findViewById(R.id.No);
holder.tvPetunjuk = (TextView) child.findViewById(R.id.petunjuk);
holder.tvTeks = (TextView) child.findViewById(R.id.Teks);
holder.tvLatin = (TextView) child.findViewById(R.id.latin);
holder.tvArti = (TextView) child.findViewById(R.id.arti);

child.setTag(holder);

} else {

holder = (Holder) child.getTag();
}

holder.tvNo.setText(dataNo.get(position));
holder.tvPetunjuk.setText(dataPetunjuk.get(position));
holder.tvTeks.setText(dataTeks.get(position));
holder.tvLatin.setText(dataLatin.get(position));
holder.tvArti.setText(dataArti.get(position));

return child;
}

public class Holder {
TextView tvNo;
TextView tvPetunjuk;
TextView tvTeks;
TextView tvLatin;
TextView tvArti;
}


}

Answer

Make sure your font located in assets folder in your project assets/fonts

public View getView(int position, View child, ViewGroup parent) {

    Holder holder;

    LayoutInflater layoutInflater;

    if (child == null) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        child = layoutInflater.inflate(R.layout.row, null);

        holder = new Holder();

        holder.tvNo = (TextView) child.findViewById(R.id.No);
        holder.tvPetunjuk = (TextView) child.findViewById(R.id.petunjuk);
        holder.tvTeks = (TextView) child.findViewById(R.id.Teks);
        holder.tvLatin = (TextView) child.findViewById(R.id.latin);
        holder.tvArti = (TextView) child.findViewById(R.id.arti);

       Typeface myTypeface = Typeface.createFromAsset(context.getAssets(),"fonts/myFontName.ttf");

       //set here the type to you desired textview
       holder.tvArti.setTypeface(myTypeface);

        child.setTag(holder);

    } else {

        holder = (Holder) child.getTag();
    }

    holder.tvNo.setText(dataNo.get(position));
    holder.tvPetunjuk.setText(dataPetunjuk.get(position));
    holder.tvTeks.setText(dataTeks.get(position));
    holder.tvLatin.setText(dataLatin.get(position));
    holder.tvArti.setText(dataArti.get(position));

    return child;
}