Home | History | Annotate | Download | only in preference
      1 /*
      2  * Copyright (C) 2015 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.Build;
     20 import android.os.Bundle;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.TextView;
     25 
     26 /**
     27  * This fragment provides a fully decorated leanback-style preference fragment, including a
     28  * list background and header.
     29  *
     30  * <p>The following sample code shows a simple leanback preference fragment that is
     31  * populated from a resource.  The resource it loads is:</p>
     32  *
     33  * {@sample frameworks/support/samples/SupportPreferenceDemos/src/main/res/xml/preferences.xml preferences}
     34  *
     35  * <p>The fragment needs only to implement {@link #onCreatePreferences(Bundle, String)} to populate
     36  * the list of preference objects:</p>
     37  *
     38  * {@sample frameworks/support/samples/SupportPreferenceDemos/src/main/java/com/example/android/supportpreference/FragmentSupportPreferencesLeanback.java
     39  *      support_fragment_leanback}
     40  */
     41 public abstract class LeanbackPreferenceFragment extends BaseLeanbackPreferenceFragment {
     42 
     43     public LeanbackPreferenceFragment() {
     44         if (Build.VERSION.SDK_INT >= 21) {
     45             LeanbackPreferenceFragmentTransitionHelperApi21.addTransitions(this);
     46         }
     47     }
     48 
     49     @Override
     50     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     51             Bundle savedInstanceState) {
     52         final View view = inflater.inflate(R.layout.leanback_preference_fragment, container, false);
     53         final ViewGroup innerContainer = (ViewGroup) view.findViewById(R.id.main_frame);
     54         final View innerView = super.onCreateView(inflater, innerContainer, savedInstanceState);
     55         if (innerView != null) {
     56             innerContainer.addView(innerView);
     57         }
     58         return view;
     59     }
     60 
     61     @Override
     62     public void onViewCreated(View view, Bundle savedInstanceState) {
     63         super.onViewCreated(view, savedInstanceState);
     64         setTitle(getPreferenceScreen().getTitle());
     65     }
     66 
     67     /**
     68      * Set the title to be shown above the preference list
     69      * @param title Title text to be shown
     70      */
     71     public void setTitle(CharSequence title) {
     72         final View view = getView();
     73         final TextView decorTitle = view == null
     74                 ? null : (TextView) view.findViewById(R.id.decor_title);
     75         if (decorTitle != null) {
     76             decorTitle.setText(title);
     77         }
     78     }
     79 }
     80