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
Related
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;
}
}
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
I was looking at the documentation for the Button widget and noticed that it is a subclass of TextView. Is there any real difference between a Button and a TextView if I use the same selector drawable on both. In other words, I already am using TextViews with selectors for my app's "buttons", but is there anything to be gained from using the Button class instead?
No, there's essentially no difference between TextView and Button. Button just comes styled as a button out of the box.
Here is the source for Button from API 19.
#RemoteView
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(Button.class.getName());
}
#Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(Button.class.getName());
}
}
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...
how can I find out when onDraw of a View has finished?
Thanks!
I don't know what you're ultimately trying to achieve, but if you need to run some logic once drawing has been completed in an Activity, then you can call View.post(Runnable) and put your logic inside there. onDraw would have likely taken place once the code in your Runnable has been reached since it put that Runnable on the message queue.
Place a boolean member in your View class and set it to true when onDraw is called (If you simply want to be able to test that a View has drawn).
Or if you want some sort of post-draw "event" call to execute a new thread- just put it at the end of onDraw.
Another suggestion how it could be done:
public class FVRTraceAbleListView extends ListView {
ListViewListener listener;
public interface ListViewListener {
void onPostDraw();
}
public FVRTraceAbleListView(Context context) {
super(context);
}
public FVRTraceAbleListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FVRTraceAbleListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FVRTraceAbleListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setListener(ListViewListener listener) {
this.listener = listener;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (listener != null) {
listener.onPostDraw();
}
}
}