Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text not showing up on android, but does show in preview in android studio

I am trying to build my first Android app using Android Studio and Firebase. I have it all connected and it is showing the content from the database and images from the firebase storage just fine. The problem is, for some reason my text isn't showing up that I added into the xml. At the bottom of the post there are 3 buttons, "like", "comment", and "re-post" they have an icon, then text next to them. The icons are showing up perfectly, but the text will not show up. Here is the "include_post_actions.xml" where the problem lies...

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_below="@+id/post_action_layout"
    android:layout_above="@+id/include"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_weight="1"
    android:gravity="center_vertical">

    <LinearLayout
        android:id="@+id/post_action_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <com.like.LikeButton
                app:icon_type="heart"
                app:icon_size="18dp"
                android:id="@+id/star_button"
                android:layout_width="18dp"
                android:layout_height="18dp" />
            <TextView
                android:id="@+id/post_likes_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textColor="@color/colorBlack"
                android:maxLines="1"
                tools:text="Like" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <ImageView
                android:id="@+id/post_comment_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_question_answer_black_24dp" />
            <TextView
                android:id="@+id/post_comments_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textColor="@color/colorBlack"
                android:maxLines="1"
                tools:text="Comment" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <ImageView
                android:id="@+id/post_repost_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_autorenew_black_24dp" />
            <TextView
                android:id="@+id/post_repost_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textColor="@color/colorBlack"
                android:maxLines="1"
                tools:text="Repost" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/post_action_buttons">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:gravity="center_vertical"
            tools:text="Likes Count"
            android:id="@+id/like_count_text"
            android:maxLines="1" />
    </LinearLayout>
    
</RelativeLayout>

In the preview the text shows up right next to the icons, but when I run it on an emulator only the icons are there, and I can't figure out why. Please help. Thank you. The preview in Android Studio

enter image description here

The app in an emulator... enter image description here

like image 661
Neglected Sanity Avatar asked Sep 22 '17 22:09

Neglected Sanity


People also ask

What does text view do in Android Studio?

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.

Is a view in Android which is capable of showing text?

The Android TextView component is a View subclass which is capable of showing text.

Can we set text to imageView Android?

With a FrameLayout you can place a text on top of an image view, the frame layout holding both an imageView and a textView.


2 Answers

The issue is that you are using tools:text="Repost". That only displays in the preview mode, you need to use android:text="Repost" to actually display it.

The tools namespace is for editor purposes only and is a great way to align things without actually setting values. If you want to actually display the text, however, you need to use the android namespace.

like image 164
RestingRobot Avatar answered Nov 02 '22 23:11

RestingRobot


tools attribute reference allows you to enforce any specific attributes only for preview in Android Studio so that you can see the text in editor. Setting text by android:text will hardcode string in TextView. Also you can change text by setText method on the TextView object

like image 38
Valeriy Posvistak Avatar answered Nov 02 '22 23:11

Valeriy Posvistak