skip to Main Content

I tried to google and found that I need to add it to the strings.xml file but I didn’t understand what should I add?

<resources>
    <string name="app_name">Tester</string>
    <string name="Speed">TextView</string>
</resources>

I want to change the TextView name to Speed.

in the design in the text field of the textView I typed TextView then got this warning and now I want to change it to Speed but I don’t understand what should I put in the strings.xml

this is the content of the activit_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="184dp"
        android:layout_marginTop="108dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Tried to google but from all the answeres I didn’t understand what to put exactly in the string.xml file.

Update

I tried this: in the strings.xml file

<resources>
    <string name="app_name">Tester</string>
    <string name="Speed" />
</resources>

in the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:text="@string/Speed"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

but then in the design it’s showing in the text field @string/Speed and not only Speed.

it’s showing the whole @string/Speed in the text field of the textView.

2

Answers


  1. <resources>
        <string name="app_name">Tester</string>
        <string name="Speed">TextView</string>
    </resources>
    

    to

    <resources>
        <string name="app_name">Tester</string>
        <string name="Speed">Speed</string>
    </resources>
    

    Change the line:

    android:text="TextView"
    

    to:

    android:text="@string/Speed"
    
    Login or Signup to reply.
  2. In the strings.xml file we can define all the string text which will be used in the whole application. And we can refer to these strings from any of the activities.

    The way you defined you string in the file was wrong.

    We need to put the value of out string inside the string tag

    <string name="Speed">Speed</string>
    

    Where Speed is the value that will be displayed and name attribute is to refer our value from the xml or activity code.

    So in the Text View you can refer to this string as

     android:text="@string/Speed"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search