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;
}
})
Related
I want to scroll the whole layout above keyboard but its not working. I was able to scroll manually but want it to scroll
programatically when a user sets focus on the edit text.
Please help me. Thanks in advance
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true"
android:paddingBottom="?attr/actionBarSize">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
......
edit_text_code.setOnFocusChangeListener { view, hasFocus ->
if (hasFocus) {
scrollView.post {
scrollView.fullScroll(NestedScrollView.FOCUS_DOWN)
}
}
}
Method 1:
First get the x/y coordinates of your Edittext using View.getLocationOnScreen (int[] location) ( After the method returns, the array contains the x and y location in that order)
Then do :
scrollView.scrollTo(x, y);
If above method doesn't work try below method :
Method 2:
edit_text_code.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// put your edit text id below
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;
}
});
XML Code :
<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>
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 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"
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 …