I have five Edit Text in my application . I also have two buttons called "Next" and "Previous". Now I want to select the next and previous edit text fields when i click the corresponding buttons form my view dynamically. Is there any way to do this.
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = getCurrentFocus().getNextFocusDownId();
if(id != View.NO_ID) {
findViewById(id).requestFocus();
System.out.println("Next");
}
}
});
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = getCurrentFocus().getNextFocusUpId();
if(id != View.NO_ID) {
findViewById(id).requestFocus();
System.out.println("Back");
}
}
});
This is the XML where you have to set the focus order
<EditText
android:id="@+id/et1"
android:nextFocusDown="@+id/et2"
android:nextFocusUp="@+id/et2"
....../>
<EditText
android:id="@+id/et2"
android:nextFocusDown="@+id/et1"
android:nextFocusUp="@+id/et1"
...../>
Edit
If you are creating view dynamic then you should use below methods to set the next focus
setNextFocusDownId(id)
setNextFocusUpId(id);
i think this may help you,
http://kahdev.wordpress.com/2008/06/29/changing-button-text-in-android/
Try -
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_next :
if(editText1.hasFocus()){
editText2.requestFocus();
}else if(editText2.hasFocus()){
editText3.requestFocus();
}
break;
case R.id.btn_previous :
if(editText2.hasFocus()){
editText1.requestFocus();
}else if(editText3.hasFocus()){
editText2.requestFocus();
}
break;
}
}
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