blob: 551b5e58afe111d78167274101a389cedf6e2eef [file] [log] [blame]
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001/*
2 * Copyright (C) 2012 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
17package com.android.settings;
18
19import android.content.Context;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070020import android.text.TextUtils;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.EditText;
24
Juan Lang44828122017-05-10 17:26:02 -070025import com.android.settingslib.CustomEditTextPreference;
26
Jason Monk39b46742015-09-10 15:52:51 -040027public class SelectableEditTextPreference extends CustomEditTextPreference {
Amith Yamasani9627a8e2012-09-23 12:54:14 -070028
29 private int mSelectionMode;
30
31 public static final int SELECTION_CURSOR_END = 0;
32 public static final int SELECTION_CURSOR_START = 1;
33 public static final int SELECTION_SELECT_ALL = 2;
34
35 public SelectableEditTextPreference(Context context, AttributeSet attrs) {
36 super(context, attrs);
37 }
38
39 /**
40 * Sets the selection mode for the text when it shows up in the dialog
41 * @hide
42 * @param selectionMode can be SELECTION_CURSOR_START, SELECTION_CURSOR_END or
43 * SELECTION_SELECT_ALL. Default is SELECTION_CURSOR_END
44 */
45 public void setInitialSelectionMode(int selectionMode) {
46 mSelectionMode = selectionMode;
47 }
48
49 @Override
50 protected void onBindDialogView(View view) {
51 super.onBindDialogView(view);
52
53 EditText editText = getEditText();
54 // Set the selection based on the mSelectionMode
55 int length = editText.getText() != null ? editText.getText().length() : 0;
56 if (!TextUtils.isEmpty(editText.getText())) {
57 switch (mSelectionMode) {
58 case SELECTION_CURSOR_END:
59 editText.setSelection(length);
60 break;
61 case SELECTION_CURSOR_START:
62 editText.setSelection(0);
63 break;
64 case SELECTION_SELECT_ALL:
65 editText.setSelection(0, length);
66 break;
67 }
68 }
69 }
70}
71