1

I have seen many Q&A on stack about it but for some reason they do not work in my case.

I think it might be because I'm working with elements from and to the view directly.

This code is working;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnOnClickRegister(View view){
     TextView txtEmail = findViewById(R.id.txtEmail);
     EditText edtTxtEmail = findViewById(R.id.edtTxtEmail);
     txtEmail.setText("Email "+ edtTxtEmail.getText().toString()); //hardcoded and will not translate
    }
}

In the strings.xml;

<string name="email">Email</string>

I get the following warning;

Do not concatenate text displayed with setText. Use resource string with placeholders.

Most Q&A resolve this like so;

getString(R.string.email, VarContainingEmail);

with an XML that look like this;

<string name="email">Email %s</string>

If I try that I get the following warning;

Cannot resolve method 'getString' in 'TextView'

I'm just starting with android studio and would like to resolve this right off the start.

I'm using Android Studio version 4.0.1

2 Answers 2

1

Please try once this way, let me know if doesn't work

<string name="email">Email, %1$s!</string>
txtEmail.setText(getString(R.string.email, edtTxtEmail.getText().toString()));
Sign up to request clarification or add additional context in comments.

4 Comments

I thought so to but i'm getting ; Format string 'email' is not a valid format string so it should not be passed to String.format
can you show how are you actually trying this approach?
I got it! I had to update all value res file. I was only updating one so some of them where not valid string. After updating all languages it fixed the error.
Exactly, @MadeInDreams please show what changes you have made to the code. Also tell whether this code is inside fragment or activity. If fragment use getContext() to call getString(). If activity use this to call getString()
1

You may need to use the context to get access to getString:

context.getString(R.string.email, VarContainingEmail);

where context could be this if you are in the activity

Edit: So if you are in a view, let's say this view is the variable view, you can use: view.getContext().getString(R.string.email, VarContainingEmail);

2 Comments

How do I declare this variable and assign a value to it in my context? This variable doesn't exist in my context because i'm reading the value from the view.
in that case, you can access the context using: view.getContext(), I edited my answer. Your view could be accessible using this, in that case you could use this.getContext()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.