I have a custom view and EditText element in my relative layout, i can't receive touch events in my custom view, how can i propagate touch events to my custom view;
My layout looks like ;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" >
<com.test.ui.CustomView
android:id="#+id/noteView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="#+id/etPageInput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top" />
</RelativeLayout>
I add touch listener to my EditText and Custom view as like ;
EditText pageInput = (EditText) findViewById(R.id.etPageInput);
pageInput.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
CustomView pageView = (CustomView) findViewById(R.id.noteView);
pageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch event received by custom view !!!")
return true;
}
});
I'm not able to receive touch events in my CustomView, any idea what am i missing ?
try to set clickable as true in xml for your custom view
and
try to setonclicklisener
Related
In FragmentA, I have a scrollable layout. Inside the layout, I have
GestureOverlayView.
<android.gesture.GestureOverlayView
android:id="#+id/signGesture"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#android:color/white"
android:fadeEnabled="false"
android:fadeOffset="10000"
android:gestureColor="#color/black"
android:gestureStrokeLengthThreshold="0.1"
android:gestureStrokeType="multiple"
android:orientation="vertical">
Is there a way to make the layout unscrollable when GestureOverlayView is touched ? I can't draw anything because the layout is moving !
You should use OnGesturePerformedListener on your gesture view and there you disable your scrollView scrolling by returning true on the touch listener
gesture.addOnGesturePerformedListener(new OnGesturePerformedListener() {
public void onGesturePerformed(GestureOverlayView v, Gesture g) {
mScrollView.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
}
});
EDIT
You can take a look at this answer , it actually present a custom ScrollView that will intercept the scroll touch and disable it when you perform gesture listener.
I am having linear layout, in which it has one imageview and one textview, when i touch the linear layout, it should highlight the image as well as text inside the linear layout with different color. I have tried using set background for the linear layout when it is pressed since i have used ontouch-listener it doesn't shows the color even i tried to change the colors programmatically but it didn't works.
Can anyone tell me how i can change the imageview and textview color when a linear layout is touched.
Xml:
<LinearLayout
android:id="#+id/linear_otherexpense"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#drawable/linearlayout_check"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="#+id/img_otherexpense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:scaleType="centerInside"
android:src="#mipmap/expense"
android:tint="#6666FF" />
<TextView
android:id="#+id/tv_otherexpense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:text="#string/dayexpense"
android:tint="#color/colorPrimary" />
</LinearLayout>
Selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" />
Code:
li_otherexpense.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(NewDaybook_Activity.this, AddingNewExpense.class));
NewDaybook_Activity.this.finish();
return false;
}
});
You can use this code. this is working fine for me .May be it will helpfull
findViewById(R.id.linear_otherexpense).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
// When you Touch Down
// U can change Text and Image As per your Functionality
break;
case MotionEvent.ACTION_UP:
//When you Release the touch
// U can change Text and Image As per your Functionality
break;
}
return true;
}
});
Try this,
linearLayout = (LinearLayout) findViewById(R.id.linear_otherexpense);
imageView=(ImageView)findViewById(R.id.img_otherexpense);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
linearLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent));
imageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
return true;
}
});
I can't get onTouch event triggered on a container when clicking on its ScrollView child.
I'm using API 12 on a Galaxy tab 10.1.
Anybody can help?
Thanks
Activity
public class TestActivity extends Activity{
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.dude);
LinearLayout mylayout = (LinearLayout) findViewById(R.id.mylayout);
mylayout.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
// Only called when touched outside the ScrollView
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
/* do stuff */
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
/* do stuff */
}
return true;
}
});
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mylayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="Touch here trigger parent's onTouch"
android:textColor="#000000"
android:textSize="40sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="#b00000"
android:gravity="center"
android:text="Touch here DOES NOT trigger parent's onTouch"
android:textColor="#000000"
android:textSize="40sp" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
mylayout.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
// Only called when touched outside the ScrollView
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
/* do stuff */
return true;
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
/* do stuff */
return true;
}
return false;
}
});
this should work... but you no longer have auto-scroll when you fling it hard... it'll be a sticky scroll moving only as much as you drag.
Did u try to create ONE OnClickListener and add it to all childs?
Maybe this could solve your
As the ScrollView makes it childs scrollable it wouldn't have an own area to click in.
(Correct me if I'm wrong ^^)
You probably need this code
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
onTouchEvent(ev);
return false;
}
have EditText widget along with some widgets placed in ScrollView.
I have set the property of EditText with android:scrollbars = "vertical" to enable vertical scrolling inside editText.
Now when i launched the activity, editText has focus and it shows vertical scrollbars for some seconds.
The issue here is when i try to scroll within EditText, ScrollView moves.
How to enable scrolling within EditText and not within scrollview.
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
.
.
.
<EditText
android:id="#+id/smset"
android:layout_width="match_parent"
android:gravity="top|left"
android:height="100dip"
android:inputType="textMultiLine" >
</EditText>
.
.
.
</LinearLayout>
</ScrollView>
In your java file
EditText dwEdit = (EditText) findViewById(R.id.DwEdit);
dwEdit.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (view.getId() ==R.id.DwEdit) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
In xml
<EditText
android:id="#+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textCapSentences">
</EditText>
You can't place something that can scroll inside something that scrolls, too.
Android can't say for sure which element you want to be scrolling when you touch the screen so it isn't supported at all. (ignoring some hacks which are pretty bad practice).
One such hack is:
scroll.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
Note that to enable scrolling in ScrollView, you will have to run the same function, but with "return false;" instead.
I wouldn't say it's bad practice if done properly, but it's not something naturally expected.
I regularly put scrollable views inside something that scrolls, but you have to lock the outside layer and enable scrolling on the inside. Ideally, you should do something else, but it is possible.
You can modify ScrollView to be lockable, as in here.
Then programatically lock LockableScrollView scrolling when the internal thing is touched.
smset.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
// Disables LockableScrollView when the EditText is touched
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
scrollView1.setScrollingEnabled(false);
}
// Enables LockableScrollView when the EditText is touched
if (event.getAction() == MotionEvent.ACTION_UP)
{
scrollView1.setScrollingEnabled(true);
}
return false;
}
});
The downside of this is that it will disable scrolling when the EditText is touched, but it shouldn't cause any other side-effects.
In kotlin, you can write an extension function for all ExitTextViews and use them everywhere :
fun EditText.setTouchForScrollBars() {
setOnTouchListener { view, event ->
view.parent.requestDisallowInterceptTouchEvent(true)
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_UP -> view.parent.requestDisallowInterceptTouchEvent(false)
}
false
}
}
but add these lines in your EditTextView XML too:
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textMultiLine"
My application need to 2 scroll views(sv1,sv2). sv1 is main scroll view and sv2 is sub scroll view of sv1.
is it possible? then how to do it.
Following one is my answer. it is working in simulator only but not working in device means device support sv1 only sv2 is not working.
How to do it?
thanks
<ScrollView android:id=”#+id/parent_scroll”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:background=”#drawable/dotted_bg”
android:focusableInTouchMode=”false”>
<LinearLayout />
<LinearLayout />
<LinearLayout >
<ScrollView android:id=”#+id/child_scroll”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#drawable/text_box_bg”>
<TextView android:id=”#+id/text_description”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:textColor=”#color/gray”
android:textSize=”12dip”
android:padding=”5dip”
android:scrollbars=”vertical”/>
<!–ScrollView>
</LinearLayout>
Step 1 : Provide unique id to both the scrollview.
Step 2 : get reference of that two scrollview in your activity.
parentScroll=(ScrollView)findViewById(R.id.parent_scroll);
childScroll=(ScrollView)findViewById(R.id.child_scroll);
Step 3: Now set touch listeners for both.
parentScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG,”PARENT TOUCH”);
findViewById(R.id.child_scroll).getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
Log.v(TAG,”CHILD TOUCH”);
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Done …