I have a ListView with an EditText on each row working.
I need to select this text on click the edittext to write numbers without erasing or moving the cursor.
I dealt with exactly the same problem by doing the following:
Subclassed EditText
public class TrickyEditText extends EditText {
private boolean mFocused = false;
private boolean mTouched = false;
public TrickyEditText(Context context) {
super(context);
}
public TrickyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TrickyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TrickyEditText(Context context, AttributeSet attrs, int defStyleAttr, int style) {
super(context, attrs, defStyleAttr, style);
}
@Override
public boolean didTouchFocusSelect() {
if (mTouched && mFocused) {
return true;
} else {
return super.didTouchFocusSelect();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mTouched = true;
return super.onTouchEvent(event);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
mFocused = focused;
if (!focused) {
mTouched = false;
}
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}
Adapter code
public class TrickyAdapter extends ArrayAdapter {
.............
@Override
public View getView(int childPosition, View convertView, final ViewGroup parent) {
.........
TrickyEditText et = ..... //initialize edittext
et.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(final View v, boolean hasFocus) {
if (hasFocus) {
v.post(new Runnable() {
@Override
public void run() {
((EditText)v).selectAll();
}
});
}
}
});
.........
}
}
Although it's working pretty well, it's not the code I'm proud of... If somebody knows a prettier solution, please tell me!