Switching the background of an android app, based on a selected color form a spinner

Viewed 52

I would like to ask how can I change the background color of an android app /provided that I have an array of colors/ Below I apply the code that I tried to write in order to do this task: The task is to change the background color of the app, based on the currently chosen color from the dropdown list(spinner). I would be very grateful if you could give me some hints on how can I fix this issue.

package com.example.assignment3_yoanivanov_322321010;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    LinearLayout linearLayout;
    Spinner spinner;
    String[] items = {"red","green","blue","orange","yellow","gray"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //textView = (TextView) findViewById(R.id.txtMsg);
        spinner = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_dropdown_item, items);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String color  = parent.getItemAtPosition(position).toString();
        //Toast.makeText(parent.getContext(),color,Toast.LENGTH_LONG).show();
        switch(color){
            case "red":
                linearLayout.setBackgroundColor(Color.RED);
            case "green":
                linearLayout.setBackgroundColor(Color.GREEN);
            case "blue":
                linearLayout.setBackgroundColor(Color.BLUE);
            case "orange":
                linearLayout.setBackgroundColor(Color.rgb(255, 165, 0));
            case "yellow":
                linearLayout.setBackgroundColor(Color.YELLOW);
            case "GRAY":
                linearLayout.setBackgroundColor(Color.GRAY);
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
2 Answers

Your problem might be that

LinearLayout linearLayout;

has got no assigned value (I don't see it in your code).

Assign the value like you did it with spinner

linearLayout = (LinearLayout) findViewById(R.id.**your_layout_id**);

and then it should be ok

You had some issues that I will mention in the points below:

1. As @Drogheda said you missing the LinearLayout, but that isn't the main problem.

Note: You can leave out a redundant definition like e.g. Spinner = (Spinner) findViewById(R.id.spinner); if you have a variable Spinner spinner; above.

Ergo: Remove (Spinner) or write Spinner spinner = findViewById(R.id.spinner);


2. In your Switch you forgot to set break; after you set the color. It will run through every color without a break, that's why nothing change.


3. You need to have spinner.getSelectedItem() instead of parent.getItemAtPosition() if you want to compare the String names. Otherwise you need to provide an Integer and not a String.


Take a look at my code. There are some little improvements made that you can learn from how to make a better approach.

I provide my full code starting with the MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

        Spinner spinner = findViewById(R.id.mySpinner);
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, Arrays.asList("red", "green", "blue", "orange", "yellow", "gray"));
        spinner.setAdapter(arrayAdapter);
        LinearLayout linearLayout = findViewById(R.id.linear_layout);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String color = spinner.getSelectedItem().toString();
                switch (color) {
                    case "red":
                        linearLayout.setBackgroundColor(Color.RED);
                        break;
                    case "green":
                        linearLayout.setBackgroundColor(Color.GREEN);
                        break;
                    case "blue":
                        linearLayout.setBackgroundColor(Color.BLUE);
                        break;
                    case "orange":
                        linearLayout.setBackgroundColor(Color.rgb(255, 165, 0));
                        break;
                    case "yellow":
                        linearLayout.setBackgroundColor(Color.YELLOW);
                        break;
                    case "gray":
                        linearLayout.setBackgroundColor(Color.GRAY);
                        break;
                    default:
                        linearLayout.setBackgroundColor(Color.RED);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

And also it's layout activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your LinearLayout switch color through Spinner"
        android:textColor="@color/white"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/mySpinner"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center" />

</LinearLayout>

RESULT:

Spinner controls colors of LinearLayout

Related