Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context.getString causing "no such static field"

Tags:

android

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?"

like image 934
Juliano Nunes Silva Oliveira Avatar asked Jun 21 '26 22:06

Juliano Nunes Silva Oliveira


1 Answers

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");

like image 93
skygeek Avatar answered Jun 24 '26 12:06

skygeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!