• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.support.v7.preference;
18 
19 import android.os.Bundle;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.view.ViewParent;
23 import android.widget.EditText;
24 
25 public class EditTextPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {
26 
27     private EditText mEditText;
28 
newInstance(String key)29     public static EditTextPreferenceDialogFragmentCompat newInstance(String key) {
30         final EditTextPreferenceDialogFragmentCompat
31                 fragment = new EditTextPreferenceDialogFragmentCompat();
32         final Bundle b = new Bundle(1);
33         b.putString(ARG_KEY, key);
34         fragment.setArguments(b);
35         return fragment;
36     }
37 
38     @Override
onBindDialogView(View view)39     protected void onBindDialogView(View view) {
40         super.onBindDialogView(view);
41 
42         mEditText = new EditText(view.getContext());
43         // Give it an ID so it can be saved/restored
44         mEditText.setId(android.R.id.edit);
45 
46         mEditText.setText(getEditTextPreference().getText());
47 
48         ViewParent oldParent = mEditText.getParent();
49         if (oldParent != view) {
50             if (oldParent != null) {
51                 ((ViewGroup) oldParent).removeView(mEditText);
52             }
53             onAddEditTextToDialogView(view, mEditText);
54         }
55     }
56 
getEditTextPreference()57     private EditTextPreference getEditTextPreference() {
58         return (EditTextPreference) getPreference();
59     }
60 
61     /** @hide */
62     @Override
needInputMethod()63     protected boolean needInputMethod() {
64         // We want the input method to show, if possible, when dialog is displayed
65         return true;
66     }
67 
68     /**
69      * Adds the EditText widget of this preference to the dialog's view.
70      *
71      * @param dialogView The dialog view.
72      */
onAddEditTextToDialogView(View dialogView, EditText editText)73     protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
74         ViewGroup container = (ViewGroup) dialogView
75                 .findViewById(R.id.edittext_container);
76         if (container != null) {
77             container.addView(editText, ViewGroup.LayoutParams.FILL_PARENT,
78                     ViewGroup.LayoutParams.WRAP_CONTENT);
79         }
80     }
81 
82     @Override
onDialogClosed(boolean positiveResult)83     public void onDialogClosed(boolean positiveResult) {
84 
85         if (positiveResult) {
86             String value = mEditText.getText().toString();
87             if (getEditTextPreference().callChangeListener(value)) {
88                 getEditTextPreference().setText(value);
89             }
90         }
91     }
92 
93 }
94