2

I have 4 different buttons. I want to change the background of the buttons at a fixed time (say 1 sec i.e. one button changes its color for one sec then retains its previous color and then other button does the same and so on) in certain random pattern, and then the user will repeat this pattern. However I am unable to change the background of the buttons randomly. I know that a timer or handler will b used but I have no idea ho to use it. Can anyone post a example program that shows how to change the background of buttons randomly?

here is my xml file: `

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>


<TextView
android:id="@+id/levelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="50dp"
android:textColor="#FFFFFF"
android:text = "" />

<TextView
android:id="@+id/countDnText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="100dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text=""
/>


<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="79dp" />

<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button6"
android:layout_alignTop="@+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button7"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />

<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button8"
android:layout_alignTop="@+id/button7"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

`

here is my Activity:`

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class EasyGameActivity extends AppCompatActivity
{

private int counter;
private TextView text;
private boolean flag = false;
private Button button = null;
private int i;

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

    startGame();
}

public void startGame()
{
    counter = 3;
    int temp;

    final Random rand = new Random();
    Handler handler = new Handler();


    while(true)
    {
        BinaryTree binaryTree = new BinaryTree(counter);


        for(int i = 0; i<counter; ++i)
        {
            temp = rand.nextInt(3);

            // yellow color button...
            if(temp == 0)
            {
                button = (Button) findViewById(R.id.button5);
                button.setBackgroundColor(Color.YELLOW);

            }


            else if(temp == 1)
            {
                button = (Button) findViewById(R.id.button6);
                button.setBackgroundColor(Color.BLUE);

            }

            else if(temp == 2)
            {
                button = (Button) findViewById(R.id.button7);
                button.setBackgroundColor(Color.RED);
            }


            else if(temp == 3)
            {
                button = (Button) findViewById(R.id.button8);
                button.setBackgroundColor(Color.GREEN);
            }


            //button.setBackgroundColor(Color.BLACK);

        }
        break;
    }

}

}`

  • Welcome!!! Could you provide the code you have so far? Thanks! – Mike Zalansky Dec 18 '15 at 17:29
  • I have posted my code, please take a look.... –  Dec 18 '15 at 18:07
  • what exactly should I write in those if conditions to do what I want? –  Dec 18 '15 at 18:14
  • First up, you probably will want to reset the previous `button`'s colour at the top of the `for` loop, with something like `if(null != button) button.setBackgroundColor(Color.BLACK);` If using a `while(true)` loop, you may want to use `Thread.Sleep` [example here](https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html) to pause the loop for the set duration. – Leejay Schmidt Dec 18 '15 at 18:24

2 Answers2

0

You could try something like this:

    Button[] changingButtons = new Button[4];
    changingButtons[0] = (Button)findViewById(R.id.button1_id);
    changingButtons[1] = (Button)findViewById(R.id.button2_id);
    changingButtons[2] = (Button)findViewById(R.id.button3_id);
    changingButtons[3] = (Button)findViewById(R.id.button4_id);

You can then access the corresponding button in the array and change the background colour using changingButtons[<desired index>].setBackgroundResource(<color resource>)

To retain the previous color, you can use ColorDrawable previousBackground = (ColorDrawable)changingButtons[<desired index>].getBackground();

Then, for the "set time" part, use a timer, and do the colour switching in the TimerTask.

If you wish to use a TimerTask would look something like this inside the method calling it:

    timer = new Timer();
    timer.schedule(new MyTask(buttonNumber), numMilliseconds);

And then MyTask would be an extension class of TimeTask

    class MyTask extends TimerTask
    {
            private int buttonId;
            public MyTask(int buttonId)
            {
                    super();
                    this.buttonId = buttonId;
            }
            public void run()
            {
                    //Do your button alterations here
            }
    }

Here's a sample I made using the above code on Ideone Hopefully this points you in the right direction!

For more information, check out these: Changing the background color programmatically, Getting the background color, Java Timer documentation

Community
  • 1
  • 1
Leejay Schmidt
  • 1,193
  • 1
  • 15
  • 24
0

You can use Collections.shuffle(colorList);

List<String> colorList=new ArrayList<>();
colorList.add("#108E44");
colorList.add("#000000ff");
colorList.add("#108E44");
for() {
   giveMeButtons(i).setBackgorundColor(Color.parseColor(colorList.get(i)));
}

And you must make other method. It will return random View button.

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
Numan Turkeri
  • 526
  • 4
  • 18