Parker
Parker

Reputation: 8851

TextView showing on design preview, but not in actual Android app

I have an XML layout in a second Activity that shows one of two LinearLayouts based on if a user is logged in or not, and the TextView I have never shows the text in the app, but it does render it correctly when I look at the "Design" panel in the layout editor

The TextView is there (if I set a background color, it shows that color) but the Text itself never renders.

Clicking a button on MainActivity results in starting the LoginActivity, which has the onCreate (it's Kotlin):

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_layout)

And layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        android:id="@+id/loggedOut"
        android:padding="24dp">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/passName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:focusable="true"
                android:hint="Username"
                android:inputType="textCapCharacters" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/passPin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:focusable="true"
                android:hint="PIN"
                android:inputType="numberPassword" />
        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/passSubmit"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login" />
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/loggedIn"
        android:visibility="visible"
        android:padding="24dp">


        <TextView
            android:id="@+id/loggedInMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="You're currently logged in"
            android:textAlignment="center"
            android:textAppearance="@android:style/TextAppearance.Material.Headline" />

        <Button
            android:id="@+id/passLogout"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Logout" />
    </LinearLayout>

</FrameLayout>

This is what Android Studio shows (those errors are TextInputLayout bugs where it says it can't find the hide password icon and such) What Android Studio shows me

And this is what my device shows
What I get on my device

Edit: I've removed everything in the layout file except for the TextView and a parent LinearLayout and it's still not showing.

Upvotes: 1

Views: 2408

Answers (1)

Parker
Parker

Reputation: 8851

Found the issue. Switching android:text="You're currently logged in" to android:text="You\'re currently logged in" fixed it. So I guess Android requires escaping single quotes when setting the text in XML?

Found by using the layout inspector tool, which showed mText as having no value

Upvotes: 2

Related Questions