Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom layout to PreferenceFragment

Tags:

android

I have a preference fragment with a preference screen hierarchy. I would like to add a custom layout to define an "about the author" section, adding imageview to an element and a link (intent to browser). I searched but I didn't find any resources about this topic.

like image 227
Cosimo Sguanci Avatar asked Oct 30 '16 20:10

Cosimo Sguanci


1 Answers

In your preference xml file (res/xml/prefs.xml), add a Preference with a custom layout:

<Preference
    android:layout="@layout/custom_preference"/>

Example of layout/custom_preference.xml with an ImageView and a TextView with a link that will be opened in the browser:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_book" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="http://www.mylink.com"
        android:autoLink="all"/>
</LinearLayout>

It's the last preference in the screenshot: preference screen with custom preference

like image 188
Carmen Avatar answered Sep 29 '22 03:09

Carmen