Question

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>
Was it helpful?

Solution

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.

OTHER TIPS

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.

I had faced the same issue. The text disappears on Nexus 7 & Nexus 10.

Scenario in which the bug occurred:

  1. Use the following layout.
  2. Set text to the textView on the click of the button.

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >
    
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Record"
                android:textColor="@android:color/black" />
        </TableRow>
    </TableLayout>
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />
    

To fix the issue, you could either switch from TableLayout to LinearLayout or set width for your buttons. This was what worked for me.

Hi I have tested this with your custom styles. I have used hardcoded color because i could not use the text color what you have used. it is working on all devices. i could not test it on nexus 7. because i dont have this device. Hopefully it will work in nexus7. Kindly check with updated file and let me know the feedback. If you have used any selectors then post the selectors files.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:orientation="vertical" >

<TableLayout
    android:id="@+id/layoutGameMainmenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="5dp"
    android:visibility="visible" >

    <TableRow>

        <Button
            android:id="@+id/buttonLoadGame"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="2dip"
            android:layout_marginRight="30dip"
            android:text="buttonLoadGame"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />

        <Button
            android:id="@+id/buttonSettings"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="30dip"
            android:text="buttonSettings"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/buttonStartGame"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="1dip"
            android:layout_marginRight="30dip"
            android:text="buttonStartGame"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />

        <Button
            android:id="@+id/buttonQuit"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="1dip"
            android:layout_marginLeft="30dip"
            android:text="buttonQuit"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />
    </TableRow>
</TableLayout>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top