android – Set background of preference according to the position-ThrowExceptions

Exception or error:

I am developing an app where I am using Preference (androidx.preference.Preference).
I am using PreferenceCategory and adding Preerence to it at runtime.

What my requirement is that first,middle and last preferences in PreferenceCategory should have different backgrounds and layouts. I am using following code for this :

public static class SettingsFragment extends PreferenceFragmentCompat {
    Activity activity = null;
    String TAG = "SettingsFragment";
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (Activity) getActivity();
    }

    @Override
    public void onResume() {
        super.onResume();
        PreferenceCategory category = findPreference("MyPreference");
        category.setLayoutResource(R.layout.preference_group_layout);
        MyAdapter myAdapter = new MyAdapter(category);
        for(int i=0;i<10;i++) {
            Preference preference = new Preference(activity);
            preference.setKey("P" + i);
            preference.setLayoutResource(R.layout.preference_layout);
            category.addPreference(preference);
            Log.d(TAG,"" + category.getPreferenceCount());
            myAdapter.notifyDataSetChanged();
        }

    }
}

class MyAdapter extends PreferenceGroupAdapter {
    String TAG = "MyAdapter";
    public MyAdapter(PreferenceGroup preferenceGroup) {
        super(preferenceGroup);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    @Override
    public Preference getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @NonNull
    @Override
    public PreferenceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d(TAG,"onCreateViewHolder called");
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder, int position) {
        Log.d(TAG,"onBindViewHolder called");
        super.onBindViewHolder(holder, position);
    }
}

But This is not showing my Prefrence in layout. I searched on internet about this but I didn’t find any similar example. How can I add Preference and set their layout according to their positions.

How to solve:

Try changing the color in `onBindViewHolder’:

@Override
public void onBindViewHolder(@NonNull PreferenceViewHolder holder, int position) {
    Log.d(TAG,"onBindViewHolder called");
    super.onBindViewHolder(holder, position);
    switch (position){
        case 0:
            holder.itemView.setBackgroundColor(Color.WHITE);
        case 1:
            holder.itemView.setBackgroundColor(Color.GREEN);
            .....
            .....
        default: 
            holder.itemView.setBackgroundColor(Color.GRAY);
    }
}

###

PreferenceGroupAdapter object has the preference data but adapter is not linked with the fragment means, adapter is not being used so do this via overriding onCreateAdapter in your PreferenceFragmentCompat and return your adapter object as:

public class SettingsFragment extends PreferenceFragmentCompat {
    Activity activity = null;
    String TAG = "SettingsFragment";

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = getActivity();
    }

    // Add this with modified logic
    @Override
    protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
        PreferenceCategory category = findPreference("MyPreference");
        category.setLayoutResource(R.layout.preference_group_layout);
        MyAdapter myAdapter = new MyAdapter(category);
        final int tot = 10;
        final int middle = tot / 2;
        for (int i = 1; i <= tot; i++) {
            Preference preference = new Preference(activity);
            preference.setKey("P" + i);
            int layout = R.layout.preference_layout;
            switch (i) {
                case 1:
                    layout = R.layout.preference_layout_first;
                    break;
                case middle:
                    layout = R.layout.preference_layout_middle;
                    break;
                case tot: // last
                    layout = R.layout.preference_layout_last;
                    break;
            }
            preference.setLayoutResource(layout);
            category.addPreference(preference);
            Log.d(TAG, "" + category.getPreferenceCount());
            myAdapter.notifyDataSetChanged();
        }
        return myAdapter;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *