0

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

Jeeva
  • 3,975
  • 3
  • 23
  • 47

2 Answers2

4

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 !

avianey
  • 5,545
  • 3
  • 37
  • 60
0

You can add your preference layout in onCreatePreferences using setPreferencesFromResource for more detail visit PreferenceFragmentCompat and Android App Settings with Preference Fragment Compat

public static class SettingsFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    // Load the preferences from an XML resource
    setPreferencesFromResource(R.xml.settings_screen, rootKey);

    SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();

    PreferenceScreen preferenceScreen = getPreferenceScreen();

    int count = preferenceScreen.getPreferenceCount();

    for (int i = 0; i < count ; i++) {
    Preference p = preferenceScreen.getPreference(i);
    if (!(p instanceof CheckBoxPreference)) {
    String value = sharedPreferences.getString(p.getKey(), "");
    setPreferenceSummery(p, value);
      }
     }
}
}

Interface definition for a callback to be invoked when a shared preference is changed- onSharedPreferenceChanged

    @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

  Preference preference = findPreference(key);

  if (preference != null) {

  if (!(preference instanceof CheckBoxPreference)) {
  String value = sharedPreferences.getString(preference.getKey(), "");
  setPreferenceSummery(preference, value);
        }
     }
  }
Adil
  • 812
  • 1
  • 9
  • 29