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 …
Related
I'm trying to make an HoziontalScrollView with items :
When I move the scrollbar to the left, the HoziontalScrollView goes right and vice versa, which will make the user confused about how to use the scrollbar to scroll instead of moving finger over the HoziontalScrollView
XML :
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fadeScrollbars="false"
android:scrollbarStyle="outsideInset"
android:scrollbarThumbHorizontal="#drawable/outer_windows_background"
android:scrollbarTrackHorizontal="#drawable/scrollbar_track_horizontal"
tools:showIn="#layout/activity_photo_editor">
//ITEMS
</HorizontalScrollView>
Thank you.
Try this :
HorizontalScrollView hsv;
hsv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
event.setLocation(-1*event.getX(), event.getY());
hsv.dispatchTouchEvent(ev);
return true;
}
})
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 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
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"