Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button texts disappear on KitKat (API level 19)

The main menu of my app (a game) uses standard Android buttons. It works fine on all my devices except on Nexus 7 with Android 4.4.2. The issue is as follows:

The text of a button suddenly disappears in either of these cases:

  • the button is pressed (it happens immediately when I touch it, no need to release it),
  • setEnabled(boolean) is called on the button

For example, if I press "Load game", the button is correctly highlighted during the press event, but "Load game" disappears completely (the button has empty text).

If I remove all custom styles and behaviors, and use only the default Android buttons with default font etc., the problem still persists.

If I decrease targetSdkVersion to 18 (from 19), everything works fine even on Nexus 7.

Any idea what has changed in KitKat in this respect? I didn't find anything suspicious.

My revalant XML code:

<TableLayout
    android:id="@+id/layoutGameMainmenu"
    android:layout_width="wrap_content"
    android:layout_height="83dip"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:visibility="gone" >

    <TableRow>

        <Button
            android:id="@+id/buttonLoadGame"
            android:layout_width="174dip"
            android:layout_height="40dip"
            android:layout_marginBottom="2dip"
            android:layout_marginRight="30dip"
            android:background="@drawable/button_gamemainmenu"
            android:text="buttonLoadGame"
            android:textColor="@color/button_gamemainmenu_text"
            android:textScaleX="0.9"
            android:textSize="16dp" />

        <Button
            android:id="@+id/buttonSettings"
            android:layout_width="174dip"
            android:layout_height="40dip"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="30dip"
            android:background="@drawable/button_gamemainmenu"
            android:text="buttonSettings"
            android:textColor="@color/button_gamemainmenu_text"
            android:textScaleX="0.9"
            android:textSize="16dp" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/buttonStartGame"
            android:layout_width="174dip"
            android:layout_height="40dip"
            android:layout_marginBottom="1dip"
            android:layout_marginRight="30dip"
            android:background="@drawable/button_gamemainmenu"
            android:text="buttonStartGame"
            android:textColor="@color/button_gamemainmenu_text"
            android:textScaleX="0.9"
            android:textSize="16dp" />

        <Button
            android:id="@+id/buttonQuit"
            android:layout_width="174dip"
            android:layout_height="40dip"
            android:layout_marginBottom="1dip"
            android:layout_marginLeft="30dip"
            android:background="@drawable/button_gamemainmenu"
            android:text="buttonQuit"
            android:textColor="@color/button_gamemainmenu_text"
            android:textScaleX="0.9"
            android:textSize="16dp" />
    </TableRow>
</TableLayout>

Important notes regarding the above code:

  • The game has a two-row main menu (with two buttons in each row, so altogether 4 buttons), and this main menu is positioned on the bottom of the screen
  • Literal texts are placeholders only, because the game has an own file format for texts, and reads data from there when the Activity is created
  • The issue persists even if I completely remove the android:textColor and android:background attributes. In this case, my buttons will have a default appearance (instead of their game specific style), but the issue persists.
  • To emphasize again: the above code works perfectly on all (tested) devices except Nexus 7 (all my devices except Nexus 7 are pre-KitKat devices)

Finally, some information about my global styles/themes:

In AndroidManifest, I set my Application theme to MyCustomTheme. The content of mycustomtheme.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyCustomTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:soundEffectsEnabled">false</item>
</style>
</resources>

Finally, my styles.xml is as follows (but it seems I don't refer its styles from anywhere, this seems to be old code from the time we made the game fullscreen, or does Android use it anywhere by default?):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
    </style>

    <style name="Theme.TranslucentWoTitle" parent="android:Theme.Translucent">
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>
like image 441
Thomas Calc Avatar asked May 11 '14 03:05

Thomas Calc


People also ask

How do I get button text on Kotlin?

To set Android Button text, we can assign android:text XML attribute for Button in layout file with the required Text value. To programmatically set or change Android Button text, we can pass specified string to the method Button. setText(new_string_value).

Is Button a view in Android?

In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it.

What is button discuss the working of Button with example?

In Android, Button represents a push button. A Push buttons can be clicked, or pressed by the user to perform an action. There are different types of buttons used in android such as CompoundButton, ToggleButton, RadioButton. Button is a subclass of TextView class and compound button is the subclass of Button class.

What is the use of button control in Android?

In android, Button is a user interface control that is used to perform an action whenever the user clicks or tap on it. Generally, Buttons in android will contain a text or an icon or both and perform an action when the user touches it.


2 Answers

Looking at your XML file, I still believe that there is something going wrong in your project.


This minimal example does not reproduce the problem on a Nexus 7 2013 4.4.2 (minSDK=8, targetSDK=19):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Taking your XML and removing backgrounds / textcolors does not reproduce it either:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/layoutGameMainmenu"
        android:layout_width="wrap_content"
        android:layout_height="83dip"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <TableRow>

            <Button
                android:id="@+id/buttonLoadGame"
                android:layout_width="174dip"
                android:layout_height="40dip"
                android:layout_marginBottom="2dip"
                android:layout_marginRight="30dip"
                android:text="buttonLoadGame"
                android:textScaleX="0.9"
                android:textSize="16dp" />

            <Button
                android:id="@+id/buttonSettings"
                android:layout_width="174dip"
                android:layout_height="40dip"
                android:layout_marginBottom="2dip"
                android:layout_marginLeft="30dip"
                android:text="buttonSettings"
                android:textScaleX="0.9"
                android:textSize="16dp" />
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/buttonStartGame"
                android:layout_width="174dip"
                android:layout_height="40dip"
                android:layout_marginBottom="1dip"
                android:layout_marginRight="30dip"
                android:text="buttonStartGame"
                android:textScaleX="0.9"
                android:textSize="16dp" />

            <Button
                android:id="@+id/buttonQuit"
                android:layout_width="174dip"
                android:layout_height="40dip"
                android:layout_marginBottom="1dip"
                android:layout_marginLeft="30dip"
                android:text="buttonQuit"
                android:textScaleX="0.9"
                android:textSize="16dp" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

Lastly, I'm noticing android:textColor="button_gamemainmenu_text", which is not default Android behavior and suggests that you're handling text color in your own way. I strongly believe that removing this custom behavior will solve the problem.

like image 180
nhaarman Avatar answered Oct 13 '22 23:10

nhaarman


Maybe you could get some new insights using the "Hierarchy View" - Perspective in Eclipse to debug your layout? It's quite useful as you can read the measured layout parameters at runtime. At least it should guide you to the right track.

like image 39
Mr.Radar Avatar answered Oct 13 '22 22:10

Mr.Radar