3

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???

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Share error log. – Taseer Sep 15 '19 at 06:48
  • there is no point to have `layout_width="wrap_content"` together with `constraintEnd_toEndOf="parent"` and `constraintStart_toStartOf="parent"`as it will stretch it anyway so `layout_width="wrap_content"` doesnt take any effect. The problem here is that the height is to small and if you do not provide both constraints for it just use `layout_height="wrap_content"`. – RadekJ Sep 15 '19 at 07:00
  • 1
    set textview width to 0dp or match parent – Ashok Kumar Sep 15 '19 at 07:16

3 Answers3

0

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>
Emad Seliem
  • 608
  • 1
  • 4
  • 5
  • as you have End_toEndOf and Start_toStartOf it is a good practice to replace android:layout_width="wrap_content" with android:layout_width="0dp" for optimization purposes – RadekJ Sep 15 '19 at 07:01
0

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"
dSanders
  • 165
  • 11
0

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

What I want to add in my answer:

  • 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.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53