Trying to set a custom font for 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 use, assigning each ListView button its own text. But they are wondering how to set only one separate list item as their own font. Basically they say that I have 3 ListAdapter buttons, button 1, button 2 and button 3. I want button 2 to be a custom font, but buttons 1 and button 3 remain regular. How can I do that?

+4
source share
2 answers

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

+9
source

There are several ways to do this, my favorite two are to set it in an XML layout. This will work if each "Button2" needs the same custom font, 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 the ListAdapter using the ViewBinder . As Pinassi wrote, this can be done from getView() , however this has the disadvantage that it cannot easily see the data and mixes the general user interface code with the user interface user code. Both SimpleAdapter and SimpleCursorAdapter have an inner class called ViewBinder with the setViewValue method. Each signature is different in that it manages the underlying data.

Just override setViewValue just for the setting you want to manually configure. For example, R.id.button1 is a simple text binding, just the method returns false, and the API will handle it for you, but since we are setting 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 Typeface system or a project resource folder . Android uses TrueType Fonts.

+1
source

Source: https://habr.com/ru/post/1333924/


All Articles