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

Android Traffic Light Simulation Code

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)
59 views2 pages

Android Traffic Light Simulation Code

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

 Java code

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

private ImageView redLight, yellowLight, greenLight;


private Button startButton;
private boolean isRunning = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

redLight = findViewById([Link].red_light);
yellowLight = findViewById([Link].yellow_light);
greenLight = findViewById([Link].green_light);
startButton = findViewById([Link].start_button);

[Link](new [Link]() {
@Override
public void onClick(View v) {
if (!isRunning) {
startTrafficSimulation();
}
}
});
}

private void startTrafficSimulation() {


isRunning = true;
[Link](false); // Disable the button during simulation
final Handler handler = new Handler();
[Link](new Runnable() {
@Override
public void run() {
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](new Runnable() {
@Override
public void run() {
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](new Runnable() {
@Override
public void run() {
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](new Runnable() {
@Override
public void run() {

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);
[Link](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