android – Bottom Sheet Fragment cannot show the language list

I like to show a language list when I press the button. I use dependency injection on the bottom sheet fragment. But it cannot show the language list. Is there something I miss? Thanks.

I try to use the following sample to learn the concept of hilt and digger. https://www.geeksforgeeks.org/dagger-hilt-in-android-with-example/

KC

** AppModule **

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    fun provideTestString() = "This is a string we will inject"

    @Provides
    @Singleton
    fun provideLanguageRepository(): LanguageRepository = LanguageRepositoryImpl()
}


** BottomSheetFragment **

@AndroidEntryPoint
class BottomSheetFragment : BottomSheetDialogFragment(){

    private lateinit var binding: BottomSheetBinding
    private lateinit var languageList: RecyclerView

    @Inject
    lateinit var testString: String

    private val viewModel: LanguageViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = BottomSheetBinding.inflate(inflater, container, false)
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        languageList = binding.languageList
        languageList.layoutManager = LinearLayoutManager(context)

        observeLanguage()
    }



    private fun observeLanguage() {
        viewModel.language.observe(this) {
            languageList.adapter = LanguageAdapter(it)
        }
    }
}


** Language **

data class Language(
    val isChecked: Boolean,
    val language: String
)

** Language Adapter **

class LanguageAdapter(private val language: List<Language>) :
    RecyclerView.Adapter<LanguageAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LanguageAdapter.ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_language, parent, false)
        return ViewHolder(view)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(index:Language){
       

 itemView.findViewById<CheckBox>(R.id.isChecked).isChecked = index.isChecked
        itemView.findViewById<TextView>(R.id.language).text = index.language
        }
    }

    override fun getItemCount(): Int = language.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(language[position])
    }


}

** Language Repository **

interface LanguageRepository {
    fun getLanguage(): List<Language>
}

** Language Repository Implementation **

class LanguageRepositoryImpl : LanguageRepository {

    override fun getLanguage() = listOf(
        Language(true, "English"),
        Language(true, "Spanish"),
        Language(false, "Korean"),
        Language(false, "Japanese"),
        Language(false, "Hindi"),
    )
}

** Language View Model **

@HiltViewModel
class LanguageViewModel @Inject constructor(
    private val languageRepository: LanguageRepository
) : ViewModel() {

    private val languageEmitter = MutableLiveData<List<Language>>()
    val language: LiveData<List<Language>> = languageEmitter

    init {
        loadLanguage()
    }

    private fun loadLanguage(){
        languageEmitter.value = languageRepository.getLanguage()
    }
}

** Main Activity **

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val bottomSheetFragment = BottomSheetFragment()

        binding.btnShow.setOnClickListener{
            bottomSheetFragment.show(supportFragmentManager, "bottom_sheet")
        }

    }
}

** MyApplication **

@HiltAndroidApp
class MyApplication : Application(){
}

** activity main **

<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

** Bottom Sheet ** 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

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

</LinearLayout>

** item language ** 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/isChecked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:minWidth="48dp"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/language"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/isChecked"
        android:layout_alignBottom="@+id/isChecked"
        android:layout_toEndOf="@+id/isChecked"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:maxLines="1"
        tools:text="English" />


</RelativeLayout>


** I have the following error  **



  2022-07-21 12:29:13.664 22674-22674/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2022-07-21 12:29:13.665 22674-22674/? E/Zygote: accessInfo : 1
2022-07-21 12:29:13.695 22674-22674/? E/omsheetwithhil: Unknown bits set in runtime_flags: 0x8000
2022-07-21 12:30:04.312 22674-22674/com.example.testbottomsheetwithhilt E/DecorView: mWindow.mActivityCurrentConfig is null

Leave a Comment