I want to create ListView where items would have position like on screenshot. So text would be on left side and Switch on right. I know it should be done by TableLayout but I can't figure it out. Thanks for all help.
-
http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – Abdullah Tellioglu Jul 03 '17 at 18:01
3 Answers
You can easily achieve this using SwitchPreference
.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="key_data_saver"
android:title="Data Saver"
android:summary="Enable click-to-download for images and videos when using mobile data" />
</PreferenceScreen>
Here is an useful Tutorial: Android Implementing Preferences Settings Screen
OR,
If you want to create your own without SwicthPreference
, then use RecyclerView
with custom layout
for each row
item.
Here is your expected row item layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<Switch
android:id="@+id/switchRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toLeftOf="@id/switchRight"
android:layout_marginRight="8dp">
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18sp"
android:text="Data Saver"/>
<TextView
android:id="@+id/textDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textColor="#727272"
android:textSize="16sp"
android:text="Enable click-to-download for images and videos when using mobile data"/>
</LinearLayout>
</RelativeLayout>
OUTPUT:
Hope this will help~

- 21,438
- 5
- 52
- 61
The best way to implement this is with a PreferenceFragment.
You can create a XML file with all the Settings. (Let's say "settings.xml" inside the folder "XML" that you can create inside "res")
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="@string/settings_category_app">
<SwitchPreference
android:defaultValue="false"
android:key="@string/settings_notification_switch"
android:summary="Enable Click to download images and videos..."
android:title="Data Saver" />
</PreferenceCategory>
Update it is a static class because I created inside the main activity
After that you can add this resource to your SettingsFragment with:
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Then you can use this fragment as any other one. (e.g. On a drawer)

- 123
- 1
- 3
- 16
TableLayout doesn't need in ur case, cause u don't have any tables. You have to use RelativeLayout instead:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="wrap_content"
android:width="match_parent"
>
<TextView
android:height="wrap_content"
android:width="wrap_content"
android:id="@+id/tvTitle"
android:layout_alignParentLeft="true"
android:text="Title"
/>
<TextView
android:height="wrap_content"
android:width="wrap_content"
android:id="@+id/tvSubTitle"
android:layout_below="@id/tvTitle"
android:text="Subtitle"
/>
<ToggleButton
android:height="wrap_content"
android:width="wrap_content"
android:id="@+id/tbSwitch"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="switch"
/>
/>
For IOS styling of ToggleButton u may to use this library: https://github.com/kyleduo/SwitchButton

- 548
- 5
- 21