Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflating a layout in preference settings

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 .

like image 861
Taki Avatar asked Jun 25 '20 10:06

Taki


1 Answers

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

like image 172
Abhinav Chauhan Avatar answered Oct 12 '22 23:10

Abhinav Chauhan