Ulaş Sancak
Ulaş Sancak

Reputation: 907

Getting custom layout reference in PreferenceFragmentCompat

I created a preference which has a custom ImageView layout which is something like this:

<Preference
        android:key="profile_picture_preference"
        android:layout="@layout/layout_profile_picture"></Preference>

My layout is this:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/profileImageView"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:background="#ABCDEF" />

But I don't know how can get a reference to it. I have tried to get it inside onCreateView method by doing this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);

    ImageView imageView = (ImageView) view.findViewById(R.id.profileImageView);

    return view;
}

But ImageView is null.

Thank you for all your help.

Upvotes: 2

Views: 2616

Answers (3)

Arcyno
Arcyno

Reputation: 4603

I have spent my day trying to solve this... Here is my answer : I have been using onCreateView instead of onCreatePreferencesto load the view :

public static class MyFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        //setPreferencesFromResource(R.xml.my_fragment_xml, rootKey); //remove this (!)
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_layout, container, false); //root layout from the fragment xml
        view myView = view.findViewById(R.id.myView); //this works (!)

        return view;
    }

}

Upvotes: 0

ByteWelder
ByteWelder

Reputation: 5604

You can specify a custom layout in your theme. There you could add your ImageView

For example:

styles.xml

<style name="YourTheme" parent="Theme.AppCompat">
    <!-- ... -->
    <item name="preferenceTheme">@style/YourTheme.PreferenceThemeOverlay</item>
</style>

<style name="YourTheme.PreferenceThemeOverlay" parent="@style/PreferenceThemeOverlay">
    <item name="android:layout">@layout/fragment_your_preferences</item>
</style>

fragment_your_preferences.xml

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

    <ImageView
        android:id="@+id/profileImageView"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="#ABCDEF" />

    <!-- Required ViewGroup for PreferenceFragmentCompat -->
    <FrameLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </FrameLayout>

</LinearLayout>

And then in onViewCreated() of your fragment class you can start using the ImageView:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);

    ImageView image_view = (ImageView)view.findViewById(R.id.profileImageView);

    if (image_view != null)
    {
        // Your code
    }
}

Upvotes: 1

Ulaş Sancak
Ulaş Sancak

Reputation: 907

I did not find any offical way. So I managed like this:

@Override
public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    recyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState);
    recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (view.getId() == R.id.profileImageView) {
                profileImageView = (ImageView) view;
            }
        }
        @Override
        public void onChildViewDetachedFromWindow(View view) {
        }
    });
    return recyclerView;
}

Upvotes: 1

Related Questions