Java Code (Mad)
Java Code (Mad)
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;
@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();
}
}
});
}
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);
}
}