Setting button text with myButton.setText prevents animation from happening - android

I have the weirdest bug in my android code.
Basically, I'm streaming video with the Camera, and I have a button to start and stop the stream, call it myButton.
If I click on the button to START the stream, I have code that does myButton.setText("stop"), so that the button now says stop if the user wants to stop the stream.
In my onTouchEvent, I have code that detects a swipe up or a swipe down, and make a menu disappear/appear accordingly.
The swiping up and down animates my menu to slide out of the screen or slide back into the screen. It works perfectly fine before I try to start streaming, but if I ever click the start streaming button WHILE the menu is off the screen (translated -menuView.getHeight() distance away to make it off the screen), then if I try to swipe the menu back down again, it won't appear. However, it definitely receives the onTouchEvent because if I click the stop button (myButton.setText("start") will execute), then the menu immediately pops into the middle of the screen without any animation (it seems as if the animation must have happened invisibly).
Anyone have any insight into what could be causing this? Have been searching around everywhere and looking it up but haven't had any luck. Thanks
Updated with relevant code:
#Override
public void onClick(View v) {
ToggleButton clickedButton;
switch (v.getId()) {
case R.id.start_stop_stream:
if (!recording) {
Thread t = new Thread(){
#Override
public void run(){
startRecording();
}
};
t.start();
startStopButton.setText("Stop");
startStopButton.setTextColor(Color.RED);
} else {
Thread t = new Thread(){
#Override
public void run(){
stopRecording();
}
};
t.start();
startStopButton.setText("Start");
startStopButton.setTextColor(Color.BLACK);
}
break;
}
}
and
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN:
downTouch = touchevent.getY();
break;
case MotionEvent.ACTION_UP:
if(menuView != null){
releaseTouch = touchevent.getY();
SharedPreferences.Editor editor = sp.edit();
if (downTouch < releaseTouch && Math.abs(downTouch-releaseTouch) > 100) {
menuView.animate().translationY(0);
editor.putString("menuDisplayed", "True");
editor.apply();
return true;
}
if (downTouch > releaseTouch && downTouch-releaseTouch > 100) {
menuView.animate().translationY(-menuView.getHeight());
rtmpURL = rtmpURLText.getText().toString();
editor.putString("rtmpUrl", rtmpURL);
editor.putString("menuDisplayed", "False");
editor.apply();
Log.v("EditText", rtmpURLText.getText().toString());
return true;
}
}
break;
}
return false;
}

Related

How to check If a view has been touched?

I'm using a third party video player in my app. The library provides enough functionalities like scroll video, pause, play video etc. And all those features will auto-hide after a few seconds. I need a few more controls over the player. So I put a toolbar on it ( It consists of the back, favorite, share buttons ). I need to enable this toolbar only If someone clicks on the player or even taps on it. And it must hide after a few seconds like the video player's controller. I tried using this,
youtubePlayerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
LogUtils.LOGE("youtubePlayerView.setOnClick", "called");
return true;
}
return false;
}
});
But in vain. Nothing is happening.
I want to detect If the user has clicked or tapped on the player.
Try Like this
public void onClickHandler(View v)
{
switch (v.getId()) {
case R.id.youtubePlayerView:
//Player Clicked
break;
}
}
This should work:
youtubePlayerView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// enable toolbar here!
}
});

Play sound when press down and stop it when press up?

I have a sound with 7 seconds duration. I want to play it when pressing down and stop it after (for example 1 second) when pressing up. My code is like below, but I have a problem??? when pressing rapidly and several times, the sound play all 7 seconds and don't stop it. I want to create a simple piano.
what's the problem? Do you have any idea for this?
c.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // Button Pressed
SID1_c = soundPool.play(sound_c, 1, 1, 1, 0, 0);
c.setBackgroundResource(R.drawable.key4);
return true;
case MotionEvent.ACTION_UP:// Button released
handler =new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
soundPool.stop(SID1_c);
}
},90);
return false;
}
return false;
}
});
During pressing down the button first you need to stop the soundPool if any present.
Write soundPool.stop(SID1_c); in case MotionEvent.ACTION_DOWN: before starting the soundPool again.

