I was wandering if there was any way to track the amount of clicks a user clicks a button.
I am creating a game and the user will only be allowed to take 5 turns. After these turns the user has lost the game.
I need to create maybe an if statement where the amount of clicks the user takes reaches > 5 then the user has lost. Is this possible.
I appreciate any help on this. Thanks
Edit:
Button link2Btn = (Button)findViewById( R.id.answerSelected );
link2Btn.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
i++;
getAnswer();
}
The get Answer method works fine except the if i > 5 statement within get Answer which is:
else if(i>5){
correctAnswer.setText("You have lost");
Use a flag variable and make an increment over a button press.
like
int i=0;
when button pressed
i++;
Now your condition
if(i>5){}
Related
I need some help,
I have one ImageButton that plays and stops a tune, I want the button to change to a stop symbol when playing and then back to a play symbol when stopped. So far I have the symbol and tune playing when the ImageButton is clicked the first time, but when it is clicked the second time, the tune stops but the image does not change, any advice?
mPlayTune.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tuneMp.isPlaying()) {
tuneMp.stop();
tuneMp.prepareAsync();
mPlayTune.setImageResource(R.drawable.ic_av_play_arrow);
}else
tuneMp.start();
mPlayTune.setImageResource(R.drawable.ic_av_stop);
}
});
Couldn't you just make 2 objects and hide or show them by clicking them? It's not a clean implementation but for sure a workaround.
I solved it, it was simply changing
tuneMp.start();
mPlayTune.setImageResource(R.drawable.ic_av_stop);
to
mPlayTune.setImageResource(R.drawable.ic_av_stop);
tuneMp.start();
this way it sets the icon before hitting tuneMp.start();
I'm about to finish my Simon Says game, but I still have one crucial problem.
After my buttons are shown to the player, I have to wait (according to the level, e.g, if 5 buttons were showed to the user, I have to wait the user to click 5 buttons). I have searched around the internet, but the only answer was "You cannot freeze the android Application... blah blah blah".
Here it is my code:
public boolean playerTurn(){
//Enable buttons
buttonUp.setClickable(true);
buttonDown.setClickable(true);
buttonRight.setClickable(true);
buttonLeft.setClickable(true);
/*
Wait for a button clicks depends on the level, but HOW?
e.g, if the level is 5, I have to wait for 5 button clicks
and after it, I can continue to run the code
*/
/*Check if the user typed the correct order
*If pressed the correct order
*return true;
*else
*return false
*/
}
For each button do something like this:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
buttonsPressedCount++;
if (buttonsPressedCount >= 5) {
// Do whatever you wanted to do
}
}
});
In this way, each button will listen for clicks and update a global counter. Finally when your global counter is greater than 5, you can proceed. The application is never "paused", it just only moves on the next section when five buttons have been clicked.
I'm not near a computer so no code example (will post later). Basically, you can set a class level counter. Provide an OnClickListener for each button - can be same or different. In the listener increment the counter and check if it reached 5 or not. If it did, check the pattern and clear counter.
You can use RxBindings for this purpose. Convert the clicks into a stream of events, and add a subscriber to it, which will react to the event of a click.
Here's how you can do it,
RxView.clicks(button)
.take(5)
.subscribe(aVoid ->
{
// Do your stuff
});
This will add a subscriber to the click event, and will only take the first five events.
Hope it helps.
I want to create a Button that behaves like a switch.
It should change its color when the user clicks it, and keep the color.
So the button is white at first and when the user clicks it, the color changes to black. When the user clicks it again it switches back to white and so on.
I tried it with a simple if else construct but only managed to get the button to be white at first and when being clicked change to black, but it won´t change back to white when clicked again.
Here´s the code so far. I guess it´s a dumb simply mistake but can´t seem to get through with it. "changecolor" is a variable I declared myself.
// Select Button Safe or At-Risk
final Button button7 = (Button) findViewById(R.id.SafeBT);
button7.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks, change color
if (changecolor == 0) {
button7.setBackgroundColor(color.black);
changecolor = 1;
} else {
button7.setBackgroundColor(color.white);
changecolor = 0;
}
}
});
Tanks for advice and help in advance.
where you have declared your variable changecolor?? .
Second thingis that you can simply an UI Element which call it : ToggleButton , it is like a Switch Button ON/OFF . is that what you want ? see this link : http://developer.android.com/reference/android/widget/ToggleButton.html
Is there some way to set an onClickListener for a webview? I've seen code here to intercept url clicks but I would just like to intercept anytime a user clicks the area the webview's on or swipes across it with their finger. I load html directly into the webview (as opposed to giving it a url) for formatting reasons and I'd really like to be able to tell any time the user clicks anywhere on the screen. I had been using a textview and that works fine, but is it doable with a webview?
Have you tried instead of an onClick, an onLongClick listener?
Taken from my recent project:
printMessageTextView.setOnLongClickListener(new OnLongClickListener() {
int active = 0;
#Override
public boolean onLongClick(View v) {
In this case, the int active = 0; is probably not needed. But should be able to listen for you.
For example
mainEdit.setOnClickListener(new OnClickListener() {
int i = 0;
public void onClick(View v) {
//here something that causing double tap event
}
});
How can i archive that?
Android doesn't support double tap by default. You can kind of get the same effect by using a normal OnClickListener that stores a timestamp and checks to see if the stored value is close enough to the current time to count as a double click. You could even make your own Listener class that behaves this way. But I am unsure what you mean when you say: "here something that causing double tap event" Since nothing in android responds to double taps by default.