How to play/pause video in android? - java

I am trying to Pause video on touch and play on release touch like as instagram reels, I had searched in google but I didn't get the solution, can anyone help me to find out the solution
binding.Video.setOnTouchListener(new View.OnTouchListener() {
boolean show = true;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (show) {
binding.fullVideo.pause();
show = false;
} else {
binding.fullVideo.start();
show = true;
}
return false;
}
});

Related

How to call dispatchTouchEvent with a 'delay' in onTouch?

I have two views which are siblings of each other. They both cover the full screen. So one is behind the other. If the upper one gets touched (onTouch), I delegate the touch events to the one underneath it (with dispatchTouchEvent).
But sometimes I want to delay that delegation, till the next time onTouch gets called. But somehow that does not work.
An example to clarify:
To viewA - which is in front of viewB - I have applied the following (simplified) code:
viewA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
int touchType = event.getActionMasked();
if (touchType == MotionEvent.ACTION_DOWN) {
savedEvent = event;
return true; // I also tried returning false here
} else {
if (savedEvent != null) {
viewB.dispatchTouchEvent(savedEvent);
savedEvent = null;
}
viewB.dispatchTouchEvent(event);
return true; // I also tried returning false here
}
}
});
To test the dispatchTouchEvent call, I have the following code for viewB
viewB.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
Log.d("test", "test"); // this code gets logged, so it is being called, but the view seems to not execute any of the touch events
return true;
}
});
When I change to code for viewA to this:
viewA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
viewB.dispatchTouchEvent(event);
return true;
}
});
everything works just fine.
But the thing is that for my use case, I sometimes have to call the dispatchTouchEvent method with the event parameter outside its originating onTouch method, so to speak.
Is this even possible? And if yes, how?
So I found out what prevents it from working. If you manually call dispatchTouchEvent you should pass through an event that you created yourself with MotionEvent.obtain().
Working example:
viewA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
int touchType = event.getActionMasked();
if (touchType == MotionEvent.ACTION_DOWN) {
// do not do this, somehow passing on a reference of the event directly does not work:
// savedEvent = event;
// this works though:
savedEvent = MotionEvent.obtain(event);
return true;
} else {
if (savedEvent != null) {
viewB.dispatchTouchEvent(savedEvent);
savedEvent = null;
}
viewB.dispatchTouchEvent(event);
return true;
}
}
});
Although I don't understand why that does work and just passing the event reference directly does not, I tested this and it does work perfectly.

background button press detection

I am trying to build an android app which detects button pressed on a background and plays a sound or something. For demo, I am trying to add this method
public boolean onKeyDown(int keyCode, KeyEvent event) {
final MediaPlayer abc = MediaPlayer.create(this, R.raw.abc);
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
abc.start();
}
return super.onKeyDown(keyCode, event);
}
to this class
public class RSSPullService extends IntentService{
public RSSPullService(){
super("Nevermind me");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
}
}
Is there something that can be helpful? I am new to android development and Java.
In touch android mobile phones there is not any keyboard hardware. So please specify which button you want to get pressed?
Edited:
Add the following permission in Manifest fie:
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
and override following methods:
//To capture any short click of button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// this is method which detect press even of button
event.startTracking(); // Needed to track long presses
return true;
}
return super.onKeyDown(keyCode, event);
}
//To capture long press of power button
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Here we can detect long press of power button
return true;
}
return super.onKeyLongPress(keyCode, event);
}

Hide and Show Toolbar on touching anywhere on screen

I have successfully implemented the on scroll toolbar Hide/Show but i am stuck at onTouch Hide/Show toolbar. i have researched many related questions but nothing works for me. I want my toolbar to hide when user touches on screen and again shows when the screen is touched again, please help
I am using Android Studio
below is my OnScrollListener java class
this.mrecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
int mLastFirstVisibleItem = 0;
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final int currentFirstVisibleItem = layout.findFirstVisibleItemPosition();
if (currentFirstVisibleItem > this.mLastFirstVisibleItem) {
HomePage.this.getSupportActionBar().hide();
} else if (currentFirstVisibleItem < this.mLastFirstVisibleItem) {
HomePage.this.getSupportActionBar().show();
}
this.mLastFirstVisibleItem = currentFirstVisibleItem;
}
});
Updated
mrecyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ActionBar actionBar = getSupportActionBar();
if(actionBar.isShowing()) {
actionBar.hide();
} else
actionBar.show();
return false;
}
});
this code works, but the problem now is that whenever i touch the screen it shows the toolbar and when i pick up my finger it hide itself, and as i am using RecyclerView it is getting difficult to scroll with all that showing and hiding. please help to make it stable so that if i touch once it stays shown and on another touch it hides itself.
here you go. This should work :)
layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (actionBar.isShowing()) {
actionBar.hide();
} else {
actionBar.show();
}
return true;
} else return false;
}
});
Use onTouchListener to your root layout. And write your code inside callback.

