Rohith Nandakumar
Rohith Nandakumar

Reputation: 11427

Android: Start activity when EditText is clicked

How do I start a new activity when the user touches an EditText like in the Facebook search and Google search widget?

Setting setOnClickListener works only after the first click. On the first click the EditText becomes highlighted, and keyboard pops up. On second click it opens the new activity. I do not want this, instead I want to open the activity on the very first click. How do I do it?

Upvotes: 3

Views: 7125

Answers (3)

dmon
dmon

Reputation: 30168

You need to disable the EditText's focus in touch mode, that will make the onclick execute on the first tap:

<EditText ...
      android:focusableInTouchMode="false" 
      android:editable="false"
/>

Upvotes: 10

shihpeng
shihpeng

Reputation: 5381

Set the input type of the EditText to InputType.TYPE_NULL:

editText.setInputType(InputType.TYPE_NULL);

, which hides the soft keyboard while receiving user interaction. Start the activity:

public void onEditTextClick(View arg0) 
{
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(intent);
}

Of course, the onEditTextClick method has to be registered to the EditText object:)

Upvotes: 2

Bipin Vayalu
Bipin Vayalu

Reputation: 3145

Please, Try this

<EditText
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="search"/>

and

public void search(View view) 
{
    EditText text = (EditText)view;

    if(text.length() == 0)
        return;
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(intent);
}

Upvotes: 0

Related Questions