1

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
} 
seddonac
  • 41
  • 1
  • 3

3 Answers3

2

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?

Palejandro
  • 2,092
  • 5
  • 26
  • 38
  • Thanks. I am very much a beginner in Android programming. What file do you place the above code in? activity_main.xml? – seddonac Apr 24 '19 at 15:41
  • In MainActivity.java (java class not xml) which is linked to activity_main.xml – TheAnkush Apr 24 '19 at 15:48
  • Where abouts in the above code as I am getting unresolved reference errors on "firststing" and "secondstring" strings. – seddonac Apr 24 '19 at 16:10
1

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>
Son Truong
  • 13,661
  • 5
  • 32
  • 58
0

If you don't want to concat them in code, you could use this plugin I've created: https://github.com/LikeTheSalad/android-stem 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="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:

<!-- Auto generated during compilation -->
<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.

César Muñoz
  • 535
  • 1
  • 6
  • 10