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