Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to set a custom font to each individual ListAdapter button

I have a list adapter and im using holder.text.setTypeface(TYPEFACE) to set the type face and I have a string of names that im using assign each ListView button its text. But im wondering how to set just one individual list item a custom font. Basically say I have the 3 ListAdapter buttons, Button 1, Button 2, and Button 3. I want Button 2 to be a custom font while Button 1 and Button 3 Stay regular. How can this be done?

like image 840
Joe Avatar asked Dec 29 '22 03:12

Joe


2 Answers

Create class that extends ArrayAdapter and set the font in the getView procedure.

like image 130
Asaf Pinhassi Avatar answered Jan 12 '23 04:01

Asaf Pinhassi


There are a couple ways to do this my favorite two are to set it in the XML layout. This will work if the same custom font is desired on every "Button2" and if the Typeface you want is already part of the system.

<TextView 
android:id="@+id/button2"
android:layout_height="wrap_content" 
android:layout_width="wrap_content"
android:typeface="sans" />

The second way to do this is to customize a ListAdapter with a ViewBinder. As Pinassi wrote, it is possible to do from getView() however this has the drawback of not being able to easily see data and mixes generic UI code with custom UI code. Both SimpleAdapter and SimpleCursorAdapter have an inner class named ViewBinder with method setViewValue. Each signature is different because of the underlying data it manages.

Simply override setViewValue for only the customization you want to manually control. For example is R.id.button1 is a simple text binding just have the method return false and the API will handle it for you but since we're customizing R.id.button2 we will return true.

switch(v.getId){
case R.id.button2:
   TextView button2 = (TextView) v;
   button2.setTypeface(customTypeFace);
   // bind text here or return false to allow the API to do this for you.
break;
// other customization
}

customTypeFace comes from a system Typeface or the project's assets folder. Android uses TrueType Fonts.

like image 26
Dan S Avatar answered Jan 12 '23 03:01

Dan S