Reputation: 1827
I have an EditText
. I would like to resize the text inside the EditText
, if the length of the text is bigger than 10 for example.
How to do this?
Thank you. Appreciate!
Upvotes: 2
Views: 2139
Reputation: 18670
You may prefer using a TextWatcher instead of an OnKeyListener (which obviously targets hardware keys) :
final float size1 = 30, size2 = 15;
final EditText edit = new EditText(this);
edit.setTextSize(size1);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 10) {
edit.setTextSize(size2);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
Upvotes: 2
Reputation: 1267
Please try:
if(myEditText.getText().toString().lenght() > 10)
{
myEditText.setTextSize(6);
}
else
{
myEditText.setTextSize(10);
}
Upvotes: 0
Reputation: 1325
This should do the trick :
final EditText et = (EditText) findViewById(R.id.editTextId);
et.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(et.getText().length()>10) {
et.setTextSize(newValue);
} else {
et.setTextSize(oldValue);
}
return false;
}
});
Upvotes: 3