Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText requestFocus()?

Tags:

android

I got a problem with requestFocus.

First, this is my source code:

<EditText
    android:id="@+id/etGLNum2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/btGLCalculate"
    android:ems="10"
    android:hint="@string/gl_num2_hint"
    android:inputType="number" />

<EditText
    android:id="@+id/etGLNum1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/etGLNum2"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/btGLCalculate"
    android:ems="10"
    android:hint="@string/gl_num1_hint"
    android:inputType="number">

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/tvGLResult"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/etGLNum2"
    android:gravity="center"
    android:text="@string/result"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/btGLCalculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tvGLResult"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/etGLNum1"
    android:text="@string/calculate" />

As you see, focus have to go to etGLNum1, but it always goes to etGLNum2.

I already tried adding etGLNum1.requestFocus(); from my java file but it didn't worked.

What should i do?

Thanks :)

like image 807
Alfred Woo Avatar asked Feb 12 '13 05:02

Alfred Woo


People also ask

What is the use of RequestFocus?

RequestFocus(FocusSearchDirection, Rect) Call this to try to give focus to a specific view or to one of its descendants and give it hints about the direction and a specific rectangle that the focus is coming from.

What is RequestFocus?

- The purpose of the requestFocus() is to get the focus on the particular component and also on the window that contains the component. - Requests that the component gets the input focus.

How do I know if EditText has focus?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class.


1 Answers

Add requestFocus in your Edittext.

<EditText
    android:id="@+id/etGLNum2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/btGLCalculate"
    android:ems="10"
    android:hint="@string/gl_num2_hint"
    android:inputType="number">
    <requestFocus />
</EditText>
like image 131
MuraliGanesan Avatar answered Sep 29 '22 13:09

MuraliGanesan