I want to bulid a reaction game where you have to click on a colour changing button when it's the right color (red). How can i display a color and change it after a second to another random color till the right colour appears.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_farberkennung);
b_zurück = (Button) findViewById(R.id.b_zurück);
b_start3 = (Button) findViewById(R.id.b_start3);
b_randomColor = (Button) findViewById(R.id.b_randomcolor);
final int background;
final Random rand = new Random();
final List<Integer> list = new ArrayList<>();
list.add(getResources().getColor(R.color.red));
list.add(getResources().getColor(R.color.green));
list.add(getResources().getColor(R.color.yellow));
//list.add(getResources().getColor(R.color.blue));
//list.add(getResources().getColor(R.color.orange));
//list.add(getResources().getColor(R.color.olive));
//list.add(getResources().getColor(R.color.purple));
background = list.get(rand.nextInt(list.size()));
b_start3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b_start3.setEnabled(false);
b_randomColor.setBackgroundColor(background);
//loop random colours till red appears
while (background != getResources().getColor(R.color.red)) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
b_randomColor.setBackgroundColor(background);
}
}, 1000);
}
startTime = System.currentTimeMillis();
}
});
b_randomColor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (background == (getResources().getColor(R.color.red))) {
endTime = System.currentTimeMillis();
currentTime = endTime - startTime;
b_randomColor.setText(currentTime + " MS");
b_randomColor.setEnabled(false);
b_start3.setEnabled(true);
}
}
});```
you can do it by this way :
create a new java class file called Timer
public class Timer {
private thread t ;
public boolean runTimer ;
public long delay ;
public int msg = 0;
private Handler handler ;
public Timer(Handler handler , long delay , int msg){
this.handler = handler ;
runTimer = true ;
this.delay = delay ;
this.msg = msg ;
t = new thread();
t.start();
}
public class thread extends Thread {
#Override
public void run(){
while (runTimer){
try{
thread.sleep(delay);
}catch (Exception e){}
Message message = handler.obtainMessage(msg);
handler.sendMessage(message);
}
}
}
public void playTimer (boolean play){
runTimer = play ;
}
}
then add this message handler inside your activity and inside that handler you can test your background color if it is red
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
}
// here the code you add to your activity
private final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (background != getResources().getColor(R.color.red)){
b_randomColor.setBackgroundColor(background);
}else {
// you could stop your timer thread like that ...
// timer.playTimer(false);
}
}
};
and finally start your timer inside your like that :
final Timer timer;
timer = new Timer(handler , 1000 , "time");
Related
My problem is I cant get my button to consistently click 5 times through a string array which is displayed in a TextView ,
whenever the maxclicks(5) and currentnumber get to 5 it stops working , Ive been trying to create if conditions to work around it, well somehow I have to manipulate my currentnumber to NOT be 5 because IF maxclicks == currentnumber my button is enabled.
In the Code below it stops just afte the first time of clicking 5 times.
so here is the Code :
public class MainActivity extends AppCompatActivity {
int currentnumber = 0;
int mod = 5;
TextView display = findViewById(R.id.tx);
Handler handler = new Handler();
int delay = 5000;
int maxclicks = list.length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
final String[] list = res.getStringArray(R.array.xyz);
final Button next_button = findViewById(R.id.next_btn);
{
((TextView) findViewById(R.id.tx)).setText(list[currentnumber]);
next_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(currentnumber == maxclicks){
currentnumber = 0;
}
if (currentnumber % mod == 0) {
next_button.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
//the button will unlock after the delay specified
next_button.setEnabled(true);
currentnumber++;
}
}, delay);
}
else {
display.setText(list[currentnumber]);
currentnumber++;
}
}
});
}
}
}
welcome to SO :) I did my best to understand on your explanation,so this is my solution for your problem and don't forget you can make your vars global to avoid final and one element array thing:
public class MainActivity extends AppCompatActivity {
private int currentnumber,mod,delay,Curclicks;
private TextView display;
private Handler handler;
private Button next_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
//Binding
display = findViewById(R.id.tx);
next_button = findViewById(R.id.next_button);
//getResources
Resources res = getResources();
//getting the data ready
String[] list = {"1","2","3","4","5","6","7"};
//assign vars
handler = new Handler();
currentnumber = 0;
Curclicks=0;
mod = 5;
delay = 5000;
//initial view
next_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(currentnumber == list.length){
currentnumber = 0;
}
if (Curclicks == mod-1) {
next_button.setEnabled(false);
display.setText(list[currentnumber]);
currentnumber++;
handler.postDelayed(new Runnable() {
#Override
public void run() {
//the button will unlock after the delay specified
next_button.setEnabled(true);
Curclicks = 0;
}
}, delay);
}
else {
display.setText(list[currentnumber]);
currentnumber++;
}
Curclicks++;
}
});
}
}
I am creating an animation that randomly selects a string that's pulled from an XML string array. It does this in rapid succession to make it look kind of like a slot machine. I got the animation going, but I do not know how to stop it after x times. Here's what I have thus far:
public class MainActivity extends AppCompatActivity {
TextView locationsTextView;
Button generateBtn;
String[] mArray;
int counter; //<-- This is how I would like to keep track of when handler should stop
Handler h = new Handler();
int delay = 50;
Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationsTextView = findViewById(R.id.locationsTextView);
generateBtn = findViewById(R.id.generateBtn);
mArray = getResources().getStringArray(R.array.locations_array);
locationsTextView.setText("");
generateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
h.postDelayed(new Runnable() {
public void run() {
updateTextView();
runnable = this;
h.postDelayed(runnable, delay);
}
}, delay);
}
});
}
private void updateTextView() {
Random random = new Random();
int maxIndex = mArray.length;
int generatedIndex = random.nextInt(maxIndex);
locationsTextView.setText(mArray[generatedIndex]);
}
}
I am thinking that something like this would work:
//pseudo code
//Button is clicked
//initialize counter to 0
counter = 0;
//updateTextView method is called repeatedly
counter ++;
if(counter == 50) {
h.removeCallbacksandMessages(null);
}
I just don't know where I would place that in the code.
If I would've taken a few more minutes before posting, I think I figured it out. See code below:
generateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter = 0; //<-- every click, set back to 0
h.postDelayed(new Runnable() {
public void run() {
counter++; //<-- increment with every method call
updateTextView();
runnable = this;
h.postDelayed(runnable, delay);
if(counter == 50) { //<-- stop after 50
h.removeCallbacksAndMessages(null);
}
}
}, delay);
}
});
}
I want to change the text of a textview after n seconds using handler. The various strings are present in a array of strings. (Updating a textview on the UI)
The following code makes the app crash everytime. what to do?
public class Main2Activity extends AppCompatActivity {
TextView textView;
Handler handler = new Handler();
String[] arr = getResources().getStringArray(R.array.wat1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = (TextView)findViewById(R.id.textView);
Runnable r = new Runnable() {
int len = arr.length;
#Override
public void run() {
textView.setText(arr[len-1]);
len--;
if(len!=0){
handler.postDelayed(this,2000);
}
}
};
handler.post(r);
}
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
textView.setText(yourArray[0]); //set text here
}
}, 1000); //1000 = 1 second
Use a Handler
Handler handler = new Handler(Looper.getMainLooper());
handper.postDelayed(runnable, 3000);
Where runnable is your action to change the text, and 3000 is the time in millis, where 3000 is equal to 3 seconds.
Try this::
textView.postDelayed(new Runnable() {
#Override
public void run() {
// set text
textView.setText("Text");
}
},300) // Time in millisecond
onLongClick or onLongLouch a TextView , I need to increase its value by 1 every 60 seconds, like this:
<TextView
android:layout_width="match_parent"
android:text="00 min"
android:id="#+id/timer"
android:layout_height="120dp"
/>
Create a Timer that runs each time in miliseconds.
final Handler handlerIncrementar = new Handler();
Timer timerTexto = new Timer();
TimerTask tareaEjecucion = new TimerTask() {
#Override
public void run() {
handlerIncrementar.post(new Runnable() {
public void run() {
try {
yourtextview.setTextSize(yourtextview.getTextSize() + 1);
} catch (Exception e) {
}
}
});
}
};
timerTexto.schedule(tareaEjecucion, 0, 60000);
Below Code will increase the counter by one every one secound till you hold the touch on text view and stops when you relese the touch
private TimerTask timerTask;
private TextView textView;
private int counter = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
int code = event.getAction() & MotionEvent.ACTION_MASK;
if (code == MotionEvent.ACTION_DOWN) {
startTimer();
} else if ((code == MotionEvent.ACTION_POINTER_UP) || (code == MotionEvent.ACTION_UP) || (code == MotionEvent.ACTION_CANCEL)) {
stoptimertask();
}
return true;
}
public void stoptimertask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
Timer timer = new Timer();
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 0, 1000); //
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(counter++ + " Sec");
}
});
}
};
}
I hope this will help you.
How do you repeat a sound every 6-10 seconds, meaning a random interval between 6 and 10? So the sound plays 6, then 7, 6, 10, etc? So far I have this, which works how I want and plays the sound “once” and changes the button on the screen. However to play the sound again i must click the button to “stop” then click again to “go”. I want to Hit then button and have the sound repeat every 6-10 seconds until you hit the stop button...
public class MainActivity extends ActionBarActivity {
MediaPlayer myMediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer myMediaPlayer = MediaPlayer.create(this, R.raw.whistle);
Timer soundTimer = new Timer();
Random r = new Random();
//Button related to play btn
final Button startStopButton = (Button) findViewById(R.id.startstopbutton);
startStopButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (startStopButton.getText().equals("Start")) {
//start the sound
//change button color and text
startStopButton.setText("Stop");
startStopButton.setBackgroundColor(Color.RED);
myMediaPlayer.start();
} else {
//stop whistle
//change button color and text
startStopButton.setText("Start");
startStopButton.setBackgroundColor(Color.GREEN);
}
}
});
}
You can get a random number between 6 and 10 like this:
int max = 10;
int min = 6;
int randomNum = new Random().nextInt((max - min) + 1) + min;
int seconds = randomNum * 1000;
Here is an example to play a sound every 6 to 10 seconds:
private final Random mRandom = new Random();
private final Handler mHandler = new Handler();
private MediaPlayer mPlayer;
private boolean mKeepPlaying = true;
private void playMySound() {
if (mPlayer == null) {
mPlayer = MediaPlayer.create(this, R.raw.whistle);
}
int delayMillis = 1000 * mRandom.nextInt(5) + 6; // random number between 6 and 10
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (isFinishing()) {
// Check if the Activity is finishing.
return;
}
mPlayer.start();
if (mKeepPlaying) {
// play the sound again in 6 to 10 seconds
playMySound();
}
}
}, delayMillis);
}
Just call playMySound(); in onCreate(Bundle) and toggle mKeepPlaying when you want to stop.
You can use MediaPlayer.OnCompletionListener to accomplish this, whenever the playback completes get a random number between 6 and 10 and post a runnable to start playback again after the calculated random time.
Here is an example of doing this.
public class MainActivity extends Activity {
Button button1;
MediaPlayer mediaPlayer = null;
private final Random mRandom = new Random();
int delayMillis;
Handler handler;
Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
mediaPlayer.start();
}
};
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toggleMediaPlayer();
}
});
}
private void toggleMediaPlayer(){
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
handler.removeCallbacks(runnable);
}else{
mediaPlayer = MediaPlayer.create(this, R.raw.hangouts_incoming_call);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
delayMillis = 1000 * mRandom.nextInt(5) + 6;
handler.postDelayed(runnable,delayMillis);
}
});
}
}}
Hope This helps .