Pressing 2 buttons simultaneously in android app, no simple solution? - android

public class TwoPlayers extends AppCompatActivity implements
View.OnClickListener {
private Button start, start2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_players);
start = (Button) findViewById(R.id.buttonStart);
start2 = (Button) findViewById(R.id.buttonStrat2);
start.setOnClickListener(this);
start2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if((v == start)&&(v == start2)){
start.setVisibility(View.GONE);
start2.setVisibility(View.GONE);
...
Initially I thought this will be very easy to do since all phones support multi-touch for years now, but now that I red around it seems it's harder to do than I tought, I need to press 2 buttons simultaneously to start a game. My approach with onClick listener above doesn't work. What will be the easiest way to do this ? Because the approach I found so far involves using OnTouchListener and ACTION_DOWN, and then record some coordinates, and check if the coordinates are within button area, which is kind of complex. Not only that but all my other buttons are using onClick and if I use just for starting the game onTouch will I have to use it for all the other buttons as well or I can leave them using onClick ?

The condition if((v == start)&&(v == start2)) can never be true. v cannot have two different values at the same time.
The onClick method will be called twice, one time when you press the start button and another time when you press the start2 button.
You can use the isPressed method to check when the views are pressed at the same time (Button is a View, so it inherits all its methods).
In other words:
#Override
public void onClick(View v) {
if(start.isPressed() && start2.isPressed()) {
start.setVisibility(View.GONE);
start2.setVisibility(View.GONE);
...
When the first button is pressed, onClick is called but the condition is false. When the second button is pressed (and the first one is still pressed) the condition will be true.

Related

How to check if button has been clicked

I have a bunch of dynamic buttons which I am setting an onClickListeners as they are produced, as well as tagging them with IDs.
Not sure if this a simple one which I have just spent too much time staring at but this is the problem.
If a user clicks a button, it changes colour this is simple and has been achieved by:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter == 0) {
button.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
Toast.makeText(getActivity(), "User Has Been Marked As Present",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = true;
counter++;
} else {
button.setBackgroundColor(Color.parseColor("#FFFFFF"));
Toast.makeText(getActivity(), "User Has Been Marked As Absent",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = false;
counter = 0;
}
}
});
If the user clicks it again, it will change back to the previous colour - but...
If the user clicks one of the other dynamic buttons that hasn't been previously clicked, the counter is thrown out.
I need to know if the button has been clicked and if not, should mark the user as present.
Currently, If on one button I click it and mark the user as present, and then move onto the next button, I will have to click it once (which marks the user as absent due to the counter) then press it again to mark the user as present.
I need the counter to treat each button individually, any ideas how this could be achieved?
Once the user has been marked present,maybe disable the onClick listener for that button since you wouldn't need it anymore?
I don't mean to sound condescending but I'm having trouble understanding what you're trying to achieve, but if each button is supposed to hold different information about a user, why not make a custom button that does just that? Make a class called customButton in your package and paste the following code there:
import android.content.Context;
import android.widget.Button;
public class customButton extends Button {
boolean haveIBeenClicked; //false by default
public customButton(Context context) {
super(context);
}
public void toggleHaveIBeenClicked(){
haveIBeenClicked=!haveIBeenClicked;
updateBackgroundColor();
}
void updateBackgroundColor(){
if (haveIBeenClicked){
this.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
}
else{
this.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
}
then, inside the onClick method (in the activity whose snippet you've shown earlier) you can just call
((customButton)v).toggleHaveIBeenClicked();
...after having created a customButton object and setting an on click listener on it.
Please let me know if this achieves what you desired. If you have trouble running this code, make sure to let me know if the comments and we'll work it out

When button gets clicked, make it dissapear, randomely change bg color and load text from database?

I am busy creating an app. I succeeded in creating the button with a custom font and all. Now what I'd want is that when I click the button, it must disapear, the background color of the view must randomely change and text must be loaded from an database.
How does one go about this?
Matthew
Well, the disappearing can be make like this:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click - Disappear in your case...
v.setVisibility(View.INVISIBLE); //can be View.GONE as well...
}
});
}
}
Then for the background I guess you can create an array with all the colors you want (or something that generates a random HEX code) and then do setBackground(X) where X is the HEX code that you just generated... You need to specify more about the database part though.

