Muzahir Muzahir - 19 days ago 5
Android Question

change the colors of different buttons randomly at a specified time

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;
}

}


}`

Answer

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

Comments