Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set focus on a TextView when an Activity starts?

Tags:

android

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.

like image 534
user256239 Avatar asked Aug 16 '10 19:08

user256239


People also ask

How do you create a TextView control in an activity file?

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.

How do you set focus on a view?

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.

Which method is used to set the text in a TextView?

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.


6 Answers

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.

like image 54
Romain Guy Avatar answered Oct 05 '22 02:10

Romain Guy


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.

like image 40
LiTTle Avatar answered Oct 05 '22 03:10

LiTTle


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"
like image 28
tomatosoup Avatar answered Oct 05 '22 02:10

tomatosoup


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>
like image 20
VIjay J Avatar answered Oct 05 '22 02:10

VIjay J


Add the TextView Property in layout

android:focusable="false"

It's work for me.

like image 24
Joy Avatar answered Oct 05 '22 02:10

Joy


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.

like image 28
App Coder Avatar answered Oct 05 '22 04:10

App Coder