I have a view on top of another view. The bottom view is supposed to fire a callback when clicked. The top view is not supposed to fire any callback when clicked.
The problem I am facing is that when there is a click on the top view, the bottom view fires its callback. I want to prevent this.
I've read a number of posts on disabling clicks and they all suggest the same thing:
view.setEnabled(false);
and
view.setClickable(false);
Neither of these prevent the top view from swallowing/blocking the click event.
Do you know how I can have a view prevent passing clicks through?
Its a little hacky but the best solution I came up with was adding an empty onClickListener to swallow the event.
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// HACK disables click through events by swallowing click
}
});
I couldn't understand what you are trying to do without an example.
But if you want to disable click for a particular view & you don't have any click functionality for this view, then try
view.setOnClickListener(null);
Also, did you try view.setFocusable(false); ??
Related
I'm using a ListView to show some images and names in my application. I can make the ListView tiles clickable and start new activities as well . But now i want to do different actions for the different items that are on one listview tile. If user clicks on image, image opens and the textView will show some toast or something. The ImageView and TextView are on the same ListView Tile. How can i achieve this? Any Help?
Define OnClickListener events in your adapter for the button and textview as required and you are done. They will precede the click event for the list item by default.
if the contained views in your custom row layout does not have a click event it will fall back to the click event of the list view item.
You can implement click event in adapter, but you also implement it in activity.
You can find solution in :http://www.migapro.com/click-events-listview-gridview/
hope it can help you.
Some note: should not use view holder in gridview like above tuturial, it can make some bug, can implement direct like this:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
Another way to implement this elegantly is to add secondary views called 'accessories' to the item views associated with your ListView. This is explaned quite well in the link below by Cyril Mottier: A Google Developer Expert on the Android platform:
http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
I'm trying to disable the "touch highlight animation" when touching the header. onNavigationItemSelected() is correctly only called when a menu item is touched. But when I touch the header, there is an animation.
I've tried setEnabled(false), setClickable(false) setLongClickable(false), setAnimation(null), setFocusable(false), but it still showing. I cannot find a methods for manipulating the header. (other than addHeader)
Any ideas? Thanks
It seems that updating to the latest version of the design library allowed for the following:
View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_view, mNavigationView, false);
headerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Consume input from header view. This disables the unwanted ripple effect.
}
});
I am quite confident that I tried this without luck before updating, though.
I have a large canvas where I placed multiple buttons. Each button has an image which is opened based on the button click event. I want to change it such a way so that when a button enters to the screen area, it will automatically open the button image.
I guess I need to find the current button view (that is visible on the screen) and then use functions to simulate the button click event (View.performClick();). As I am not entirely sure, any suggestion would be highly appreciated.
Can you try to take both view button view and image at the same position, when click on button view then this view hide and show image.
I think the simplest way is to add ImageButton and change the image/background by click
Something like that:
boolean isShown;
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(isShown){
//set empty bg
}else{
//set right content
}
isShown = !isShown;
}});
I have a child view in my parent view.
Now I am using animation to show child view for 10 sec and hide it after that.
I want to show the child view even after 10 sec only if user touches it while animating.
So how can I determine if the child view is touched by user or not?
Does android provide any API to determine if view is touched by user at any instance of time?
You can set the "clickable" property to true and setOnClickListener(). like this:
exampleView.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//TODO
}
});
You can also use the setOnTouchListener for more information on the "click".
Take a look here.
I've searched around and have not come out with a solution (maybe not using the correct keywords).
So, I've a custom ListView which its item can be dragged around when the item is long clicked. Within its item, there's an ImageView and LinearLayout containing two TextViews. Actions are done when the LinearLayout or ImageView is clicked.
To do this, I've use setOnItemLongClickListener on my DragListView which extends ListView, to initiate drag action, and onInterceptTouchEvent to manage the drag action.
Then, I have built a custom adapter extending BaseAdapter and overrided its getView() to implement the child items in the row. The LinearLayout and ImageView have been setOnClickListener.
The problem is, the LinearLayout and ImageView are able to do their stuff, but the onItemLongClick isn't called.
The listener inside getView();
holder.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
For item long click (drag initiator)
setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
//Do something
}
Thank you very much!
I think that a gesture detector is one of ways to handle events.
Usually, however, a gesture detector is used when we want to detect a gesture not a long-press.
The reason why onItemLongClick isn't called is that onClickListener might consume a touch event.
In that reason, if you want to handle onItemLongClick, intercept touch event and dispatch it to views you want to handle.
You can find more details following link.
http://developer.android.com/guide/topics/ui/ui-events.html
Ok, just found out the solution myself.
Instead of using onItemLongClickListener, I create a gesture detector to detect for long press. Then I override dispatchTouchEvent and force to scan for long press first, then return super.dispatchTouchEvent and the other following touch events.
Suggestions are still welcomed!