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:
setEnabled(boolean)
is called on the buttonFor 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:
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.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>
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).
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With