Well, I'm developing a simple application that when started starts displaying random images and text on the screen just for learning and what I have so far is this:
package br.com.appteste;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class RunActivity extends ActionBarActivity {
Timer timer = new Timer();
int delay = 1000;
int interval = 1000;
ImageButton imBt_Post;
TextView tv_Post, tv_setScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_running);
imBt_Post = (ImageButton) findViewById(R.id.ImageSelectGame);
tv_Post = (TextView) findViewById(R.id.TextSelectGame);
tv_setScore = (TextView) findViewById(R.id.TextScoreGame);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
int[] varButtonImages = {R.drawable.circle_blue,
R.drawable.circle_green,
R.drawable.circle_pink,
R.drawable.circle_red,
R.drawable.circle_yellow};
final int buttonSelect = varButtonImages[new Random().nextInt(varButtonImages.length)];
imBt_Post.setTag(buttonSelect);
imBt_Post.post(new Runnable() {
@Override
public void run() {
imBt_Post.setBackgroundResource(buttonSelect);
}
});
String[] varTextColor = {"Azul","Verde","Rosa","Vermelho","Amarelo"};
final String textSelect = varTextColor[new Random().nextInt(varTextColor.length)];
tv_Post.post(new Runnable() {
@Override
public void run() {
tv_Post.setText(textSelect);
}
});
}
}, delay, interval);
imBt_Post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I'm trying from this code to find a way that when the user clicks on the ImageButton "imBt_Post" the comparison happens to see if the text that was drawn and set in the TextView "tv_Post" matches the color of the circle (Image) set in ImageButton, someone could help me in how to do this because at the moment I have no idea how to do this.