Skip to content Skip to sidebar Skip to footer

Set Font For Listview, Android

I spent all day searching for an answer, found a lot, but all of them are not what I need. So, I have a pretty simple menu with ListView and a file for items with TextView. I want

Solution 1:

view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
                .getContext().getAssets(), "fonts/DroidSerif.ttf"));

Solution 2:

here is code for custom adapter:

privateclassCostumiseAdapterextendsArrayAdapter<item>{

publicCostumiseAdapter(ArrayList<item> items) {
 super(getActivity(), 0, items);
}

@Overridepublic View getView(int position, View convertView, ViewGroup parent){
 if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.scroll_fragment, null);
}

Itemitem= getItem(position);

TextViewtext= (TextView) convertView.findViewById(R.id.text_view);
text.setText(item.mString);

RadioButtonb1= (RadioButton) convertView.findViewById(R.id.radio_b1);
b1.setChecked(item.mB1);

RadioButtonb2= (RadioButton) convertView.findViewById(R.id.radio_b2);
b2.setChecked(item.mB2);

CheckBoxcB= (CheckBox) convertView.findViewById(R.id.check_box);
cB.setChecked(item.mB2);

return convertView;
}
}

I've take it from http://androide-examples.blogspot.com/2013/11/android-scroll-list.html see for the full example.

In this example are some semantik mistakes. So you have to fix some items to Items (capitalise)...

Solution 3:

You need to create a custom adapter for setting custom font. Here is the minimal code :

publicclassCustomAdapterextendsArrayAdapter<String> {
   //Contructor herepublic View getView(int position, View convertView, ViewGroup parent) {
              Viewview=super.getView(position, convertView, parent);
              TextViewtextView= (TextView) view.findViewById(R.id.myTextView);
             // Here you set the typeface as you do for the TextView 
              textView.setTypeFace(....);
       }
  }

That should provide you the custom font for your Listview.

Solution 4:

I found a desition! Well, first of all I create another class for Adapter:

publicclassTypefaces{
    privatestaticfinal Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
    publicstatic Typeface get(Context c, String name) {
    synchronized (cache) {
    if (!cache.containsKey(name)) {
    String path = "fonts/"+name;
    try {
    Typeface t = Typeface.createFromAsset(c.getAssets(), path);
    cache.put(name, t);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return cache.get(name);
    }
    }
    }

Then in the main Adapter I added two strings:

Typeface tf = Typefaces.get(getContext(), "tahoma.ttf");
        holder.txtTitle.setTypeface(tf);

That's all! It's working! )) Thank you all for help and answers.

Solution 5:

try in xml:

android:fontFamily="somefont.ttf"

From android 4.1+ the following font are available: http://robotofont.com/

android:fontFamily="sans-serif"android:fontFamily="sans-serif-thin"android:fontFamily="sans-serif-light"android:fontFamily="sans-serif-condensed"

you can also set font programaticly:

TextViewt= (TextView) findViewById(R.id.name);
Typefacefont= Typeface.createFromAsset(getAssets(),"font/somefont.ttf");
t.setTypeface(font);

put the font file in a folder...

Post a Comment for "Set Font For Listview, Android"