CharacterPickerDialog.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/* 2 * Copyright (C) 2008 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 android.text.method; 18 19import com.android.internal.R; 20 21import android.app.Dialog; 22import android.content.Context; 23import android.os.Bundle; 24import android.text.*; 25import android.view.LayoutInflater; 26import android.view.View.OnClickListener; 27import android.view.View; 28import android.view.ViewGroup.LayoutParams; 29import android.view.ViewGroup; 30import android.view.WindowManager; 31import android.widget.AdapterView.OnItemClickListener; 32import android.widget.AdapterView; 33import android.widget.BaseAdapter; 34import android.widget.Button; 35import android.widget.GridView; 36import android.widget.TextView; 37 38/** 39 * Dialog for choosing accented characters related to a base character. 40 */ 41public class CharacterPickerDialog extends Dialog 42 implements OnItemClickListener, OnClickListener { 43 private View mView; 44 private Editable mText; 45 private String mOptions; 46 private boolean mInsert; 47 private LayoutInflater mInflater; 48 49 /** 50 * Creates a new CharacterPickerDialog that presents the specified 51 * <code>options</code> for insertion or replacement (depending on 52 * the sense of <code>insert</code>) into <code>text</code>. 53 */ 54 public CharacterPickerDialog(Context context, View view, 55 Editable text, String options, 56 boolean insert) { 57 super(context); 58 59 mView = view; 60 mText = text; 61 mOptions = options; 62 mInsert = insert; 63 mInflater = LayoutInflater.from(context); 64 } 65 66 @Override 67 protected void onCreate(Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 70 WindowManager.LayoutParams params = getWindow().getAttributes(); 71 params.token = mView.getApplicationWindowToken(); 72 params.type = params.TYPE_APPLICATION_ATTACHED_DIALOG; 73 74 setTitle(R.string.select_character); 75 setContentView(R.layout.character_picker); 76 77 GridView grid = (GridView) findViewById(R.id.characterPicker); 78 grid.setAdapter(new OptionsAdapter(getContext())); 79 grid.setOnItemClickListener(this); 80 81 findViewById(R.id.cancel).setOnClickListener(this); 82 } 83 84 /** 85 * Handles clicks on the character buttons. 86 */ 87 public void onItemClick(AdapterView parent, View view, int position, long id) { 88 int selEnd = Selection.getSelectionEnd(mText); 89 String result = String.valueOf(mOptions.charAt(position)); 90 91 if (mInsert || selEnd == 0) { 92 mText.insert(selEnd, result); 93 } else { 94 mText.replace(selEnd - 1, selEnd, result); 95 } 96 97 dismiss(); 98 } 99 100 /** 101 * Handles clicks on the Cancel button. 102 */ 103 public void onClick(View v) { 104 dismiss(); 105 } 106 107 private class OptionsAdapter extends BaseAdapter { 108 private Context mContext; 109 110 public OptionsAdapter(Context context) { 111 super(); 112 mContext = context; 113 } 114 115 public View getView(int position, View convertView, ViewGroup parent) { 116 Button b = (Button) 117 mInflater.inflate(R.layout.character_picker_button, null); 118 b.setText(String.valueOf(mOptions.charAt(position))); 119 return b; 120 } 121 122 public final int getCount() { 123 return mOptions.length(); 124 } 125 126 public final Object getItem(int position) { 127 return String.valueOf(mOptions.charAt(position)); 128 } 129 130 public final long getItemId(int position) { 131 return position; 132 } 133 } 134} 135