How to make hint in EditText visible during user input and after it? And is it posible to place hint on the right side of EditText control, but user input text - on the left side?
Here is an example what I'm looking for:
How to make hint in EditText visible during user input and after it? And is it posible to place hint on the right side of EditText control, but user input text - on the left side?
Here is an example what I'm looking for:
You can use onFocusChangeListener to display the editText when the user is entering text into the field.
myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
myEditText.setHint(R.string.hint);
}
}
});
And regarding the alignment of the hint, It is not possible to right align the hint. Atleast it is not straightforward. In normal cases, Hint is designed to be visible only when there is no text in the EditText box and it has the same gravity as the editText content. You may try to have an accompanying TextView on the right besides the editText, if you want to provide a persistent 'hint'.
Im not sure it is possible by the widget mechanics. What I would try is to manage its content with a function to be called after every callback (you have to set a listener to the edittext). Try adding this listener:
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
/*here goes your function*/ }
});
Then for the function use parsed HTML to give the grey'ish 'hint' color several spaces after the user's input. You can use this formula:
String tmpHtml = "<html>a whole bunch of html stuff</html>";
String htmlTextStr = Html.fromHtml(tmpHtml).toString();
Hope this gives you an idea on how to implement it.
@ Sergii,
I agree with you.
Here My Answer Code with Same XML
private void setHintRight(final EditText et, final TextView tv) {
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false && TextUtils.isEmpty(et.getText().toString())) {
tv.setVisibility(View.VISIBLE);
}
}
});
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (et.getText().length() == 0) {
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
Here, you have to pass the edittext with left gravity and and Textview with right gravity...
So I've found pretty simple solution, a bit tricky, but it works - just to to make relative layout with EditText for input and TextView for hint inside: