package com.example.lab5; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Use Linear Layout class for creating linear layout LinearLayout secondLayout = new LinearLayout(this); // Change Parameter to Vertical secondLayout.setOrientation(LinearLayout.VERTICAL); secondLayout.setPadding(16, 16, 16, 16); // get the intent parameters passed from previous Intent intent = getIntent(); String username = intent.getStringExtra("username"); String password = intent.getStringExtra("password"); // Create a TextView dynamically to display username and password TextView welcomeText = new TextView(this); welcomeText.setText("Welcome " + username); welcomeText.setTextSize(18); welcomeText.setPadding(0, 0, 0, 20); // Create a Spinner dynamically for color selection Spinner colorSpinner = new Spinner(this); String[] colors = {"RED", "GREEN", "BLUE", "YELLOW", "WHITE"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, colors); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); colorSpinner.setAdapter(adapter); // Set listener for color selection colorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { String selectedColor = parentView.getItemAtPosition(position).toString(); // Change the background color based on the selected color switch (selectedColor) { case "RED": secondLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark)); break; case "GREEN": secondLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark)); break; case "BLUE": secondLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark)); break; case "YELLOW": secondLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_light)); break; case "WHITE": secondLayout.setBackgroundColor(getResources().getColor(android.R.color.white)); break; } } @Override public void onNothingSelected(AdapterView<?> parentView) { // Do nothing } }); // Create a Button dynamically to go back to MainActivity Button backButton = new Button(this); backButton.setText("Go Back"); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Start MainActivity and finish SecondActivity Intent intent = new Intent(SecondActivity.this, MainActivity.class); startActivity(intent); finish(); } }); // Add views to the layout secondLayout.addView(welcomeText); secondLayout.addView(colorSpinner); secondLayout.addView(backButton); // Set the dynamic layout as the content view setContentView(secondLayout); } }