0

My current task is to set background color for several linear layouts according to the seek bar changes. I'm trying to store layouts in a linked HashMap as keys and use arrays with rgb integers as their values. I'm also using random class to set the default rgb values.

I've been thinking for a while about what structure to choose and decided to use linked hash map to have easy access to the rgb values in onStopTrackingTouch method and change them from default after seek bar has been moved.

But seems that I'm moving in a wrong direction. I stuck in lots of types and this code doesn't work, but it isn't the main question. What I'm really doubt about is that this structure will work for me. Is there any way to make it simplier?

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

private LinearLayout mTopLeftLayout;
private LinearLayout mBottomLeftLayout;

private LinearLayout mTopRigthLayout;
private LinearLayout mMiddleRightLayout;
private LinearLayout mBottomRightLayout;

LinkedHashMap<View, ArrayList<Integer[]>> colorMap = new LinkedHashMap<View, ArrayList<Integer[]>>();

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

    final SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
    seekBar.setOnSeekBarChangeListener(this);

    mTopLeftLayout = (LinearLayout)findViewById(R.id.topLeftLayout);

    mBottomLeftLayout = (LinearLayout)findViewById(R.id.bottomLeftLayout);
    colorMap.put(mBottomLeftLayout, null);

    mTopRigthLayout = (LinearLayout)findViewById(R.id.topRigthLayout);
    colorMap.put(mTopRigthLayout, null);

    mBottomRightLayout = (LinearLayout)findViewById(R.id.bottomRightLayout);
    colorMap.put(mBottomLeftLayout, null);

    setRandomBackground();

}

private void setRandomBackground(){

    Random rnd = new Random();

    for (Map.Entry<View, ArrayList<Integer[]>> color: colorMap.entrySet()){

        ArrayList<Integer[]> value = color.getValue();

        for (int i=0;i<3; i++) {
            value.add(rnd.nextInt(256));

        }

        int backgroundColor = Color.rgb(value[1], value[2], value[3]);
        color.setBackgroundColor(color);

    }

}

//seekBar methods goes here

}
Oleg
  • 11
  • 1

1 Answers1

0

I think you are using so much complex method for this, when we have easy and short for same as per my understanding of your query, so please find below solution for that.

First declare random variable as global like below,

  Random rnd;

Now, initialize it in onCreate() method as below,

 rnd = new Random();

Now check your main method for setting colors,

   private void setRandomBackground(){
         mTopLeftLayout.setBackgroundColor(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));  

         mBottomLeftLayout.setBackgroundColor(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));     

         mTopRigthLayout.setBackgroundColor(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

   }

So by this way you can easily set the background color as per random generator, and as we have declare it globaly so generally it will not generate duplicate values also.

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
  • Thanks, but I don't have any problems with setting random color for background. The task is to change background color in other methods and for this I need to know its previous value. So it has to be stored somewhere. As far as I'm working with several layouts and I need to store three values for each I'm trying to use collections for this task. – Oleg May 27 '16 at 14:02