I’m trying to get a string from my strings.xml file inside an event handler, although I’m getting “No such static field”.
Here is my code:
mSearchEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
Context context = view.getContext();
if (hasFocus && mSearchEditText.getText().toString().trim() == context.getString(R.string.searchbar_address_label)) {
mSearchEditText.setText("");
}
else if (!hasFocus && mSearchEditText.getText().toString().trim().isEmpty()) {
mSearchEditText.setText(context.getString(R.string.searchbar_address_label));
}
}
});
What is wrong with this code?
Thanks
UPDATE: This code was intended to show a hint inside the EditText. The R.string seems to be unavailable inside listener’s functions. So, consider my question as “Is there a native way to show hints inside EditText instead of writing my own code?”
use
context.getResources().getString(R.string.searchbar_address_label);for setting hint use
android:hint="your_text"in xml layout. or same can be done programmatically using edittext.sethint(“text”);