Android – How to disable softkeyboard in an EditText box while still have the flashing cursor


Android has a pretty unfriendly design where the soft keyboard appears in any editable EditText of an activity as soon as you enter that activity. I wanted a design such that the cursor inside the textbox is still flashing, however, I didn’t want the soft keyboard (which in my opinion, hides the entire UI, making it ugly). Moreover, the user should be the one deciding whether to enter something in the textbox and not the software.

Well, to achieve this, required a combination of a hack (courtesy: StackOverflow, and StackOverflow) as well as some meddling around:

In onCreate() of the activity where you have the edittext box:

protected void onCreate(Bundle savedInstanceState) {

mAmountText = (EditText) findViewById(R.id.amount);

// This will disable the Soft Keyboard from appearing by default
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

// Provide focus to the Amount box (since it is the top edittext box inside RelativeLayout)
mAmountText.requestFocus();

}

In your layout file (call it note_edit.xml):

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout
android:id=”@+id/main_layout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#ffffffff”
android:descendantFocusability=”beforeDescendants”
android:focusableInTouchMode=”true”
>

<EditText
android:id=”@+id/amount”
android:textColor=”#ff0066ff”
android:numeric=”decimal”
android:gravity=”left”
android:hint=”$0.00″
android:textSize=”48sp”
android:layout_height=”72dp”
android:layout_width=”fill_parent”
android:windowSoftInputMode=”stateHidden”
>
</EditText>

</RelativeLayout>

Advertisements

About hattipper

Collector of factoids. Love capturing images forever on my Canon SLR. On this site, you will learn factoids you didn't know about in a delightful manner.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s