I
I
ilyanovik2020-10-03 10:57:25
Android
ilyanovik, 2020-10-03 10:57:25

How to change button background on touch?

How to change button background on touch in Android Studio? I am new to this, so please explain in detail.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Carp, 2020-10-03
@ilyanovik

public class MainActivity extends AppCompatActivity {

    Button button;   //создаем кнопку

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button12);  //ссылаемся на id кнопки из лайаут

        final int[] colors = getResources().getIntArray(R.array.colors);   //создаем массив из color массива xml

        button.setOnClickListener(new View.OnClickListener() {     //ставим слушателя на кнопку, реагирует на косание

            @Override
            public void onClick(View v) {  //переопределяем метод
                final int randomColor = colors[new Random().nextInt(colors.length)];  //присваиваем переменной случайный цвет из массива
                button.setBackgroundColor(randomColor);  // ставим цвет на кнопку

            }
        });
    }
}

layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button12"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.432" />

</androidx.constraintlayout.widget.ConstraintLayout>

in colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>

    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>

<integer-array name="colors">
<item>@color/blue</item>
<item>@color/purple</item>
<item>@color/green</item>
<item>@color/orange</item>
<item>@color/red</item>
<item>@color/darkblue</item>
<item>@color/darkpurple</item>
<item>@color/darkgreen</item>
<item>@color/darkorange</item>
<item>@color/darkred</item>
</integer-array>

    </resources>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question