0
<EditText
    android:id="@+id/et_name"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_margin="20dp"
    android:hint="@string/str_name" />

<TextView
    android:id="@+id/tvSentence"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:text="@string/str_hello1"/>

I want et_name to be added to the @string/str_hello1 line. I tried this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:text="@string/str_hello1"  + "@+id/et_name"

and

    android:text="@string/str_hello1"  + et_name

and

    android:text="@string/str_hello1"  + "et_name"

But none of them worked and now everything is underlined in red saying unexpected text.

2 Answers 2

0

you have to do it programatically form java side or kotlin you cant do that in xml

Sign up to request clarification or add additional context in comments.

1 Comment

Do you have an example or a link to one? I've been trying to google and i can't seem to narrow down the right search terms. I can't find what i want. I'm ok doing it in MainActivity.kt, i just need to learn how.
0

Just like what paradox codee said, you need to do it programmatically. In your XML code, put a button that will let you get the value of your editText when clicked. Let's call it btn_concat.

<Button
    android:id="@+id/btn_concat"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_margin="20dp"/>

Now, if you're comfortable doing it on Java, here's how you can do it. On your MainActivity.java, put the following code. The comments // explain what each code does:

public class MainActivity extends AppCompatActivity {
    
    // Put here the global objects
    Button btn_concat;
    EditText et_name;
    TextView tvSentence;

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

        // Now, this is where you initialize your objects
        // Find objects in layout by using their assigned IDs
        btn_concat = findViewById(R.id.btn_concat);
        tvSentence = findViewById(R.id.tvSentence);
        et_name = findViewById(R.id.et_name);

        // Set a click listener for the concat Button
        btn_concat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // This is where we do what you're trying to do
                // First, get the value of your EditText,
                // and let's assign it to a String object called etValue
                String etValue = et_name.getText.toString();
                // Get the str_hello1 from resources
                String hello = getResources().getString(R.string.str_hello1)
                // Let's now update the TextView
                String concatValue = hello + etValue;
                tvSentence.setText(concatValue);                
            }
        });
    }

And that's it! Just enter anything on the editText, click the button, and you will see the concatenated text on the TextView. If you want, you can also do the concatenation as the user is typing. Just add a TextChangeListener to the EditText and put the code on onTextChanged interface.

Comments

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.