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
Related
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.
I have a set of buttons and views displayed on the screen. At some point, I would like to be able to interact only with one of them and block all the others.
And I need to retain all the listeners to restore theirs behaviors later.
Any ideas?
Thanks!
For disabling buttons, when you clicking on a specific button/view, disable other buttons/views in the specific onClickListener like
//button1 onClickListener
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button2.setClickable(false); //disable button2
view1.setClickable(false); //disable view1
}
});
likewise for all other buttons and views, then you may enable those by calling
button2.setClickable(true);
view1.setClickable(true);
Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView
when I make a custom view class and add a clickListener it fires anywhere on the screen I click, even where the custom view is not. If I use the same code with a button from a layout it only fires when I click the button not anywhere on screen. Any ideas how to just only listen for when my custom class is directly clicked?
button only fire when pressed
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(DEBUG_TAG, "button clicked");
}
});
stroke object fires when you press anywhere on screen, even outside of stroke's bounding box
Stroke stroke = new Stroke(this);
mainLayout.addView(stroke);
stroke.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// fires on every screen click :>(
Log.d(Main.DEBUG_TAG, this.toString()+"shape clicked");
}
});
I think your custom view just fills the whole screen. That's why it reacts on every click on the screen. You need to make it smaller and everything will work fine.
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...