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>
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;
}
})
I have a view and I want to show it when I click on button/layout and hide it when I touch somewhere else. How can I do it?
I wrote some code in dispatchTouchEvent(Motion Event) and it's working. But, I think there must be another way to do it.
You can fill the outside of your RecyclerView with another clickable view and implement setOnTouchListener method for that view. Here's an example:
Let's say we have got RecyclerView at the top of our RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scrollbars="vertical" />
<!--View below is just to fill the remaining space. We will use this view to catch outside touch-->
<View
android:id="#+id/outside_detector"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/recyclerView"
android:clickable="true"
android:focusable="true"/>
</RelativeLayout>
And we want to hide and show our recyclerview when we click outside of RecyclerView:
((View) findViewById(R.id.outside_detector)).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN){
if(recyclerView.getVisibility() == View.VISIBLE){
recyclerView.setVisibility(View.INVISIBLE);
}else{
recyclerView.setVisibility(View.VISIBLE);
}
}
return true;
}
});
If you want to show recyclerview on button click, then just write recyclerView.setVisibility(View.VISIBLE) method inside button ClickListener!
Hope this helps!
I have frame layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/widgetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center" >
</LinearLayout>
<LinearLayout
android:id="#+id/widgetOverlayFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
</LinearLayout>
First layout contains widget layout, and second one is transparent and receives onLongClick for dragging method.That works, the problem is when i want to interact with widget, to click something, click event is taken by overlayFrame. How can i pass click event trough overlayFrame to widgetView?
EDIT:
Now I'm trying to attach GestureLister to overlayFrame LinearLayout, to somehow see difference between MotionEvent that represent single click and long click. The problem that I'm facing is that OnLongPress in gesture listener is always called whatever I do, single click or long click.
Is there an explanation for this behavior? Tnx!
In my case I added flag to layoutParams of my view, which should have passed a touch event under:
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
This way view will ignore all touches (like view with visibility GONE)
Instead of using GestureListener you can override the onTouchListener in this way.
This will call longPress when the timer runs out and if an up comes in between it will cancel the LongPress
CountDownTimer c;
long time=5000;
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
c= new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
time=millisUntilFinished;
}
public void onFinish() {
Log.i("","LONG PRESS");
}}.start();
break;
case MotionEvent.ACTION_UP:
if (time>0) {
Log.i("example","short click");
c.cancel();
}
break;
}
return true;
}
Maybe setting the overlay's attribute android:focusable="false" or android:focusableInTouchMode="false" would be enough.
You could also make your own overlay widget and override its onInterceptTouchEvent method to return false always.
public class NotTouchableLinearLayout extends LinearLayout {
// Override constructors
// ...
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
Instead of setting a GestureListener on the top layout, you should create your own class that extends LinearLayout and to override it's onTouchEvent method.
There you can implement the logic of long click \ short click etc.
The click events will first be sent to the widget layout, and only if it doesn't handle them (hence it's onTouchEvent returns false), you will get them on the top layout.
edit:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/widgetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center" >
</LinearLayout>
<com.example.touchtesting.MyLinearLayout
android:id="#+id/widgetOverlayFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
</com.example.touchtesting.MyLinearLayout>
</FrameLayout>
change the com.example.touchtesting to the name of your package.
and this is the class:
public class MyLinearLayout extends LinearLayout{
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
private long startClick = 0;
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startClick = ev.getEventTime();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (ev.getEventTime() - startClick < 500) {
Log.i("example","short click");
}
else {
Log.i("example","long click");
}
break;
}
return true;
}
}
To get the long clicks to pass through the widgetView LinearLayout add android:longClickable="true". (For ordinary clicks add android:clickable="true")
So widgetView becomes:
<LinearLayout
android:id="#+id/widgetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:longClickable="true">
</LinearLayout>
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"
I have a simple WebView example app that has the following layout:
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="#+id/urlToLoad"
android:hint="Type url to load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="#+id/webviewgo"
android:text="Go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false" />
<com.example.exWebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
It works as expected, but I am interested in routing a keyboard event to the WebView. Currently, even if I select the WebView (and scroll etc.), when I type any key, it goes to the EditText control... which is not what I want.
How do I make a keyboard event go to the WebView instead?
According to this thread, all you have to do is set, inside the main activity's OnCreate(), an OnTouchListener() with a requestFocus():
mWebView = (MyWebView) findViewById(R.id.webview);
mWebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
What this code basically does is lock focus on any down or up event the moment it reaches your view. Note how it returns false, so that the event can further propagate to other views, as if it weren't handled.