Search code examples
javaandroidsharedpreferencespreferenceactivity

Is it possible to add a custom component to a Preference Activity in android?


Is there any way to add custom component in preference activity in order to change value for the setting?


Solution

  • You can add any custom component that extends android.app.Preference

    Or if it should be a View - create a layout, but you must include a ListView with android.R.id.list in your layout for the PreferenceScreen

    <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/layout_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_color" >
    
        <YourCustomComponentView
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/view"
            android:layout_alignParentBottom="true"/>
    
    </RelativeLayout>
    

    Just setContentView() it before adding any preference.