5
votes

I have a custom layout which has a textview and ratingbar , and then i set the layout as view to my preferences , the problem is that i tried to inflate the view in my preferences fragment and set the ratingbar onclick , i put a log to see if it is clicked or not but nothing is happening

  • This is my preference
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference
        android:title="@string/YourPreferredLanguage"
        android:icon="@drawable/ic_baseline_language_24" />

    <CheckBoxPreference
        android:title="@string/french"
        android:key="french"
        />
   
    <CheckBoxPreference
        android:title="@string/portuguese"
        android:key="portuguese"
        />

    <Preference
        android:title="Rate Our App"
        android:icon="@drawable/ic_baseline_stars_24"
        />

    <PreferenceCategory
        android:layout="@layout/ratinglayout"/> //this is my custom layout

</PreferenceScreen>
  • This is my layout
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">

    <TextView
        android:id="@+id/ratingtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="75dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="20dp"
        android:text="Rate "
        android:textColor="@android:color/black"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:numStars="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.75"
        app:layout_constraintStart_toEndOf="@+id/ratingtext"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

*Snippet of code of how i m implementing it

class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
             setPreferencesFromResource(R.xml.settingspreferences,rootKey)

val view = LayoutInflater.from(requireContext()).inflate(R.layout.ratinglayout,null,false)
            val ratingbars = view.findViewById<RatingBar>(R.id.ratingBar)
            ratingbars.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
                Log.d("TAG","Rating is " + rating) // when i click on rating bar , nothing happens
}
}

Any help would be appreciated guys , thank you .

1
will you also share preference xml file?Abhinav Chauhan
I edited the comment , can you please check again ?taki eddine
i think the preference that you are inflating has nothing to do with the layout that is set to preference category that preference was already inflated so you did not set a click listener to that one , you just inflated a layout and set a listener but that layout is not visible anywhere so you can not click it, if you want a custom prefernece you should subclass the preference classAbhinav Chauhan

1 Answers

3
votes

in addition to my comment above if you only want to access that preference only.

do it like follows

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
         setPreferencesFromResource(R.xml.settingspreferences,rootKey)
         getPreferenceScreen()
         .getPreference(4)
        .setOnPreferenceClickListener { preference - > // handle click 
          }
       }

note that you are setting a layout to preference category which is not meant for purpose you want to use it for , the correct way is to subclass the preference class and giving it the behavior you want , then you can use it in the preference screen directly