How do you concatenate two strings to show in one textview?

how to concatenate two strings in settext
how to concatenate two strings in xml android
android textview concatenate strings
how to concatenate multiple strings in android programmatically
do not concatenate text displayed with settext. use resource string with placeholders.
android concatenate resource strings
kotlin concatenate textview
android textview settext concatenate

I am working on a Text Adventure Game.

In the strings.xml I have two strings.

How do you add the two strings together so that a textview shows:

You are in a Garage.

Thanks in advance.

strings.xml

<string name="location_prefix">You are in a </string>
<string name="location_name">Garage</string>

MainActivity.kt

package com.example.textdisplay

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Gradle:

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
} 

You can concatenate Strings using the + operator:

String firstString = resources.getString(R.string.you_are)
String secondString = resources.getString(R.string.garage)

textView.setText(firstString + secondString)

before you ask, always check if someone had similar problem :)

How do I concatenate two strings in Java?

I want to concat two strings for a TextView in android, Data Binding , concate it with grave accent (`) android:text="@{`Hello ` + user.firstName}"/>. You can concat it in multiple ways, check it here� Sometimes we might need a functionality where we needs to concat two strings in xml only. So here are the different ways of how can we do it using databinding. 1. Using grave accent(`)[crayon-5f2df06f71ce2659742491/] 2. Using strings.xml [crayon-5f2df06f71cee109158540/] and in your string.xml file [crayon-5f2df06f71cf4574660177/] 3. Using concat() [crayon-5f2df06f71cfa925707204/] 4. Using

Assume in activity_main.xml file, there is a TextView whose id is textView. Here are some solutions to set text for the textView.

Solution 1

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val textView = findViewById<TextView>(R.id.textView)
        textView.text = getString(R.string.location_prefix) + getString(R.string.location_name)
    }
}

Solution 2

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val textView = findViewById<TextView>(R.id.textView)
        textView.text = "${getString(R.string.location_prefix)}${getString(R.string.location_name)}"
    }
}

Solution 3

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val textView = findViewById<TextView>(R.id.textView)
        textView.text = String.format("%s%s", getString(R.string.location_prefix), getString(R.string.location_name))
    }
}

It's up to you to pick a solution.

Update: To keep the end space in location_prefix string, then go to string.xml file and change:

<string name="location_prefix">You are in a </string>

to

<string name="location_prefix">You are in a\u0020</string>

Concat Two Strings In TextView Using DataBinding, Using grave accent(`) android:text="@{`Hello ` + user.firstName}" Using strings.xml. android:text="@{@string/location(user.city,user.state)}" Using concat() android:text="@{user.firstName.concat(@string/space).concat(user.lastName)}" Using string.format() android:text= "@{String.format(@string/Hello, user.name)}" Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

If you don't want to concat them in code, you could use this plugin I've created: https://github.com/LikeTheSalad/android-string-reference which will generate a string with all of the strings you'd like to concatenate at build time, so for your case, you'd have to define the following in your strings:

<string name="template_location_prefix">You are in a ${location_name}</string>
<string name="location_name">Garage</string>

And then, when you run the tool, you'd get the following:

<string name="location_prefix">You are in a Garage</string>

Which you can then access to as with any other manually defined string within your layouts and/or code.

More details on the repo's page.

How to concat two strings in setText() in Android, how to concatenate multiple strings in android programmatically Concat Two Strings In TextView Using DataBinding; INVISIBLE vs GONE View in would Combine Two Strings into one string using the .concat() String function and show the� Use Concatenate to join two cells. The basic function of concatenate is to join two or more text strings together. You can join up to 255 different strings together using one concatenate command. Take the following example:

I want to concat two strings for a TextView in android, Data Binding Api, I want to concat two strings for a TextView in android, Data Binding Api. how to concatenate I want the TextView to display Hello UserName. How to achieve� Concatenate is simply a fancy way ot saying “to combine” or “to join together” and there is a special CONCATENATE function in Excel to do this. This function allows you to combine text from different cells into one cell. For example, we have a worksheet containing names and contact information.

android.widget.TextView.append java code examples, TextView.append (Showing top 20 results out of 990) View view;(TextView) view. private void appendEntryToView(String text, long timestamp) { Date date = new Date(timestamp); // Add a Single TextView with two different colored text. In Excel, you can combine or merge text from two or more cells, as well as columns and rows,into one cell. If you have a lot of rows of data where you want to combine text, you can simply start typing the combined text in an adjacent column and Excel will fill in the rest for you.

Concatenate multiple strings in XML?, I want to concat two strings for a TextView in android, Data Binding Api, how to Show us more code to help you better, string concatenation is actually a very� Concatenation means joining two values or two strings together, similar to excel we use & or also known as ampersand operator to concatenate, two concatenate two strings we use & operator like String 1 & String 2, now there is an important thing to remember and that is while using the & operator we need to provide spaces or VBA will consider it as long.

Comments
  • welcome stackoverflow. stackoverflow.com/help/mcve
  • Thanks. I am very much a beginner in Android programming. What file do you place the above code in? activity_main.xml?
  • In MainActivity.java (java class not xml) which is linked to activity_main.xml
  • Where abouts in the above code as I am getting unresolved reference errors on "firststing" and "secondstring" strings.
  • Thanks. Thats great. I went with Solution 1. I had to add "import android.widget.TextView". However I get the error "Overrides function in AppCompatActivity".
  • @seddonac Do you have any problem with your code now?
  • The code runs but with a warning as above with "overrides function in appcompatactivity". How do you get rid of the warning?
  • Also, how can I add a space between the strings? At the moment the result of concatenating the two strings is "You are in aGarage". I need a space between "a" and "Garage".
  • Thanks that works fine. What do you do about the warnings? The app runs fine with no build errors.