I am trying to set text in my MainActivity.java, but the text fails to appear in emulator.
My 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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="204dp"
android:text="Show Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="2dp"
android:layout_height="17dp"
android:layout_marginTop="76dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
My MainActivity.java
> package com.example.shownamenow;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button myButton;
private TextView showText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = findViewById(R.id.button);
showText = findViewById(R.id.textView);
showText.setText("Hello I am here!");
}
}
I get error in the MainActivity.java saying "string literal in setText cannot be translated. Use android resources...". So I used string resources instead: showText.setText(R.String.Hello) and I created the resource in strings.xml with value of "Hello I am here". Still when I load the app in the emulator, the text refuses to appear. What am I doing wrong???
This is because width for text view it is very small , please check below code
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="204dp"
android:text="Show Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
As you can see in your xml file,
<TextView
android:id="@+id/textView"
android:layout_width="2dp"
android:layout_height="17dp"
android:layout_marginTop="76dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
Your width attribute is too small to be seen. Try using a bigger value like,
android:layout_width="50dp"
As mentioned in both of the other answers - your android:layout_width="2dp"
attribute makes your textView to be too small to be noticed.
Both of the above answers will work for your case but I want to add something
Even if you will change your layout width to 50dp
for example there is no guaranty that you will see your view on the screen.
Because different phones got different screen size when using fixed size dimensions some view will look good on one device but may not look good on another device (Your view may not be seen on the screen if you put too many views with fixed size).
If you already choose to use ConstraintLayout
you can use these two attributes to make your view responsive to the screen size:
app:layout_constraintWidth_percent
app:layout_constraintHeight_percent
For example:
<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent=".2"
app:layout_constraintHeight_percent=".1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
In the above example, I told my view to be equal to 20%
of the screen size in width and 10%
in height.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.