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