Change Font on ListView


#1

I was looking for a lot on the internet to solve my problem but I did not find a good solution.

GOAL: How to change font in ListView?

XML layout code

<ListView
    android:id="@+id/tvListDrinki"
    android:layout_width="match_parent"
    android:layout_height="444dp"
    android:background="@color/googleWhite"
    android:layout_marginTop="196dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.518"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

List View “Ticket” Code

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_lista_drinkow"
    android:orientation="horizontal">

    <ImageView
        android:layout_marginLeft="20dp"
        android:id="@+id/ivDrinkImage"
        android:layout_width="50pt"
        android:layout_height="50pt"
        android:layout_weight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/martini" />

    <LinearLayout
        android:layout_width="90pt"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Martini"
            android:textColor="@color/white"
            android:textSize="24dp"
            android:textStyle="bold" />

    </LinearLayout>
</LinearLayout>

Kotlin Code

class Drinki : AppCompatActivity() {



var listOfDrinks = ArrayList<DrinkiLista>()
var adapter: DrinksAdpater? =null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    setContentView(R.layout.activity_drinki)



    //wczytaj drinki
    listOfDrinks.add(DrinkiLista("Blood Mary", R.drawable.bloodmary))
    listOfDrinks.add(DrinkiLista("Black Russian", R.drawable.blackrussian))
    listOfDrinks.add(DrinkiLista("Blue Hawaii", R.drawable.bluehawai))
    listOfDrinks.add(DrinkiLista("Coconut Cocktail", R.drawable.coconat))
    listOfDrinks.add(DrinkiLista("Cosmopolitan", R.drawable.cosmopolitan))
    listOfDrinks.add(DrinkiLista("Kir Royal", R.drawable.kirroyal))
    listOfDrinks.add(DrinkiLista("Mai Tai", R.drawable.maitai))
    listOfDrinks.add(DrinkiLista("Manhatan", R.drawable.manhatan))
    listOfDrinks.add(DrinkiLista("Margarita", R.drawable.margarita))
    listOfDrinks.add(DrinkiLista("Martini", R.drawable.martini))
    listOfDrinks.add(DrinkiLista("Mimosa", R.drawable.mimosa))
    listOfDrinks.add(DrinkiLista("Mint Julep", R.drawable.mintjulep))
    listOfDrinks.add(DrinkiLista("Mojito", R.drawable.mojito))
    listOfDrinks.add(DrinkiLista("Pina Colada", R.drawable.pinacolada))
    listOfDrinks.add(DrinkiLista("Sex On The Beach", R.drawable.sexonthebeach))
    listOfDrinks.add(DrinkiLista("Strawberry Mojito", R.drawable.stawberymohito))




    adapter = DrinksAdpater(this,listOfDrinks)
    tvListDrinki.adapter = adapter

}

class DrinksAdpater:BaseAdapter{

    var listOFDrinks = ArrayList<DrinkiLista>()
    var context:Context?=null
    constructor(context:Context, listOFDrinks: ArrayList<DrinkiLista>):super(){

        this.listOFDrinks=listOFDrinks
        this.context=context

    }

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {

        val drink= listOFDrinks[p0]
        var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var myView = inflator.inflate(R.layout.drinki_ticket,null)
        myView.tvName.text= drink.name!!
        myView.ivDrinkImage.setImageResource(drink.image!!)
        return myView

    }

    override fun getItem(p0: Int): Any {
        return listOFDrinks[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return listOFDrinks.size
    }

}