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.
Related
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;
}
});
I have a weird problem.
Achieved
I have 4 Fragments in a ViewPager attached with TabLayout. The first one is QR-code Scanner, which shows a camera. I wanted to start camera only when the Fragment is visible. For this, I override the Fragment's method setUserVisibleHint.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
checkPermissionForCamera(); //it checks permission and start camera
} else {
stopCamera();
}
}
And it's working absolutely fine.
Desired
Now, what I want to achieve is that when Fragment is not visible (or is being visible by scrolling), it shows a view with background over camera, so that instead of camera, that cover is visible. Like image below.
For it, I edited startCamera and stopCamera, now it looks like below,
public void startCamera() {
if(cameraCover != null)
cameraCover.setVisibility(View.GONE);
isCameraStarted = true;
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera();
}
public void stopCamera() {
if(cameraCover != null)
cameraCover.setVisibility(View.VISIBLE);
if(mScannerView != null) {
isCameraStarted = false;
mScannerView.stopCamera();// Stop camera on pause
mScannerView.stopCameraPreview();// Stop camera preview
}
}
But the gray cover is only visible for the first time, rest of the time I get the camera opened with view on which it's scrolled, like below.
I've also tried to override onPageScrolled of OnPageChangeListener, but with no luck. Here's what I've done.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 0) {
if(positionOffset > 0.7) {
fragment = adapter.getItemAtPosition(0); //fragment is at zero
if(fragment != null) {
if(fragment instanceof ScanQRFragment) {
((ScanQRFragment) fragment).stopCamera();
}
}
}
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Try either of the following ways :
1- override setMenuVisibility :
#Override
public void setMenuVisibility(final boolean visible) {
if (visible) {
//start camera preview
}
super.setMenuVisibility(visible);
}
2- Check yourFragment.isResumed() instead of isVisible()
I have two questions about ViewPager.
I have in my activity 2 ViewPagers, one for each week (of date). For example, one for 14/9/14 - 20/9/14, and the the other one for the next week 21/9/14 - 27/9/14. When I slide the ViewPager to it's next one, it will change each on of the ViewPagers to the next week of itself (21/9/14 - 27/9/14, 28/9/14 - 4/10/14).
When I change the data f the second ViewPager and then slide the first ViewPager to it's next one (and this is the same week like the second view pager data that I changed), it won't show the changes that I made, unless I slide two more times and then return to the changed week (because of the memory of the ViewPager). I tried mPager1.setOffscreenPageLimit(0); , but it didn't helped. What can I do in order to solve that?
I want that when I slide one of my ViewPagers, so the other one will animate itself exactly like my finger is sliding the other ViewPager. Any ideas how to do that?
I hope it helps:
it won't show the changes that I made
use adapter.notifyDataSetChanged(); after you have made changes.
I want that when I slide one of my ViewPagers, so the other one will
animate itself exactly like my finger is sliding the other ViewPager.
Any ideas how to do that?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager1= (ViewPager) findViewById(R.id.pager1);
viewPager2= (ViewPager) findViewById(R.id.pager2);
viewPager1.setAdapter(new MyAdapter1(getSupportFragmentManager()));
viewPager2.setAdapter(new MyAdapter2(getSupportFragmentManager()));
viewPager1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
viewPager2.onTouchEvent(event);
return false;
}
});
viewPager2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
viewPager1.onTouchEvent(event);
return false;
}
});
}
if you got
09-19 05:49:29.313: E/AndroidRuntime(1272): java.lang.IllegalArgumentException: pointerIndex out of range
try:
in your layout file you must use <package name of HackyViewPager.HackyViewPager instead of <android.support.v4.view.ViewPager
public class HackyViewPager extends ViewPager {
public HackyViewPager(Context context) {
super(context);
}
public HackyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return false;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
try {
return super.onTouchEvent(event);
}catch (IllegalArgumentException e) {
e.printStackTrace();
return false;
}
}
}
Reference:
Controlling two ViewPager Together
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.
I wanna use a button (not a key) just like backspace so when it is down do something repeatedly.
I've found proper code for hardware keys but as I mentioned I want a BUTTON do such things.
Thanks
You can set an OnTouchListener on a Button instance. You can then override the onTouch method of the of the listener to do what it is that you want until MotionEvent passed to the onTouch method has MotionEvent.getAction == MotionEvent.ACTION_UP. See this link for an example:
Android onTouch Listener event
A switch statement is sufficient, just customize it to fit your needs using what I said above. --hope this helps, Scott
Thanks Scott. Finally I found the answer and did the job.
public MyActivity extends Activity
{
private Handler mHandler = new Handler();
private Runnable mUpdateTask = new Runnable()
{
public void run()
{
Log.i("repeatBtn", "repeat click");
mHandler.postAtTime(this, SystemClock.uptimeMillis() + 100);
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button repeatButton = (Button) findViewById(R.id.repeatButton);
repeatButton.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent motionevent)
{
int action = motionevent.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
Log.i("repeatBtn", "MotionEvent.ACTION_DOWN");
mHandler.removeCallbacks(mUpdateTask);
mHandler.postAtTime(mUpdateTask, SystemClock.uptimeMillis() + 100);
}
else if (action == MotionEvent.ACTION_UP)
{
Log.i("repeatBtn", "MotionEvent.ACTION_UP");
mHandler.removeCallbacks(mUpdateTask);
}
return false;
}
});
}
}