Programmatically generated views - android

I'm adding a view at the top of the current view programmatically by inflating it from an XML.
The problem is that after the view is added and showed, I'm still able to press button from the underlying view. How can I prevent this?

Your new view needs to handle the touch event instead. Just create a new, empty touch listener for this purpose:
myTopView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;//true to specify that the event was consumed by the top view
}
});

Try to set an empty click listener for the view at the top.
For example:
myTopView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// empty, just to consume the event.
}
});
It will do nothing except to consume the event and not let pass it to underlying view.

Related

Android - preview by press and hold

I have a listview that contains some items, i want to allow the user to preview the data of a specific item by pressing and holding on the item. i want the preview window/popup to stay showed as long as the user is pressing.
I am trying to achivie the same preview functionality in IOS and instagram
i already implemented on longpress but not sure what to is the best thing to show to get a the desired result
lv.setAdapter(arrayAdapter);
lv.setLongClickable(true);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
Log.v("long clicked", "pos: " +pos);
return true;
}
});
any hints on how to implement that or best way to implement it ?
Well I'm using recycler view with images.
To show the image I use the long press listener calling this method:
public void publicationQuickView(Post post){
View view = getLayoutInflater().inflate( R.layout.quick_view, null);
ImageView postImage = (ImageView) view.findViewById(R.id.ivFeedCenter);
ImageView profileImage = (ImageView) view.findViewById(R.id.ivUserProfile);
TextView tvUsername = (TextView) view.findViewById(R.id.txtUsername);
tvUsername.setText(post.user.name);
Picasso.with(this).load(post.picture).priority(Picasso.Priority.HIGH).noPlaceholder().into(postImage);
Picasso.with(this).load(post.user.picture).noPlaceholder().into(profileImage);
builder = new Dialog(this);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
builder.setContentView(view);
builder.show();
}
I inflate the layout and inject into a Dialog.
To dismiss the dialog I'm using the RecyclerView.OnItemTouchListener() like this:
rvUserProfile.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if(e.getAction() == MotionEvent.ACTION_UP)
hideQuickView();
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent event) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}});
And finally:
public void hideQuickView(){
if(builder != null) builder.dismiss();
}
You could add a custom OnTouchListener to the view that represents a given item in you ListView (or RecyclerView or whatever). This allows you to detect when a gesture starts (i.e. first finger down) end ends (i.e. last finger up) or is canceled (e.g. gesture was actually a scroll and has been intercepted by the ListView).
The code you need to do that would look something like that:
itemView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int actionMasked = event.getActionMasked();
switch (actionMasked) {
case MotionEvent.ACTION_DOWN:
// show preview
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// hide preview
break;
default:
}
return true;
}
});
Edit: You may need to include some logic to detect a simple tap (e.g. measure if the whole gesture lasted not longer than ViewConfiguration.getTapTimeout()) and call v.performClick().
See the following answer on how to handle long press with recycler view: RecyclerView onClick
Once you have that, you can show a layout to display your data.
Check this out...
Implement a listener on long click listener
how to implement a long click listener on a listview
and then enable the visibiity of a hidden view. While doing that of course check this link out on how to enable dynamic position of the preview pane
How can I dynamically set the position of view in Android?

Disable onClicklistner while other touch listner is active

I am trying to implement UI like google keep.
In the main layout there is frame layout in which there is a FAM(Floating Actions Menu) and a blackshadow view which is visible when FAM is expanded.
What i want is that when i touch the shadow view it should Collapse the FAM. To do that i have implemented onTouchlistner on shadowview.
shadowview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(fam.isExpanded()){
fam.collapse();
}
return false;
}
});
But what happens is that when i touch the area with MyCardGridView's card (Which open's another activity) open's another activity. Which should not happen.
You should try to use return true; instead of return false;, because as the documentation says :
Returns:
True if the listener has consumed the event, false otherwise.
Maybe the issue here is caused by return false

Click events doesn't bubble up in RecyclerView

I'm wondering why click events doesn't bubble up if I click inside a RecyclerView.
My scenario uses a simple RecyclerView to show some TextViews, while its parent View has an OnClickListener registered to perform some action if someone clicks into the view.
If I click on e.g. a TextView the click event bubbles properly up to the parent. But if I click onto the RecyclerView the click doesn't bubble up.
The weird thing is, that if I set an OnClickListener directly on the RecyclerView it is also never fired. I assume that is the root of all evil.
So, why does my RecyclerView don't receive any click events? Why doesn't its parent receive any click events?
Hierarchy looks like this:
- View (has an OnClickListener) <- Why no clicks?
- RecyclerView (clickable=false)
- TextView
- TextView
....
I experimented with setFocusable() and setClickable(), but without luck.
PS: My current solution sets an OnClickListener on each TextView inside the RecyclerView and uses getParent() to propagate the click to the parent view using performClick() manually.
I use the special class handler for RecyclerView, the RecyclerView.OnItemTouchListener. I can be added from the consuming class. Here is the default implementation of the handler class:
recyclerView.addOnItemTouchListener( new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
//Put your code here.
//Called 2 times per touch, on pressdown and on release.
return false; //true will disable scroll view.
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
setDefaultBottomBar();
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});

How to restore an INVISIBLE view to be VISIBLE again when its space on the layout is touched?

Simply, I want when touching a textView to set itinvisible and when touch the same area of the screen on which the view was visible, to set it visible again. Please refer to the code below, it might clarify more the task i am intending to achieve.
Code:
targetTv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i(TAG, "button is touched #view ("+v.getX()+","+v.getY()+")");
Log.i(TAG, "button is touched #event ("+event.getX()+","+event.getY()+")");
if (targetTv.getVisibility() == targetTv.VISIBLE) {
targetTv.setVisibility(targetTv.INVISIBLE);
}else {
targetTv.setVisibility(targetTv.VISIBLE);
}
return false;
}
});
When the view is invisible, no events dispatched to it.
Because of that, you should wrap your view with a layout that is always visible, so it can detect events even when the contained View is invisible.
Then you have to add an onClickListener to the container, that toogles the visibility of the wrapped item.

Android pass OnClick Event to underlying View in FrameLayout

Idea: I want to display an image as background. On this image, somewhere on the screen there is a little clickable View. On top of this all, a black draggabke image with a little transparent whole (displaying the underlying Views).
This is solved with a FrameLayout, whereas the image is the first frame, the View is the second frame and the ''Viewport'' is the third frame.
Now the goal is, that the user can click that second frame, if it is visible in the viewport. The problem is, that this transparency is only ''in the png'', not in the frame itself. So the solution would be to propagate the OnClick coordinates to the underlying frame. How is this possible? Is there a functionality for a FrameLayout in such cases?
Making the upper layer as clickable false, i think the click event should be ignored by it and passed to the second frame
Create a custom class that extends FrameLayout
Add the following overridden methods
#Override public boolean onTouchEvent(MotionEvent event) {
return false;
}
#Override public boolean onInterceptTouchEvent(MotionEvent event) {
return true;
}
Then add touch listener to frame layout like
framelayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
underLyingView.onTouchEvent(motionEvent);
return false;
}
});
This will pass onclick event to the view underLyingView which lies under FrameLayout

Categories

Resources