Buttons invisible in emulator but visible in preview

Asked

Viewed 977 times

5

Why do buttons I create in xml not appear on Avd? In the layout they appear with a gray color, but when I touch to run in the emulator they are simply invisible. If I put some text on the button, the text appears, but the button does not. I’m following the tutorial to create this game and did nothing different, however mine is like this.

Android Studio mostrando uma prévia do layout criado, no qual aparece uma grade de botões cinzas sem nenhum texto dentro deles

Emulador de Android executando a aplicação. A tela está vazia, como se os botões estivessem invisíveis

my xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado1"
            android:onClick="clickQuadrado" />

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado2"
            android:onClick="clickQuadrado" />

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado3"
            android:onClick="clickQuadrado" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado4"
            android:onClick="clickQuadrado" />

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado5"
            android:onClick="clickQuadrado" />

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado6"
            android:onClick="clickQuadrado" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado7"
            android:onClick="clickQuadrado" />

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado8"
            android:onClick="clickQuadrado" />

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:tag="quadrado9"
            android:onClick="clickQuadrado" />
    </LinearLayout>
</LinearLayout>
  • 1

    It is interesting also you show the code that is on main.xml

  • Gustavo, unless you have other definitions in your Activity For these buttons, all you do is just define dimensions and click action practically. You need a background, a text or something like that. For that you need the properties android:background or android:text, for example. To contextualize, you have the link of this tutorial that is following?

  • Okay, thank you. I am following the tutorial of the channel eXcript https://www.youtube.com/watch?v=FsJkaAnd9uY. Another question, when I give a margin in the first Linear Layout (most external), the changes are applied only in the XML preview, the emulator is the same, unless I apply the changes from the second Linear layout, then it works right. Only then you’d cost more lines... Thank you

  • I forgot to comment that in the tutorial I mentioned in which I am following, it does not apply any background to Buttons, which I thought would be some configuration error in my.

  • Gustavo, take a look if it is not the theme you are using that is making the button invisible (because they have the same color). What happens when you press the place where the button should be? He replies?

  • Good Afternoon @Gustavo, I tested your xml and it’s showing all the buttons. I believe it’s something you’re doing in your class. Any questions I’m willing to help you.

  • Gustavo, I tested it here and it’s working fine. But anyway, if it doesn’t appear there, you can try changing the color of the buttons. Clicking on the button>going to properties (right corner of the screen)>background>color.. So you can set a different color. If the buttons are the same color as the theme, this will already solve. Also check that your java class has no setVisibility(View.INVISIBLE) method; ..

Show 2 more comments

1 answer

2

I tested in the Android Studio your xml, it shows the button normally on my virtual machine.

Try this scope for each button:

<Button
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/buttonX"
                android:layout_gravity="center_horizontal" 
                android:onClick="clickQuadrado"/> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.