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
}
});
Related
I have an EditText that is focused and a Button that is not focused.
When I click the Button, the EditText loses focus.
When I long-click the Button, the EditText does not lose focus.
Whats the source of this behaviour? I want to achieve the long-click behaviour within a default click, is this possible?
Long click behavior is for by default for ClipBoard actions . If you want to override it with Single click . You can do this as follows.
editText=(EditText)findViewById(R.id.txt);
editText.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Do your stuff
return true;
}
});
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.performLongClick();
}
});
If you are doing it to just get rid of Focusing problem then its not the way.
I have a view animator that has a lot Button on it. I've a OnTouchEvent for the view . codes works fine if I touch on space, but if I touch the button the code doesn't work. how can i fix it?
final View frame = (View)findViewById(R.id.viewAnimator1);
frame.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
viewAnimator.showPrevious();
}
public void onSwipeLeft() {
viewAnimator.showNext();
}}
You need to set on click listener for buttons, if all of them makes the same work, you can extend button class and implement on click listener and use it instead of normal button
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);
}
});
I have a view that can be swiped to another page. It has an onClick listener, which displays a dialog box.
The problem is, swiping triggers both actions (i.e. It shows the next page and the dialog box).
How can I disable the onClick listener when swiping.
It sounds like you want to use a GestureDetector (as well), the SimpleOnGestureListener has onSingleTapConfirmed() for click events and onFling() for swipe events.
While onClick of swipelayout put swipelayout.getSurfaceView() and while swiping it triggers only swipe action. See the code below:
holder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your onClick stuff here..
}
}
I want to write a program that handles touch events in a specific order within a single activity.
For example:
A few views are shown.
If the user doesn't tap on the first view, I will show another activity.
If the user taps on it however, I want to detect a tap on the second, then the third, etc.
How can I handle multiple touch events?
I think I need an onTouchEvent method and in it I need an if-else statement for the first click but I don't know how I can monitor for the subsequent touch events.
It may help you.I always do like this
public void onClick(View v){
if(v==imageView1){
//do ssomething
}
if(v== imageView2){
//do something
}
if(v==imageView3){
//do something
}
like this u can do according to different button or imageview
I'm assuming the picture is an ImageView inside the main view. Why not just append touchlisteners to each view?
Set the onClickListener for the n+1 view only when nth view is clicked.
Like this
view1.setOnClickListener(new OnClickListener() {
void onClick(View v) {
view2.setOnclickListner(new Onclicklistener() {
void onClick(View v) {
// add further view's click listeners else do what ever if this
// is the last view.
}
});
}
});
Not an elegant solution but should work IMHO.
From your problem statement it seems you have an ordered list of views, each should have a touch listener, but the listener for the second view should not fire unless the listener for the first view has fired first.
This can easily be done by keeping a counter in your activity:
private int highestIndexTapped. When a view is tapped, check whether its index is such that index == highestIndexTapped + 1.
If it is, increase highestIndexTapped by 1 and fire the listener. Otherwise either eat the touch event or pass it on to the next part of your pipeline.