Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText: Avoid getting focus on setText()

I have an activity with a scrollview containing some buttons and a EditText So the buttons are above the EditText component.

By clicking on a Button the EditText component change his text value by using EditText.setText("foo");

Unfortunately this call will set the focus to the EditText component, so the ScrollView will scroll to the EditText component (cursor blinks).

Is there a way to avoid this?

I have tried to do something like this

public void setText(String t){
    editText.setFocusable(false)
    editText.setFocusableInTouchMode(false);
    editText.setText(t);
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
}

But that does not work, and sometimes setFocusable(true) has no impact, so the EditText is not "clickable" anymore.

like image 773
sockeqwe Avatar asked Oct 17 '13 09:10

sockeqwe


1 Answers

editText.clearFocus();

Called when this view wants to give up focus.

http://developer.android.com/reference/android/view/View.html#clearFocus()

like image 189
mesutbeyaztas Avatar answered Sep 17 '22 18:09

mesutbeyaztas