6

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 .

Taki
  • 3,290
  • 1
  • 16
  • 41
  • will you also share preference xml file? – Abhinav Chauhan Jun 25 '20 at 11:35
  • I edited the comment , can you please check again ? – Taki Jun 26 '20 at 07:04
  • 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 class – Abhinav Chauhan Jun 26 '20 at 09:46

1 Answers1

2

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

Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
  • thank you for your answer but what do you mean by subclassing a preference , it is my first time using preference settings , if you dont mind , thank you – Taki Jun 26 '20 at 14:10
  • Should i create a seperate class where i inflate my layout , then in my xml settings i create preferencecategory and inside it i put my custom class in there , give it a key and find it by key in my settigns fragment ? is it like this ? thank you – Taki Jun 26 '20 at 14:31
  • ex - I created a custom preference here -> https://github.com/AbhinavChauhan97/Gym_Data_Manager/blob/master/app/src/main/java/com/abhinav/chauhan/gymdatamanager/Preferences/SecurityOptionsPreference.java which shows a preference of two radio button which behaves as a group and it is used here -> https://github.com/AbhinavChauhan97/Gym_Data_Manager/blob/master/app/src/main/java/com/abhinav/chauhan/gymdatamanager/Preferences/SecurityPreferences.java – Abhinav Chauhan Jun 27 '20 at 02:50
  • okey i did create a class that extends preference , i overid onbindviewholder and initiate the setlayoutresource on the init block , should i include this class in the my preference settings xml or initate in my settingspreference fragment ? – Taki Jun 27 '20 at 18:28
  • you can use it in xml with fully qualified name as well as in code, same as you can use create EditTextPrefernce in xml as well as in code – Abhinav Chauhan Jun 28 '20 at 03:23
  • This is how i did it my friend , can you check and tell if it is correct and what i m missing , thank you this is my code in paste bin https://pastebin.com/aChQ6mEL – Taki Jun 28 '20 at 08:01
  • after doing all these steps and start my app , it shows this error android.view.InflateException: Binary XML file line #30: Error inflating class taki.food.foodappv.RatingPreferences ( which has something to do with the custom class that i included as a preference in my xml layout ) – Taki Jun 28 '20 at 08:08
  • when you create a view or preference from xml the system calls a constructor which which takes a `Context` and `AttributeSet` object the attributes you give to that preference are passed in AttributeSet object , as you only have one constructor which takes context you can only create this preference in code to use in xml introduce the constructor which system calls ex-> https://gist.github.com/FaizVisram/9541052 – Abhinav Chauhan Jun 28 '20 at 11:24
  • you are welcome taki eddine, and you also learned how to create custom preference , which encapsulate the logic and lets you easily test and reuse this preference , you will use this knowledge more in your career as a android developer – Abhinav Chauhan Jun 29 '20 at 03:33
  • Thank you for the help and i appreciate it so much – Taki Jun 29 '20 at 14:20
  • Hello mate , i opened a topic preferences but this time it has something to do with changing checkboxpreferences color can you please check it out if you have any idea thank you https://stackoverflow.com/questions/62659485/changing-switch-preference-color – Taki Jul 01 '20 at 08:11