How do I get a string from resources using its name in Android?


Introduction

Android applications use different types of strings to be displayed inside different types of views such as text view, buttons and others. In most of the cases instead of hard coding the strings inside the code. It is preferred to add these strings inside the strings.xml file so that it becomes easy to modify and manage those strings across all the projects. Along with that this will help to enable internalization for the strings to be used within our application.

Implementation

We will be creating a simple application in which we will be displaying two text views. One for our heading and another for our message. In the first text view we will display text from our strings.xml file through our activity_main.xml file and in another text view, we will be setting the string to our text view from our java file.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 3 : Adding strings in string.xml file

Navigate to app>res>values>strings.xml file and add below code to it. Comments are added in the code to get to know in detail.

<resources>
   <string name="app_name">Android Application</string>
   <!-- we will be displaying below string inside our heading text view -->
   <string name="heading_str">String from resource using its name in Android</string>
   <!-- we will be displaying below string inside our message text view -->
   <string name="message_string">Welcome to Tutorials Point</string>
</resources>

Step 4 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- on below line creating a text view for displaying a heading-->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_marginStart="10dp"
      android:layout_marginEnd="10dp"
      android:gravity="center"
      android:padding="4dp"
      android:text="@string/heading_str"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="20sp"
      android:textStyle="bold" />

   <!-- creating a text view for displaying a message-->
   <TextView
      android:id="@+id/idTVMessage"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idTVHeading"
      android:layout_centerInParent="true"
      android:layout_marginStart="10dp"
      android:layout_marginEnd="10dp"
      android:gravity="center"
      android:padding="4dp"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="20sp"
      android:textStyle="bold" />

</RelativeLayout>

Explanation − In the above code we are creating a Relative layout as a root layout and inside that we are creating a simple text view in which we are displaying the heading for our application. After that we are creating one more text view inside which we will be displaying our message. In the first text view we are directly loading the message from our strings.xml file inside our heading text view. Where as in our second text view we will be setting the text from our java file.

Step 5 : Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // on below line we are creating variable for text view.
   private TextView msgTV;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // on below line initializing variable with id.
      msgTV = findViewById(R.id.idTVMessage);
      
      // on below line we are setting data to our text view from our string.xml file.
      msgTV.setText(getResources().getString(R.string.message_string));
   }

}

Explanation − In the above code we have firstly created the variable for the text view. After that we will get to see an onCreate method. Inside this onCreate method we are inflating a layout file in which we will be displaying our layout file. Inside this onCreate method we are initializing our text view with the id which we have given inside our activity_main.xml. After that we are setting data from our strings.xml file for this text view.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above article we have taken a look on How to use strings.xml file inside your android studio project to save the strings to be used. Along with that we have also taken a look on How to use the strings from strings.xml file inside your project.

Updated on: 30-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements