0

I have an activity where the user is presented with a list of edit texts. The problem is, as soon as the activity starts, the virtual keyboard is brought up right away for the first edit text. I don't want this to happen as there is a spinner before this edit text, and often I've found that popping up the keyboard like this makes the user forget about that previous spinner.

Here's the relevant code:

<Spinner
    android:id="@+id/event_location"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30px"
    android:prompt="@string/location_prompt" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Event Name: " />
<EditText
    android:id="@+id/event_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Activity Type: " />
<Spinner
    android:id="@+id/event_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Address: " />
<EditText
    android:id="@+id/event_address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Summary: activity with an EditText starts, and the virtual keyboard automatically pops up. I don't want it to do this unless the user presses on the edit text.

Thanks for any help.

jnthnjns
  • 8,962
  • 4
  • 42
  • 65
JDS
  • 16,388
  • 47
  • 161
  • 224

3 Answers3

2

The EditText is automatically set to focus so you need to put android:focusable="false" or editText.setFocusable(false) to prevent the EditText to be focused while the spinner is visible. You could also try editText.setSelected(false).

A. Abiri
  • 10,750
  • 4
  • 30
  • 31
1

This is how it works for me:

  1. to make sure the edit text wont get focus on startup:
    In xml set the focusable attribute of the EditText to false:
android:focusable="false"

2. in order to make the EditText focusable again after startup:
set the focusable attribute from code in the oncreate() method of the wanted activity after the setContent() method:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.main_view);
    ....

    EditText et = (EditText) findViewById(R.id.et_notes);

    et.setFocusable(true);

    et.setFocusableInTouchMode(true);

    ...
}  
Alex K
  • 22,315
  • 19
  • 108
  • 236
chipopo
  • 326
  • 2
  • 9
1

Try changing your spinner XML to this:

<Spinner 
android:layout_marginTop="30px"
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:id="@+id/event_location"
android:prompt="@string/location_prompt">
<requestFocus />
</Spinner>

Also try to do for each EditText:

android:focusable="false"
android:focusableInTouchMode="false"

Look at this post: Stop EditText from gaining focus at Activity startup

Community
  • 1
  • 1
pqn
  • 1,522
  • 3
  • 22
  • 33
  • alright sure, I'll try that out. Is there a way I could just make it so that nothing has a prompt? I.e. nothing "pops up" until the user clicks on something. EDIT - the requestFocus did not change anything. – JDS Aug 07 '11 at 18:09