1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.leanback.preference; 18 19 import android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.TextView; 24 25 import org.jspecify.annotations.NonNull; 26 27 /** 28 * This fragment provides a fully decorated leanback-style preference fragment, including a 29 * list background and header. 30 * 31 * <p>The following sample code shows a simple leanback preference fragment that is 32 * populated from a resource. The resource it loads is:</p> 33 * 34 * {@sample samples/SupportPreferenceDemos/src/main/res/xml/preferences.xml preferences} 35 * 36 * <p>The fragment needs only to implement {@link #onCreatePreferences(Bundle, String)} to populate 37 * the list of preference objects:</p> 38 * 39 * {@sample samples/SupportPreferenceDemos/src/main/java/com/example/androidx/preference/LeanbackPreferences.java leanback_preferences} 40 */ 41 public abstract class LeanbackPreferenceFragmentCompat extends 42 BaseLeanbackPreferenceFragmentCompat { 43 LeanbackPreferenceFragmentCompat()44 public LeanbackPreferenceFragmentCompat() { 45 LeanbackPreferenceFragmentTransitionHelperApi21.addTransitions(this); 46 } 47 48 @Override onCreateView(@onNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)49 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, 50 Bundle savedInstanceState) { 51 final View innerView = super.onCreateView(inflater, container, savedInstanceState); 52 // parent class would create a themed context based the preferenceTheme attr. 53 LayoutInflater themedInflater = LayoutInflater.from(innerView.getContext()); 54 final View view = themedInflater.inflate(R.layout.leanback_preference_fragment, container, 55 false); 56 final ViewGroup innerContainer = (ViewGroup) view.findViewById(R.id.main_frame); 57 if (innerView != null) { 58 innerContainer.addView(innerView); 59 } 60 return view; 61 } 62 63 @Override onViewCreated(@onNull View view, Bundle savedInstanceState)64 public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { 65 super.onViewCreated(view, savedInstanceState); 66 setTitle(getPreferenceScreen().getTitle()); 67 } 68 69 /** 70 * Set the title to be shown above the preference list 71 * 72 * @param title Title text to be shown 73 */ setTitle(CharSequence title)74 public void setTitle(CharSequence title) { 75 final View view = getView(); 76 final TextView decorTitle = view == null 77 ? null : (TextView) view.findViewById(R.id.decor_title); 78 if (decorTitle != null) { 79 decorTitle.setText(title); 80 } 81 } 82 } 83