Android application error: the theme is tied to a specific button

I am making a simple calculator for android (my first application) and I have problems that puzzle over when I try to apply a specific android: theme to a button.

The problem occurs when a button with a certain theme tries to execute an activity method in an onclick event. Based on what I searched in StackOverflow, it looks like the "context" of a button with a specific theme is different from the activity context, and because of this, it cannot find my method that processes the onclick recorded in the action.

There is my style.xml where I define the application 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 xml layout:

 <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 the change to “android: theme” with “style”, although this solves the problem, the new color colorButtonNormal does not apply: (.

Help me D:

PD: Sorry for my bad English

+5
source share
1 answer

I answered a similar question here , where you can get a little more background.

A possible solution to your problem is not to use android:onClick="onClickButton" , but to set onClickListener in the code. Thus, you can save the Button theme. The official docs have an example of how to do this.

+3
source

All Articles