Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 591
Rohith Nandakumar Avatar asked Jun 29 '26 08:06

Rohith Nandakumar


2 Answers

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"
/>
like image 166
dmon Avatar answered Jul 01 '26 00:07

dmon


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:)

like image 31
shihpeng Avatar answered Jun 30 '26 22:06

shihpeng