1
Good people, I’m developing a simple application that when started is 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 am trying from this code to find a way that when the user click on the Imagebutton "imBt_Post" happens the comparison to see if the text that was drawn and set in Textview "tv_Post" corresponds to 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.
Isn’t it easier for you to work with indexes? Why not save the selected item index as instance variable and compare between the two indices?
– Wakim
sorry... I’m a beginner.. could help me do that?
– Jeiferson
Okay, I’m putting together an explanation of what I’m talking about.
– Wakim
I’m on hold :)
– Jeiferson