Lollipop Lollipop - 6 months ago 22
Android Question

Why android.R.id.list 's parent is FrameLayout not LInearLayout in Android N

I get the parent layout of

preferenceScreen
on lower than Android N is the following code :

final Dialog dialog = preferenceScreen.getDialog();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Log.d("TAG",dialog.findViewById(android.R.id.list).getParent().getClass()+"");
LinearLayout root = (LinearLayout) dialog.findViewById(android.R.id.list).getParent();
} else {
ViewGroup root = (ViewGroup) dialog.findViewById(android.R.id.content);
}


Here is the layout of PreferenceScreen in Android source code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/transparent"
android:layout_removeBorders="true">

<ListView android:id="@android:id/list"
style="?attr/preferenceFragmentListStyle"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:paddingTop="0dip"
android:paddingBottom="@dimen/preference_fragment_padding_bottom"
android:scrollbarStyle="@integer/preference_fragment_scrollbarStyle"
android:clipToPadding="false"
android:drawSelectorOnTop="false"
android:cacheColorHint="@android:color/transparent"
android:scrollbarAlwaysDrawVerticalTrack="true" />

......other widget......

</LinearLayout>


It works well on lower than Android N,But when I run my app in Android N, show error that the parent of ListView that id is android.R.id.list is FrameLayout not LinearLayout.
The Android source code explained that it is LinearLayout not FrameLayout,but why error?

Answer

It would make sense to use a FrameLayout instead of a LinearLayout with one child view. This change is most likely permanent. From the deleted answer and comments, I see you are using this library and this question is related to this issue on GitHub.

My advice would be to discontinue using that library. You should be using a PreferenceFragment instead of the deprecated PreferenceActivity. If you want to have preferences with material design on pre-L you should use the official v7 preference support library. However, last I checked this had some pitfalls.