L
L
LuVairo2021-02-20 16:20:29
Java
LuVairo, 2021-02-20 16:20:29

How to properly organize android Java code?

Hello, I'm writing a calculator app that conditionally has 5 buttons. The background (selector) property is set on each of them, in which the pressed and unpressed states are registered. Now the question is: in addition, I need to make the button change the color of its text when it is touched, and return to its original state when released. Do I need to write some cumbersome code that initializes each button, defines an OnTouchListener method, and then assigns a given listener to each of them, or is there a more elegant solution? Tell me who knows. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-02-20
@LuVairo

just like the background color, colors can also be specified.
In res/color you create a selector like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#f00" android:state_pressed="true"/>
    <item android:color="#00f" android:state_enabled="false"/>
    <item android:color="#0f0" />
</selector>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question