Search code examples
androidandroid-preferences

How do access custom layout from PreferenceFragmentCompat?


I want to access views from a custom layout that was set to a preference

settings_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference
            android:key="storage"
            android:layout="@layout/layout_storage" />

    ...
</PreferenceScreen>

SettingsFragment.kt

class SettingsFragment: PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        addPreferencesFromResource(R.xml.settings_screen)
    }
}

There was a question on stackoverflow similar to this, but in that answer, he is accessing the view from the main layout file not from preference

PreferenceFragmentCompat custom layout

and also I found this article, he uses onBindView method to access the custom view.

https://code.luasoftware.com/tutorials/android/override-layout-of-android-preference/

but there is no method call onBindView in PreferenceFragmentCompat

UPDATED

layout_storage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:padding="20dp"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
            android:paddingBottom="5dp"
            android:orientation="horizontal"
            android:layout_width="match_parent" android:layout_height="wrap_content">

        <TextView
                android:textColor="@color/colorGray"
                android:paddingTop="5dp"
                android:layout_weight="1"
                android:text="USED"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>

        <TextView
                android:textColor="@color/colorGray"
                android:text="FREE"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    </LinearLayout>


    <ProgressBar
            android:progress="30"
            android:scaleY="5"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:id="@+id/progressBar"/>

</LinearLayout>

Now I want to get the progressBar


Solution

  • Override the following method in PreferenceFragmentCompat :

    @Override
    protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
        return new PreferenceGroupAdapter(preferenceScreen) {
            public PreferenceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                PreferenceViewHolder holder = super.onCreateViewHolder(parent, viewType);
                View customLayout = holder.itemView;
                if (customLayout.getId() == R.id.myCustomId) {
                    // get ref on your layout or modify it
                }
                return holder;
            }
        };
    }
    

    You might want to add an id in your LinearLayout to check for correct Layout...
    Hope it helps !