There are an EditText and a TextView in my Activity. I want to set focus on the TextView when the Activity starts. I used the following code:
TextView myTextView = (TextView) findViewById(R.id.my_text_vew);
myTextView.setFocusable(true);
myTextView.setOnClickListener(this);
myTextView.requestFocus();
But the code doesn't work. The EditText always receives focus when the activity starts.
Can anyone help?
Thanks.
Modify src/MainActivity. java file to add necessary code . Modify the default content of res/layout/activity_main. xml file to include Android UI control.
If you are using java code to generate layout then you can use methods setFocusable(boolean) and setFocusableInTouchMode(boolean). You can then set the focus to a view using public method requestFocus(). The framework will handle routine focus movement in response to user input.
Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method.
This is expected when you start the Activity using the touch screen. When in "touch mode," only a couple of Views (EditText and ListView) can receive the focus. If you start your application using the trackball you will see the focus on the TextView. What you are seeing is the correct and expected result.
I came across with your question now. Maybe after 2 years you have found a solution. But I write it here for the others that will come across with this in the future. The only thing you have to do is to go to the AndroidManifest.xml
, find the Activity
in which you want this behaviour and add this android:windowSoftInputMode="stateHidden"
. So, at the start of your Activity
no keyboard will be shown, until you touch the EditText
object.
If anyone is still wondering, you can use the following in your layout xml to make a textview focusable in touch mode:
android:focusableInTouchMode="true"
In order to request focus to TextView you need to set focusable="true"
and focusableInTouchMode="true"
as follows:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heading"
android:textSize="20sp"
android:textStyle="bold"
android:focusable="true"
android:focusableInTouchMode="true">
<requestFocus />
</TextView>
Add the TextView Property in layout
android:focusable="false"
It's work for me.
Use this:
EditText mEditText = (EditText) findViewById(R.id.edit_text);
TextView myTextView = (TextView) findViewById(R.id.my_text_vew);
mEditText.setFocusable(false);
myTextView.setFocusable(true);
This takes focus off of the EditText and puts it on the TextView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With