Problem
I'm trying to create a custom Preference that takes up the entire height of the current PreferenceScreen. My preference is conceptually similar to the Wi-Fi selection screen. It asynchronously loads data and adds it to a ListView of possible options. To be consistent with the Wi-Fi screen I'd like the preference to launch a standalone preference activity that takes up the entire screen. I don't want to use a dialog like ListPreference since the data is loaded asynchronously and it wouldn't be consistent with the Wi-Fi screen.
Approach 1
My first approach used a PreferenceFragment to load a PreferenceScreen that only had a single child preference. This has the notable issue that my custom preference is shoved into a single list view option:
My Preference is getting cut off because the PreferenceScreen is only letting it take up that tiny portion of the screen. It looks like the PreferenceScreen really just launches a single activity with a ListView that inflates the PreferenceScreen's child Preferences. I tried just subclassing PreferenceScreen to change how the ListView displays my custom Preference; however, it appears that PreferenceScreen is final. I was also unable to successfully manipulate the height of my custom Preference from just my preference's XML or java code. I was able to increase the height partially by following the suggestion found in this post; however, I could never get it to take up the entire screen real estate. Here's the current XML for my Preference's view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/widget_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:layout_centerInParent="true">
<ListView
android:id="@+id/server_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"/>
</RelativeLayout>
Approach 2
My next approach just created a custom activity that inflated some views exactly how I wanted. The problem here is that I wasn't able to figure out how to tie in my custom view directly into the preference system. It looks like both PreferenceActivity and PreferenceFragment want you to inflate a hierarchy that's rooted with a PreferenceScreen. It seems like no matter what I do I'll run into problems like those seen in Approach 1 if I try to use that. This means the only way I could actually save preference data was going to involve manually saving, loading, and manipulating user preference data when my activity starts, exits, etc. This seems like a pretty poor option because I'm creating a worse version of the existing PreferenceActivity and PreferenceFragment classes.
That being said, my Preference is ultimately going to save three discrete key-value pairs even though the user only ends up making a single selection. My situation may be different enough to merit me rolling my own custom code.
Future Direction
- Figure out how to adjust the height of my custom Preference in Approach 1 to force it to take up all available screen real estate
- Figure out how to use the Preference system without manually handling all of the stuff that PreferenceActivity and PreferenceFragment normally handle for you.
- Accept that my situation is actually different enough to merit rolling my own code to save and load the three key-value pairs of settings using approach 2