Reputation: 173
I want to change the focus from edit-text field to a submit button when done is clicked on the soft input.
I've the following code :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Driver + Conductor :"
android:textSize="17sp"
android:textStyle="bold" />
<EditText
android:id="@+id/bus_driver_above60"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:hint="Driver"
android:inputType="numberDecimal"
android:textAlignment="center"
android:textColor="#000"
android:nextFocusDown="@+id/bus_conductor_above60"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:text="+"
android:textSize="17sp"
android:textStyle="bold" />
<EditText
android:id="@+id/bus_conductor_above60"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:hint="Conductor"
android:inputType="numberDecimal"
android:textAlignment="center"
android:textColor="#000"
android:nextFocus="@+id/bus_above60_btn"/>
</LinearLayout>
<Button
android:id="@+id/bus_above60_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Calculate"
android:textSize="17sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#3498DB"/>
Here in the code,
android:nextFocusDown="@+id/bus_conductor_above60"
changes the focus from Driver Edit text field to Conductor Edit text field.
But after Conductor Edit text field, there is a Calculate button - bus_above60_btn.
android:nextFocusDown="@+id/bus_above60_btn"
is not changing the focus from Conductor Edit text field to the button below that field.
How do I achieve this?
Upvotes: 0
Views: 1439
Reputation: 2684
If you just want to make button focused on Done action, first you must set your buttons focusableInTouchMode
as true
<Button
android:id="@+id/bus_above60_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Calculate"
android:focusableInTouchMode="true"
android:textSize="17sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#3498DB"/>
Now in your EditText
's OnEditorActionListener
, make button focused when done clicked:
bus_conductor_above60.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int action, KeyEvent keyEvent) {
if(action == EditorInfo.IME_ACTION_DONE)
bus_above60_btn.requestFocus();
return false;
}
});
Upvotes: 1
Reputation: 5351
Afaik there are two ways for getting your goal
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:hint="@string/username"
android:paddingLeft="10dp"
android:inputType="text"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:maxLines="1"
android:hint="@string/password"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@color/colorPrimary"
android:text="@string/login"
android:textColor="@color/white"
android:textSize="17sp" />
</LinearLayout>
The reason for it, from documentation, is the following:
Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides by using these XML attributes in the layout file [...]
So if you put the button in the same layout of the edittexts, it should manage the next focus automatically.
editText
edittext.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
Hope this helps!
Upvotes: 1
Reputation: 154
Your code will work if all components are in same layout. So firstly include your button in same layout where other editTexts placed then try.
Upvotes: 1
Reputation: 595
<EditText
android:id="@+id/bus_conductor_above60"
android:imeOptions="actionDone"
/>
In your code
bus_above60_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sameFunction();
}
});
bus_conductor_above60 = findViewById(R.id.bus_conductor_above60);
bus_conductor_above60 .setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
sameFunction();
}
return false;
}
});
Upvotes: 1