Reputation: 23
Preference Example of multiple selections next to each other
I am trying to add a preference into my PreferenceScreen that allows the user to select one condition from multiple in a single row next to each other (like the image above).
Is there a pre-build way by android of doing that, or will I have to create a custom preference Layout to achieve this. An if yes what is the best way of doing that.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:title="Preferences">
<Preference
android:key="title"
android:title="title"
android:layout="@layout/preference_title"
/>
<SwitchPreference
android:key="dark_mode"
android:title="DarkMode"
android:defaultValue="false"/>
//Instead of Switch Preference for Dark Mode
</PreferenceScreen>
Upvotes: 0
Views: 410
Reputation: 1086
There is not a pre-built preference layout like that. You need to create a custom preference just using a horizontal RadioGroup.
two_inline_button_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingStart="?android:listPreferredItemPaddingStart"
android:paddingEnd="?android:listPreferredItemPaddingEnd">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start" />
</FrameLayout>
<RadioGroup
android:id="@+id/two_inline_button_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/left"
android:layout_weight="1.0"
android:button="@android:color/transparent" />
<RadioButton
android:id="@+id/right"
android:layout_weight="1.0"
android:button="@android:color/transparent" />
</RadioGroup>
</LinearLayout>
your preference xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
app:title="Preferences">
<InlineButtonPreference
android:key="dark_mode"
android:layout="@layout/two_inline_button_layout"
custom:titleText="DarkMode"
custom:startText="ON"
custom:endText="OFF" />
</PreferenceScreen>
TwoInlineButtonPreference.kt
class TwoInlineButtonPreference : Preference {
....
override fun onBindView(view: View) {
super.onBindView(view)
val radioGroup = view.findViewById(R.id.two_inline_button_layout) as RadioGroup
}
}
Upvotes: 0