0

I've created some custom themes, which help me customize the layouts based on user preferences.. in my App (Quiz-App) I'm changing the button color if it was the correct answer. After that I show a "correctAnswer"-Dialog with an onClick where the buttons should be reseted and get their normal color again. But since I'm using custom themes I'm not able to achief this. Any help ist very welcome!

My theme.xml:

    <style name="Theme.Custom1" parent="Theme.AppCompat.NoActionBar">
        <item name="background">@color/Red</item>
        <item name="arrow">@color/RedDark</item>
        <item name="toolbar">@color/RedDark</item>
        <item name="text">@color/Gold</item>
        <item name="button">@drawable/button_custom1</item>
    </style>

My Layout.xml where custom theme is first assigned:

        <Button
            android:id="@+id/buttonA2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="9dp"
            android:layout_marginLeft="9dp"
            android:layout_marginEnd="9dp"
            android:layout_marginRight="9dp"
            android:layout_marginBottom="16dp"
            **android:background="?button"**
            android:onClick="buttonA2"
            android:textColor="@color/white"
            app:layout_constraintBottom_toTopOf="@+id/buttonB2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

Code where I change color to green and Dialog for reset:

    public void buttonA2(View view) {
        if (currentQuestion.getOptb().equals(currentQuestion.getAnswer())) {
            **buttonA2.setBackground(getResources().getDrawable(R.drawable.button_correct));**
            if (qid < list.size() - 1) {
                disableButton();
                **correctDialog();**
            } else {
                gameWon();
            }
        } else {
            gameLostPlayAgain();
        }
    }

resetColor() ist called in the correctDialog:

    public void resetColor() {
        buttonA2.setBackground(getResources().getDrawable(R.drawable.button_custom));
        buttonB2.setBackground(getResources().getDrawable(R.drawable.button_custom));
        buttonC2.setBackground(getResources().getDrawable(R.drawable.button_custom));
        buttonD2.setBackground(getResources().getDrawable(R.drawable.button_custom));
    }

That resetColor worked before I started using custom themes.. now I have 4 different themes set on Users preferences and not only one drawable "button_custom", but 4 of them (button_custom1 to 4). So long story short: in resetColor I need to know which custom theme was used on the button before I changed the color to green and set the background to that custom theme again(e.g. the drawable inside the custom theme).

Many thanks in advance!

EDIT: I already tried some stuff like this without any usefull result:

Drawable buttonBack = buttonA2.getBackground();
String buttonBackground = buttonBack.toString();
System.out.println(buttonBackground);
int i = getResources().getIdentifier("tb.quiz:drawable/" + buttonBackground, null, null);
System.out.println(i);
buttonA2.setBackground(getResources().getDrawable(i));

Another try was with Resources.Theme buttonBack = this.getTheme(); instead of Drawable buttonBack

Heaf
  • 15
  • 1
  • 7

1 Answers1

0

In case someone faces the same problem, I found another question which gave me the hint: https://stackoverflow.com/a/53884954/7671760

Using typeArry buttonDraw I saved the default button theme in onCreate --> buttonDraw = getTheme().obtainStyledAttributes(new int[]{R.attr.button});

then changed my resetColor() to:

buttonA2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonB2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonC2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonD2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));

Don't know if that's the smartest way, but it worked for me.

Cheers!

Heaf
  • 15
  • 1
  • 7