ImageView onTouchListener with Gallery - android

I want to add to the ImageView the pinch feature. So I extended the ImageView and I implemented the OnTouchListener interface. The ImageView is clickable. The feature itsel works, but when I use the custom ImageView with the Gallery widget, the gallery does not swipe. If I set focusable and clickable to false, gallery swipe. How can I let they work together?
Code for the custom ImageView
public class Custom6 extends ImageView implements OnTouchListener {
public Custom6(Context context) {
super(context);
setClickable(true);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(this);
}
public Custom6(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
setClickable(true);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(this);
}
public Custom6(Context context, AttributeSet attrs) {
super(context, attrs);
setClickable(true);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
return true;
}

Return false in ontouch lister and then try i hope it will work...

Related

How to disable the overscroll in scrollview but only in the top for android?

I know the we can turn off the overscroll via overscroll mode. But how do i control it in a way the that bottom has the overscroll effect while the top doesn't has the overscroll effect in a scroll view?
There are one method in ScrollView class called getBottomFadingEdgeStrength() so you can create a Class that extends ScrollView and override the above method. like
/**
* Created by droidwithme on 23/05/16.
*/
public class MyScrollView extends ScrollView {
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected float getBottomFadingEdgeStrength() {
return 0.0f;
}
}
and return a 0 to this method. this may help you.

Customized Scrollable EditText in Scrollview Makes the Scrollview Jumps

I want to have a EditText in a scrollview and this EditText needs to be scrollable. So I made a customized one as follow:
public class myEditText extends EditText {
public myEditText(Context context) {
super(context);
}
public myEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public myEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
this.getParent().requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
}
}
This EditText has only one problem, that is when the text gets really long, the Scrollview containing it jumps for some reason. This makes me unable to edit text after a certain length. Is anyone know why that happens?
I think I solved it.
The issue was when there is a cursor in the EditText, the bringPointIntoView(int offset) method will make EditText scroll to the position where the cursor is at, so that the cursor will be in your sight and you can edit the text. Usually that's how it works, but when the EditText is in a ScrollView, instead of scroll the EditText, it makes the ScrollView scrolls as well. That's the reason I saw the ScrollView jumps.
The solution is simply override the bringPointIntoView method.
So if anyone wants to use a scrollable EditText in a ScrollView, I think this may help:
public class myEditText extends EditText {
public myEditText(Context context) {
super(context);
}
public myEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public myEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
this.getParent().requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
}
#Override
public boolean bringPointIntoView(int offset) {
return false;
}
}

Implement View.OnTouchListener on custom control

I created a view that extend RelativeLayout, and I what that, this view to be able to handle touch events. So what I thought about was to implement the OnTouchListener in my custom control, like this:
class MyCustomControl extends RelativeLayout implements View.OnTouchListener {
public MyCustomControl (Context context) {
super(context);
}
public MyCustomControl (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomControl (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
other_methods;
#Override
public boolean onTouch(View view, MotionEvent event) {
//event handle here.
}
}
It doesn't work but if I do something like this, it does:
MyCustomControl control = (MyCustomControl) LayoutInflater.from(context).inflate(R.layout.my_control, null);
control.setOnTouchListener(control);
What should I do in order to have the touchListener built-in(already defined and activated) in my custom control ?
I know, it's late, but I solved my case by adding
this.setOnTouchListener(this);
to constructor

android- How to detect double tap on imageview and start new fragment

I have done a lot of search over the topic and have even referred the official documentation from android developer website. But still not clear with the concept.
I have read that for implementing the touch gestures i need to use the GestureDetector and MotionEvent packages. But its implementation kept me confusing over the things.
What i simply want is, my layout includes a many of textviews along with two imageviews. I want to detect a double tap on my images and want to start a new fragment activity. In the new fragment activity i want to show the same image in full screen in landscape mode.
I have done ton of reading but it kept me confusing.
Please help.
Thank you
Here is Double Tap Gesture ImageView.
public class CustomImageView extends ImageView {
private Context context;
private GestureListener mGestureListener;
private GestureDetector mGestureDetector;
public CustomImageView(Context context) {
super(context);
sharedConstructing(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
sharedConstructing(context);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
sharedConstructing(context);
}
private void sharedConstructing(Context context) {
super.setClickable(true);
this.context = context;
mGestureListener=new GestureListener();
Log.e("Adding", "Listener:::");
mGestureDetector = new GestureDetector(context, mGestureListener, null, true);
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
//..my other code logic
invalidate();
return true; // indicate event was handled
}
});
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDoubleTap( MotionEvent e ) {
// TODO DoubleTap Comparison
Log.e("onDoubleTap","onDoubleTap");
return true;
}
}
}
Reference Link

Click listener for drawable inside custome framelaout class android

hi I am developing android application in which i am using one custom frame layout class. Inside that class I am using one drawable and with the help of canvas i m drawing that. I did this in following way :
public class BackgroundContainer extends FrameLayout implements OnTouchListener{
Drawable mShadowedBackground;
public BackgroundContainer(Context context) {
super(context);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mShadowedBackground =
getContext().getResources().getDrawable(R.drawable.actionbar);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Log.i("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", "OOOOOOOOOOOOOOOOOOOOOO");
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: {
invalidate();
}
}
return true;
}
#Override
protected void onDraw(Canvas canvas) {
mShadowedBackground.setBounds(getWidth()-150, 0, getWidth(), mOpenAreaHeight);
canvas.save();
canvas.translate(0, mOpenAreaTop);
mShadowedBackground.draw(canvas);
canvas.restore();
}
}
}
Now I am want to listen click even on my drawable. I implement ontouch event but its not working. Am i doing it in wrong way. Need help thank you.
Drawables are not clickable as it is not considered as a view.
Like deepak already said: by implementing the corresponding listener you just provide the behavior what should happen for a specific event. You still need to add the listener :) In your case this would help (in your init()):
setOnTouchListener(this);

Categories

Resources