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