Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crash when android:theme is aplied to a specific button

Tags:

android

themes

i am doing a simple calculator aplication for android (my first app) and i have problems that are breaking my head when i try to apply a specific android:theme to a button.

The problem comes out when the button with the specific theme try to execute an activity method in onclick event. Based on what I 've searched in StackOverflow, is like the "context" of the button with the specific theme is different from the activity context, and because of that it cant find my method that handles the onclick written in the activity.

There is my style.xml where i define my aplication theme and my specific button theme:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorButtonNormal">#dc000000</item>
        <item name="android:background">#dc262626</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="contextButtonTheme" parent="AppTheme">
        <item name="colorButtonNormal">@color/contextButtonsColor</item>
    </style>

</resources>

There is my button in the layout xml:

<Button
    android:layout_width="0dp"
    android:layout_weight="25"
    android:layout_height="match_parent"
    android:text="X"
    android:id="@+id/multButton"
    android:textSize="11pt"
    android:theme="@style/contextButtonTheme"
    android:onClick="onClickButton"/>

The solution i read here is that change "android:theme" with "style", although this solves the crash, the colorButtonNormal new color is dont applied :(.

Pls help me D:

PD: Sorry for my bad english

like image 219
Hawky Avatar asked Oct 19 '22 03:10

Hawky


1 Answers

I've answered a similar question here, where you can get a bit more background.

A possible solution to your problem is to not use android:onClick="onClickButton" but set an onClickListener in code. This way you can keep your themed Button. The official docs have an example how to do this.

like image 154
ci_ Avatar answered Oct 29 '22 21:10

ci_