How to check if a button is clicked or not?

I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});

how to implement gesture to a button on android?

I have a button (button1) and two sound (sound1, sound2), I want to implement a gesture to this button. if Button1 is clicked, then the sound is sound1 but if Button1 is touched by the gesture, the sound is sound2. to handle the sound, I use a SoundManager class. to handle touch I use multitouch class.
public class MyActivity extends MultiTouch {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
SoundManager.getInstance();
SoundManager.initSounds(this);
SoundManager.loadSounds();
Button Button1 = (Button)findViewById(R.id.button1);
Button1.setOnTouchListener(this);
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
SoundManager.playSound(1, 1);
}
});
}
public void onDestroy()
{
super.onDestroy();
SoundManager.cleanup();
}
}
Thank you so much
There is no big difference between click and touch.
But if you want to implement both, you have to make a trick to make it work.
For the System it is hard to decide laying the finger on a button, if you want to "click" or "touch" it. To controll this, you can get the time, the button is clicked/touched/whatever...
for example:
If you click/touch/whatever a Button you start counting, if you stopped clicking/touching/whateevring you stopped the countig. After that its easy: if(time < 1s) else
Beware, that it would be a good idea to have a Click-Listener and TouchListener with the same code in it and just decide, using the counting stuff.
I've implemeted it already at it works perfectly!
You can for exemple use a GestureListener (https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#5307216038506006379) and handle the differents events or type of gesture for your button (touch, tap, swipe...)
dynamically creating button would cause problems.but if make the buttons in you xml file and then set on

Issue with ImageButton.setVisibility()

I'm having a problem when setting the visibility of two image buttons one on top of the other. The idea is to implement a play/pause control. The problem is that the only part where setting the visibility actually works is in the click listeners of the buttons. If I try to change it somewhere else nothing happens. Any idea why is this happening?
playBtn.setOnClickListener(new OnClickListener() {//PLAY BUTTON LISTENER
public void onClick(View v) {
playBtn.setVisibility(ImageButton.GONE);
pauseBtn.setVisibility(ImageButton.VISIBLE);
mp.start();
}});
pauseBtn.setOnClickListener(new OnClickListener() {//PAUSE BUTTON LISTENER
public void onClick(View v) {
pauseBtn.setVisibility(ImageButton.GONE);
playBtn.setVisibility(ImageButton.VISIBLE);
mp.pause();
}});
final class SeekBarTask extends TimerTask {
public SeekBarTask(int duration) {
}
#Override
public void run() {
if(seekBar.getProgress() >= mp.getDuration()) {//IF SONG HAS FINISHED...
pauseBtn.setVisibility(ImageButton.GONE);//THESE ONES
playBtn.setVisibility(ImageButton.VISIBLE);//DOESN'T WORK
mp.stop();
}
else {
seekBar.incrementProgressBy(100);
}
}
}
I would recommend just changing the icon of one ImageButton.
I would think only one of two things could be happening. Either this code never gets hit, or the variables are not referring to the same object instances you're expecting them to. Have you put a breakpoint inside that condition? I would check that a break point even gets hit in there, and then check that the variables are pointing at the correct button instances.
Without seeing the rest of the code I have to ask...why are you checking on a progress bar for a "finished playing" condition versus using the media players on completion callback?
I'm doing something very similar, and I use the MediaPlayer's OnCompletionListener to flip the visibility of my buttons.
I don't remember the details of Android GUI manipulation but could it have to do that you're doing it from another thread and you're not supposed to?
i noticed that setting an ImageButton to View.INVISIBLE is not working when you have set an Animation to it. you have to remove the Animation then make it Invisible. bad pitfall i think...

Categories

Resources