0% found this document useful (0 votes)
35 views2 pages

Java Code (Mad)

The document contains Java code to simulate a traffic light by changing the visibility of red, yellow and green image views over time using handlers. It imports necessary classes, defines views and a button, and implements a click listener to start the simulation by toggling light visibility with delays.

Uploaded by

prachivkadam29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Java Code (Mad)

The document contains Java code to simulate a traffic light by changing the visibility of red, yellow and green image views over time using handlers. It imports necessary classes, defines views and a button, and implements a click listener to start the simulation by toggling light visibility with delays.

Uploaded by

prachivkadam29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

 Java code

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ImageView redLight, yellowLight, greenLight;


private Button startButton;
private boolean isRunning = false;

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

redLight = findViewById(R.id.red_light);
yellowLight = findViewById(R.id.yellow_light);
greenLight = findViewById(R.id.green_light);
startButton = findViewById(R.id.start_button);

startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isRunning) {
startTrafficSimulation();
}
}
});
}

private void startTrafficSimulation() {


isRunning = true;
startButton.setEnabled(false); // Disable the button during simulation
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
redLight.setVisibility(View.VISIBLE);
yellowLight.setVisibility(View.INVISIBLE);
greenLight.setVisibility(View.INVISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
redLight.setVisibility(View.INVISIBLE);
yellowLight.setVisibility(View.VISIBLE);
greenLight.setVisibility(View.INVISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
redLight.setVisibility(View.INVISIBLE);
yellowLight.setVisibility(View.INVISIBLE);
greenLight.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {

redLight.setVisibility(View.INVISIBLE);

yellowLight.setVisibility(View.INVISIBLE);

greenLight.setVisibility(View.INVISIBLE);
startButton.setEnabled(true); //
Enable the button after simulation
isRunning = false;
}
}, 5000); // Green light duration
}
}, 3000); // Yellow light duration
}
}, 5000); // Red light duration
}
}, 0);
}
}

You might also like