A
A
Alexey R2014-07-15 14:35:08
Java
Alexey R, 2014-07-15 14:35:08

Is it possible using Enum to make the Layout toggle different colors?

Good day to all. I have such a task. I need to make the color of the Layout change when I click on the smartphone screen ... I solved this problem, but now I started to study enum (enumerations) and caught fire with such a question. Is it possible to switch Layout with different colors using Enum? And I can't seem to figure out how to do it. Can you point out what I'm doing wrong. Where to read what to solve this problem.

package com.example.screern;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

enum Colors {
  Red, Blue, Yellow;
}

public class MainActivity extends Activity implements OnClickListener{
  
  LinearLayout linearLayout1;
  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                
        linearLayout1 = (LinearLayout)findViewById(R.id.LinearLayout1);
        linearLayout1.setOnClickListener(this);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

  @Override
  public void onClick(View v) {
    Colors color;
    color = Colors.Red;
    color = Colors.Yellow;
    switch (color) {
    case Red:
      linearLayout1.setBackgroundColor(Color.RED);
      break;			
    case Yellow:
      linearLayout1.setBackgroundColor(Color.YELLOW);
      break;
    default:
      break;
    }
    
  }
}

I am attaching the code, maybe someone will tell you where there is an error in it.
The program works but displays only yellow color if you remove the line "color = Colors.Yellow;" then only red color and the switching itself does not occur ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anyd3v, 2014-07-15
@Axeles

Colors color;
color = Colors.Red;
color = Colors.Yellow;

there is an error, there should be an assignment of the next value, and you first assign red then assign yellow.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question