0

my problem is that any text views inside any items of all recyclerview don't take its font from XML, just shows the original font of the android

only items of all recycler views have this issue, other text views work fine

I have a lot of recycler view around 23 rv and there is more so it's not a good thing to change every text font programmatically I tried many things with no luck here any help?

this is an example: Adaptor view

import android.content.Context
import android.util.Log

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.ecom.R
import com.ecom.data.model.FabricProduct
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.item_fabric_product.view.*


class CollectionAdaptor(var mList: List<FabricProduct>, var context: Context, val listener: (FabricProduct) -> Unit
) : RecyclerView.Adapter<CollectionViewHolder>() {

    fun updateList(newitems: List<FabricProduct>) {
        mList = newitems
        notifyDataSetChanged()
   }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectionViewHolder {return CollectionViewHolder( LayoutInflater.from(context).inflate(R.layout.item_fabric_product,parent,false))
}

    override fun onBindViewHolder(holder: CollectionViewHolder, position: Int) {
        holder.bind(mList[position], listener)

    }

    override fun getItemCount(): Int {
        return mList.size
    }}

class CollectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bind(item: FabricProduct, listener: (FabricProduct) -> Unit) = with(itemView) {
        itemView.text_subcategorie_slogan.text = item.slogan
        itemView.text_subcategorie_title.text = item.title
        setOnClickListener { listener(item) }
    }}

recyclerview setup

mFabricProductlist = FakeData.getFabricCollection()

            rootView.rv_collection_subcategory.layoutManager = GridLayoutManager(activity!!.applicationContext, 2)

            mCollectionAdaptor =
                CollectionAdaptor(mFabricProductlist as List<FabricProduct>, activity!!.applicationContext) { //get the selected item here}

     rootView.rv_collection_subcategory.adapter = mCollectionAdaptor

inside a fragment_collection.xml

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_collection_subcategory"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    />

item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="15dp"
    android:layout_marginBottom="4dp"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="10dp"
        android:layout_marginStart="4dp"
        android:orientation="vertical">


        <TextView
            android:id="@+id/text_subcategorie_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/montserrat_bold"
            android:shadowColor="#000"
            android:shadowDy="1"
            android:shadowRadius="1"
            android:text="000"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/text_subcategorie_slogan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/montserrat"
            android:shadowColor="#000"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:text="000"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="12sp" />


    </LinearLayout>

</FrameLayout>

build.gradle

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso- 
    core:3.2.0-alpha03'
    //Navigation
    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Amr263
  • 143
  • 2
  • 8
  • remove `android:fontFamily="@font/montserrat_bold"` from your xml file – Dinesh Shingadiya May 31 '19 at 05:04
  • Possible duplicate of [How to set custom font for a whole application in Android?](https://stackoverflow.com/questions/33923803/how-to-set-custom-font-for-a-whole-application-in-android) – Manoj Perumarath May 31 '19 at 05:07
  • You are not using the `xml` for item that you have posted... **Your viewholder has got 3 textViews while your xml contains only 2** – Santanu Sur May 31 '19 at 05:20
  • @SantanuSur sorry I fixed it here thanks – Amr263 May 31 '19 at 05:33
  • @ManojPerumarath thanks, I tried this before, anyway I have 3 fonts not only one, so it will be the same issue the problem is if I attached a font for specific textview inside an item of recyclerview , it didn't change any thing just the original font. you get me? – Amr263 May 31 '19 at 05:38

2 Answers2

1

use AppCompatTextView instead of TextView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="15dp"
    android:layout_marginBottom="4dp"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="10dp"
        android:layout_marginStart="4dp"
        android:orientation="vertical">


        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text_subcategorie_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:fontFamily="@font/montserrat_bold"
            android:shadowColor="#000"
            android:shadowDy="1"
            android:shadowRadius="1"
            android:text="000"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text_subcategorie_slogan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:fontFamily="@font/montserrat"
            android:shadowColor="#000"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:text="000"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="12sp" />


    </LinearLayout>

</FrameLayout>
hamid keyhani
  • 451
  • 3
  • 12
0

it's working now, the problem was

mCollectionAdaptor =
                CollectionAdaptor(mFabricProductlist as List<FabricProduct>, activity!!.applicationContext) { //get the selected item here}

I passed applicationContext of the activity so the solution just passes the activity

 mCollectionAdaptor =
                    CollectionAdaptor(mFabricProductlist as List<FabricProduct>, activity!!) { //get the selected item here}

thanks

Amr263
  • 143
  • 2
  • 8