have onTouchEvent only work on bottom half go the screen

I'd like to have two onTouchEvents on my app. One event would only work on the top half of the screen and the other event would only work on the bottom half of the screen. Is this possible?
//top
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// my logic
return super.onTouchEvent(event);
}
//bottom
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// my logic
return super.onTouchEvent(event);
}
Since the only provided solutions require you to use multiple Views, I figured I would provide one for a single View that encompasses the entire screen:
public boolean onTouch(View view, MotionEvent event) {
if(event.getY() < activity.getResources().getDisplayMetrics().heightPixels / 2){
//top
}
else{
//bottom
}
return true; //handle the touch
}
This was me just assuming you were only using a single View...
Something like this:
topLayout = (LinearLayout) findViewById(R.id.topLayout);
bottomLayout = (LinearLayout) findViewById(R.id.bottomLayout);
topLayout.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Log.d(TAG, "top was touched");
return false;
}
});
bottomLayout.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Log.d(TAG, "bottom was touched");
return false;
}
});
Or you could move those anonymous classes to their own respective classes and use them:
topLayout = (LinearLayout) findViewById(R.id.topLayout);
bottomLayout = (LinearLayout) findViewById(R.id.bottomLayout);
topLayout.setOnTouchListener(new TopOnTouchListener());
bottomLayout.setOnTouchListener(new BottomOnTouchListener());
This is assuming you have some view that spans the top and bottom of your layout. In the above example I'm using two LinearLayouts to do so, but feel free to choose your own.

How to exit an infinite while loop when user taps screen?

I currently have a while loop that starts when a user taps the screen. The loop is meant to be infinite, and should stop when it senses another screen tap from the user. (And, if tapped again, it'll start looping again, etc.) I’ve tried to break the loop in two different ways, with both yielding non-satisfactory results:
METHOD 1: When I try this method, what happens is that the user taps, the loop starts. The user taps again, and that tap doesn't get registered and the loop just keeps on going.
boolean playing = false;
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(!playing)
{
playing = true;
while(playing)
{
//do something here
}
}
else if(playing)
{
playing = false;
}
}
return false;
}
METHOD 2: When I try this method, what happens is that the user taps, the loop starts, goes through one iteration, then stops.
boolean playing = false;
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(!playing)
{
playing = true;
whileLoop:
while(playing)
{
//do something here
}
}
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
playing = false;
break whileLoop;
}
}
return false;
}
I've looked at these questions, but neither helped me with my specific situation:
How do I exit a continuous while loop by some user input?
How to exit an infinite while loop using user input?
So how do I implement this while loop? I just want a simple interface where a user's tap would toggle the while loop on/off.
Put the infinite loop within a Thread. Having it there, you can just have a control variable inside (like playing), and once tap again and you want to stop, simply set it to false. The Thread, running paralelly, will be able to handle both events (the tap and the Thread stop).
---- EDIT ----
Example added:
// You'd probably need to store this variable class-wide,
// so you can control when the user has tapped for the first or second time
boolean playing = false;
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(!playing)
{
playing = true;
new Thread(
new Runnable() {
public void run() {
while(playing)
{
//do something here
}
}
}
).start();
}
else if(playing)
{
playing = false;
// As you're controlling the playing value in the Thread,
// setting it here to false would mean that your thread stops too.
}
}
return false;
}
try this:
boolean playing = false;
Handler handler = new Handler();
Runnable runner = new Runnable() {
#Override
public void run() {
// do something here
handler.post(this);
}
};
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(!playing) {
playing = true;
runner.run();
return true;
}
if(playing) {
playing = false;
handler.removeCallbacks(runner);
handler = null;
runner = null;
Toast.makeText(context, "NO", Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
});

Categories