Tuesday, May 24, 2011

Android - Writing a Keyboard Dialog

Recently, I was given a task that was not very well documented, or the documentation expected and assumed one thing, while in reality, I wanted another. I am writing an Android application for a science application and needed a scientific notation to be easy to input. This means that I need the keyboard by default to show up in numbers mode, but still allow symbols such as "e" to be allowed. I quickly learned that with the default android keyboard, you could not do this, as the inputType is what dictates where it starts. This means that in order to start the keyboard in number mode, it will also accept ONLY numbers. 
I had to search for another way, and frankly, I didn't need another Keyboard JUST for scientific notation. And because SoftKeyboards are so complicated to write, I saved that for a later project. Instead, I made a Dialog that behaved like a Keyboard and, when clicked, the EditText would show this View:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  android:id="@+id/keypad"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:stretchColumns="*">
  
  <TextView android:id="@+id/keypad_output"
  android:textSize="20dip"
    android:paddingTop="10dip"    
    android:paddingLeft="2dip"
    android:paddingRight="2dip" />
  <TableRow>
  <Button android:id="@+id/keypad_1"
  android:text="1"/>
  <Button android:id="@+id/keypad_2"
  android:text="2"/>
  <Button android:id="@+id/keypad_3"
  android:text="3"/>
  </TableRow>

Read more: <dream-in-code>