How can I show the visual effect of a pressed button when I press another button?

I have two buttons. How to show the visual effect of clicking both buttons, actually pressing only one of them?
If you want to show effects on button press and button release, you can use this code.
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
button2.setPressed(true);
break;
case MotionEvent.ACTION_UP:
button2.setPressed(false);
break;
}
return false;
}
});
If you only want only the animation effect you can try this :
buttonLeft = findViewById(R.id.left);
buttonRight = findViewById(R.id.right);
buttonLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonRight.setPressed(true);
buttonRight.setPressed(false);
}
});
}
It's working, but maybe there's a better way to this,
Hope it will help

How to listen onTouch and onClick event simultaneously?

I have a button and a text label displaying a number. I want to click the button to add the number of the label to the button. I want to also enable holding on the button for a while and later start to add the number continuously. If the longclick event has added the number, the click event will do nothing. How can I implement this?
Use a custom TouchListener like this (this is very basic, written in the browser, not in an IDE):
boolean touching = false;
long startTime = 0;
#Override
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
if(touching){
onLongClick(System.currentTimeMillis() - startTime)
}else{
touching = true;
startTime = System.currentTimeMillis;
onClick();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
touching = false;
break;
}
return true;
}
onLongClick(long elapsedTime){
//Do stuff
}

Changing button text and ID by clicking on it

In part of my program, I change the id and text of a button when it is clicked. When the button is clicked again, the id and text are reverted to the previous values. However, I am getting error. (I have added a comment to the line where I am getting the error).
I just need to change text by clicking a button. Then change it back to the old values by clicking it again. Does anyone have a solution or some better idea on how to accomplish this?
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
mPlayer.stop();
start.setText("Pause");
start.setId(R.id.pause);
break;
case R.id.pause:
start.setText("Pause"); //here it is not accepting pause
start.setId(R.id.btn_start_again);
break;
}
}
I don't think you want to change the id of the Button. What it looks like you are doing is having a Button that starts play then turns to pause while playing. Simply have the two Buttons in the same space in your xml. Start with the visibility of the pause Button as gone then check the visibility in the function.
Something like
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
mPlayer.stop();
btnPause.setVisibility(View.VISIBLE);
btnPlay.setVisibility(View.GONE);
break;
case R.id.pause:
btnPause.setVisibility(View.GONE);
btnPlay.setVisibility(View.VISIBLE);
break;
}
Button Docs
You don't need to change the ID of the button, use something like a boolean flag to keep track of the state of the button:
boolean isPlaying = false;
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
if(isPlaying){
mPlayer.stop();
start.setText("Play");
isPlaying = !isPlaying;
}else{
//mPlayer.start() <--- you don't start it anywhere?
start.setText("Pause");
isPlaying = !isPlaying;
}
break;
}
}
There a better way you can do it.
Set different tag to the button according to the status.
changing ID programmatically is a bad idea, better solution is to define a global boolean value:
private boolean isSelected = false;
and then in Your onClick:
public void onClick(View v) {
if(isSelected==false){
mPlayer.stop();
start.setText("Pause");
isSelected=true;
}else{
start.setText("Pause");
isSelected=false;
}
you could just have 1 button which can handle different events and change its behaviour based upon the current button text.
public void onClick(View v) {
switch(v.getText()){
case "Pause":
mPlayer.stop();
start.setText("Play");
break;
case "Play":
start.setText("Pause");
mPlayer.play();
break;
}
}
or you could have the button initally setup to fire 1 click event which once fired then sets the button click event to the second click event and vice versa
public void onClickOne()
{
// do stuff
btn.OnClick = onClickTwo();
}
public void onClickTwo()
{
// do stuff
btn.OnClick = onClickOne
}
I ended up doing it in this way:
String value=start.getText().toString();
if(value.equals("Start")){
start.setText("Pause");
}
else if(value.equals("Pause")){
start.setText("Start");
}

Categories

Resources