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

Viewed 8747

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

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

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

Try changing tools:text="Repost" to android:text="Repost"

As per documentation:

The tools namespace is a specially recognized namespace by the Android tools, so all the attributes you define on view elements in the tools-namespace will be automatically stripped when the application is packaged and there is no runtime overhead.

These are attributes which are used when the layout is rendered in the tool, but have no impact on the runtime. This is useful if you for example want to put sample data in your textfields for when you are editing the layout, but you don't want those attributes to affect your running app.

Hope this helps!

try changing width attribute from match_parents to wrap_contents.

Related