Reputation: 3761
In one of my edit text
, when I'm writing I want the text to be witten in middle & If I delete the text or the text.length = 0, then I want the cursor to be on starting (left).
For this I'm trying in the
I was thinking of doing it with Gravity.CENTER_VERTICAL & Gravity.NO_GRAVITY
, in my TextChangedListener's afterTextChanged()
. But it is not working.
How to do this?
Thanks
Upvotes: 0
Views: 106
Reputation: 3633
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(s.length()>0){
edittext.setGravity(Gravity.CENTER);
}
else{
edittext.setGravity(Gravity.CENTER_VERTICAL);
}
}
})
Upvotes: 1
Reputation: 2664
Try this..
if (TextUtils.isEmpty(editText.getText())) {
editText.setSelection(0);
}else{
//Sets the cursor position of the edittext
editText.setSelection(POSITION_YOU_WANT);
}
Upvotes: 0
Reputation: 629
ed.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
ed.setGravity(Gravity.CENTER);
}
})
Upvotes: 0