'Keep getting an error saying I should use '@string' resource for my XML on Android Studio
I'm using Android Studio and I'm new to this.
For some of the objects like the last button object, the system tells me "Hardcoded string 'Message', should use'@string' resource". What does this mean?
I've searched online and some say to use <resource> but I don't know what this means. How can I fix this?
For example, in the first ImageView, an error pops up here android:contentDescription="homeImage"
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="wrap_content"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView6"
android:layout_width="135dp"
android:layout_height="130dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="138dp"
android:contentDescription="homeImage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round"
tools:ignore="ImageContrastCheck" />
<TextView
android:id="@+id/textView"
android:layout_width="150dp"
android:layout_height="55dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="136dp"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/followButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginEnd="88dp"
android:text="Follow"
app:layout_constraintEnd_toStartOf="@+id/messageButton"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/messageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginEnd="72dp"
android:text="Message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Solution 1:[1]
It's warning not an error. And only to make it easier to reuse strings and also for translation
Solution 2:[2]
It is a warning that tells you that you would better use the @string xml file to have your texts at .
In Android studio, a resource is a localized text string, bitmap, layout, or other small piece of noncoded information that your app needs. At build time, all resources get compiled into your application.
for example your layout you are presenting above is a resource file that goes in @layout folder you also can use @drawable folder to save your images vectors ... and there is plenty of other folders android studio provide to control your and to make it easier for you and for the system . you might at the beginning find it hard but after some time you will like it .
for more information ,have a look at App resources overview
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kamal douma |
| Solution 2 | Abood Moorad |
