How can you keep a sliding drawer from closing when it is open and I touch the top? The sliding drawer is on same page as video player which likes to climb to the top. what steps do I need to take. In the sliding drawer handle I have a button which I want to click, but it closes the moment I touch the button (which is inside a linear layout within the the slider handle). Any suggestions would be welcome.
You can open and clos sliding drawer here post use
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
slideButton.setBackgroundResource(R.drawable.closearrow);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
slideButton.setBackgroundResource(R.drawable.openarrow);
}
});
Related
I am trying to implement an expandable fab, I implemented it and it works as expected i.e when the user taps on fab_main the submenu becomes visible and active, and when the user taps on the fab_main again the sub menu turn invisible and inactive.
What My problem is that I want the submenu to close (in case it is open) when the user taps on screen like for example the current activity
I have Tried using ontouch (which didn't work) and dispatchTouchEvent(which worked but I was not able to use the sub menu buttons
This is my code for how I implemented expandable FAB
fab_main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isOpen) {
textView_img_edit.setVisibility(View.INVISIBLE);
textview_bg_edit.setVisibility(View.INVISIBLE);
fab2_bg_edit.startAnimation(fab_close);
fab1_img_edit.startAnimation(fab_close);
fab_main.startAnimation(fab_anti_clock_wise);
fab2_bg_edit.setClickable(false);
fab1_img_edit.setClickable(false);
isOpen = false;
} else {
textView_img_edit.setVisibility(View.VISIBLE);
textview_bg_edit.setVisibility(View.VISIBLE);
fab2_bg_edit.startAnimation(fab_open);
fab1_img_edit.startAnimation(fab_open);
fab_main.startAnimation(fab_clock_wise);
fab2_bg_edit.setClickable(true);
fab1_img_edit.setClickable(true);
isOpen = true;
}
}
});
I used this code in both onTouch and dispatchTouchEvent
if (isOpen) {
textView_img_edit.setVisibility(View.INVISIBLE);
textview_bg_edit.setVisibility(View.INVISIBLE);
fab2_bg_edit.startAnimation(fab_close);
fab1_img_edit.startAnimation(fab_close);
fab_main.startAnimation(fab_anti_clock_wise);
fab2_bg_edit.setClickable(false);
fab1_img_edit.setClickable(false);
isOpen = false;
}
Try setting a onTouchListener on the base layout. You can check for MotionEvent.ACTION_UP and close your fab submenu (if it's open) there.
Starting with API 14 you can use this flag SYSTEM_UI_FLAG_HIDE_NAVIGATION on a View within your Activity to tell the system you want to hide the navigation bar until there is user interaction (screen touch). Once the user taps the screen the bar is shown.
The Activity that I am doing this in takes some action (starts a separate activity) when the user touches the screen, before adding the above flag to my view this worked perfectly.
After adding that flag the system intercepts the first screen touch and reacts to it by showing the nav bar. It's not until the second touch that any of my Views, or my Activity are receiving a TouchEvents.
Does anyone know of a way that I can set up a listener that will let me launch my second activity the first time the screen is touched instead of needing to double tap when using this hide nav flag?
I've tried all of the following and I'm not getting callbacks to any of them when the screen is touched for the first time to show the nav bar.
#Override
public void onUserInteraction(){
Log.i(myTag, "INTERACT");
}
#Override
public boolean onGenericMotionEvent(MotionEvent me){
Log.i(myTag, "GENERIC");
return true;
}
//I thought maybe the size change would lead to a callback here. No dice though.
#Override
public void onWindowAttributesChanged(WindowManager.LayoutParams params){
Log.i(myTag, "WINDOW CHANGE");
}
#Override
public boolean dispatchTouchEvent(MotionEvent me){
Log.i(myTag, "TOUCH");
return true;
}
Note: I am not trying to prevent the nav bar from being shown upon the first touch, I just want to also take some other action when that event occurs.
As Josh Lee suggested in his comment, View.OnSystemUiVisibilityChangeListener was the key.
Here is the code that I used:
mView.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int vis) {
Log.i(myTag, "System UI"+ vis);
if(vis == 0){
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(i);
finish();
}
}
});
I think that mView could be a reference to any view that is currently showing in your Activity. In my case it was a fullscreen VideoView, and was the only view in my layout.
I have a moderately complex layout with several clickable elements. When the user clicks on an empty space or on a passive element (for example TextView) I want to hide the ActionBar. Is there a way to do this?
view.getRootView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// hide action bar
}
});
(Please note that the behavior described in this question only appeared because of something else seemingly unrelated we were doing. See the accepted answer.)
We have an Android activity with a GridView and a SlidingDrawer inside of a RelativeLayout. The way this activity responds to the trackball (or cursor keys) is rather odd. The focus will move among the items in the GridView, but whenever the cursor moves in a direction "out" of the GridView. (e.g. up when at the top, left when already at the leftmost item) the sliding drawer opens or shut. Notably, the focus stays on the same item in the GridView---it does not move to the sliding drawer.
With a trackball this is particularly horrible, as spinning the trackball past your real destination will cause the sliding drawer to repeatedly open and close.
We've determined that we can turn off the trackball entirely by overriding onTrackballEvent(). We'd prefer to have the trackball and cursor work normally on the GridView but not cause the sliding drawer to open or close. In principle we'd also like the trackball to focus on the various contents of the sliding drawer when it is open.
How?
You may consider creating custom views extending GridView and SlidingDrawer and using custom implementations of onInterceptTouchEvent and onTouchEvent for the GridView and a custom implementation just for onInterceptTouchEvent for the SlidingDrawer. You may not need to implement a custom SlidingDrawer depending on what user interactions may be triggered on the handle
for your custom GridView, give it an interface maybe defined like this:
public interface MyGridViewListener {
public boolean shouldPreventScroll();
}
return if your custom SlidingDrawer is opened. this returned value will be used to determine if actions should be performed(for onInterceptTouchEvent and onTouchEvent methods) on the GridView. So when the SlidingDrawer is opened, actions performed on the GridView will not trigger anything on the SlidingDrawer.
Activity:
MyGridView gridView = (MyGridView) findViewById(R.id.gridView);
gridView.setMyGridViewListener(new MyGridViewListener() {
#Override
public boolean shouldPreventScroll() {
return slidingDrawer.isOpened();
}
});
MyCustomGridView:
shouldIntercept will be called whenever some touch/track event happens on the GridView.
private boolean shouldIntercept() {
boolean shouldIntercept = false;
if(myGridViewListener != null) {
shouldIntercept = myGridViewListener.shouldPreventScroll();
}
return shouldIntercept;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return shouldIntercept() ? true : super.onInterceptTouchEvent(ev);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return shouldIntercept() ? true : super.onTouchEvent(ev);
}
#Override
public boolean onTrackballEvent(MotionEvent event) {
return shouldIntercept() ? true : super.onTrackballEvent(event);
}
public MyGridViewListener getMyGridViewListener() {
return myGridViewListener;
}
public void setMyGridViewListener(
MyGridViewListener myGridViewListener) {
this.myGridViewListener = myGridViewListener;
}
I hope this points you in a right direction or at least helps
While playing around with a custom sliding drawer I set the layout of the handle to some odd value, something like
handle.layout(0, 0,0, 0);
to make the handle disappear but dragging a finger from the side of the screen would still open the sliding drawer, which is what I didn't want, so I set
handle.layout(10000, 10000, 10000, 10000);
which moved it way outside the viewable area and the drawer could no longer be pulled out manually by dragging from the side of the screen. After looking at the source code its teh position of the handle that determines the sliding of the drawer, get rid of the handle and it should solve your problem.
If you need to open/close the drawer call animateOpen()/animateClose()
As it turned out, we caused this problem by an unrelated bit of foolishness. We wanted the MENU key to open and close the SlidingDrawer. We did this by overriding onPrepareOptionsMenu():
public boolean onPrepareOptionsMenu (Menu menu) {
slidingDrawer.animateToggle();
return true;
}
This works fine; but it turns out it can be called when the menu is not about to be opened. In particular, if the Activity uses setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT), then an unhandled key event will end up accessing the menu. This includes trackball motion off the edge of the screen.
The less dumb way to get the desired behavior is
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_MENU) {
slidingDrawer.animateToggle();
}
return super.onKeyUp(keyCode,event);
}
Meanwhile, we can get the trackball to move within the SlidingDrawer when it is open by setting up a SlidingDrawer.OnDrawerOpenListener which calls
slidingDrawer.getContent().requestFocus();
Finally it seems like a good idea to call
slidingDrawer.getHandle().setFocusable(false);
I would like to know how to prevent the menu bar from closing.
#Override
public void onOptionsMenuClosed(Menu menu) {
}
When the activity starts, I open the menu and want it to stay open.
new Handler().postDelayed(new Runnable() {
public void run() {
openOptionsMenu();
}
}, 1000);
You cannot keep the Options Menu open and it will always act that way. But what you can do is create your own custom menu in a layout.xml and set the visibility to GONE. Then, override the onKeyDown() method and listen for presses of the menu key. If it is pressed, the options menu will be set to open/close (VISIBLE/INVISIBLE) depending on its current state. That way, you can control if the options menu will remain open or not even after touch.