Skip to content
Advertisement

Android: How to cycle through different images with button click?

In my android program I am wanting to cycle through different images of traffic lights on the click of a button. Whenever the app loads it starts off with an image of a red light, and when I click it I want it to change the green light, and the another click to a yellow light. This is what I have in my Java file

package com.example.trafficsimulator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {

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

    }


    public void stopButton(View view){
        final Button button = findViewById(R.id.button);
        ImageView image = findViewById(R.id.redLightImage);
        button.setBackgroundColor(getResources().getColor(R.color.yellowlight));

        button.setText("Go");
        image.setImageResource(R.drawable.yellowlight);


    }

and XML file

   <?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">

    <ImageView
        android:id="@+id/redLightImage"
        android:layout_width="217dp"
        android:layout_height="372dp"
        android:layout_marginStart="97dp"
        android:layout_marginTop="61dp"
        android:layout_marginEnd="97dp"
        android:layout_marginBottom="298dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/redlight" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="156dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="167dp"
        android:layout_marginBottom="173dp"
        android:background="#BA1C1C"
        android:onClick="stopButton"
        android:text="@string/stop"
        android:textColor="@android:color/white"
        android:textColorHint="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/redLightImage"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

Advertisement

Answer

You can achieve your goal like this

Main Activity

    Button buttonChangeLight;
    ImageView imageLight;
    int counter = 0;
    
    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonChangeLight = findViewById(R.id.button);
        imageLight = findViewById(R.id.redLightImage);


        //to change lights
        changeLight();
    }

    //change you light
    public void changeLight(){
        buttonChangeLight.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(counter == 2){
           counter = 0;
           image.setImageResource(R.drawable.redLight);
        }else if(counter == 1){
           counter++;
           image.setImageResource(R.drawable.yellowLight);
        }else if(counter == 2){
           counter++;
           image.setImageResource(R.drawable.greenLight);
        }
      }
    });
  }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement