Clicking on Edittext inside nested Scrollview scrolls outer scrollview - android

I have a parent horizontalScrollview that contains various layouts. In one particular layout there is a scrollview. Inside that scrollview there is edittext.
Now when I click on any edittext for input, the horizontal scrollview automatically scrolls the screen to some other layout and I am not able to see the current active layout(containing scrollview).
I know nested scrollviews is a bad coding practice but I can not change it in this case as there will be large amount of rework. Is there any workaround for this.
Following is the xml code for main layout containing horizontalscrollview:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView
android:id="#+id/CloudHScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and following is the code for one of the child layouts that is inserted later at some stage. This layout contains the scrollview which has edittexts:-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:background="#drawable/blueoverlay"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="300dp"
android:paddingTop="90dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:gravity="right"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black" />
</LinearLayout>
</LinearLayout>
</ScrollView>

I'm not sure if this will work, but you can try extending outer ScrollView class and handle its onScroll events on your own.
look here:
listening to scroll events horizontalscrollview android
Android: how to listen for scrolling events?

I had the same issue, solved it by adding the following to AndroidManifest.xml file for the corresponding activity.
android:windowSoftInputMode="adjustResize"

Related

Cause Layout get focus before its views

I am developing an application for Android TV.
I have a layout which have horizontal LinearLayout (custom) which is used for navigation and below it a container for fragments.
What I want is when a user is focused on any item in the fragment (assuming that the fragment has only 1 row of views), when he clicks on UP on the D-Pad, the LinearLayout above it will get the focus and not the closest.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<com.myApp.tv.view.categories.CategoriesLayout
android:id="#+id/home_categoriesLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="30dp"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/home_contentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I really prefer not to interfere the fragment code.
Please give me your suggestions, thank you

Android ScrollView automatically scrolls to the bottom of the view

I have created one activity which is containing one map, image and another text view, and I have added "scrollview" tag for it. But after the activity starts it scrolls to the end of the page automatically. Please tell me why this happens and how to stop it from moving to end so I can scroll it by myself.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.radikallab.earch.DetailsActivity"
android:background="#drawable/background10">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Title"
android:textStyle="italic"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/background4"
android:id="#+id/iv"/>
<TextView
android:id="#+id/rat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Title"
android:textStyle="italic"/>
<TextView
android:id="#+id/addr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="Title"
android:textStyle="italic"/>
<WebView
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginTop="10dp"
android:id="#+id/webView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I also face the same scenario once but adding the descendantFocusability attribute to the ScrollView's containing LinearLayout, solve my issue.
android:descendantFocusability="blocksDescendants"
you can use this as :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
Set android:focusableInTouchMode="true" in your linear layout:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
In case you do want to set this manually using Java;
Try this- it really works:
Use FOCUS_UP to scroll to the top and FOCUS_DOWN to scroll it to the Bottom.
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(View.FOCUS_UP);
}
What worked for me is a combination of the answers from Punit Sharma and FAЯAƸ.
My issue was a scrollable view (RecyclerView) inside another scrollable view (NestedScrollView) -- same as your WebView inside a ScrollView.
Wrapping my inner scrollable view inside a FragmentLayout with attribute android:descendantFocusability="blocksDescendants" solved my auto-to-bottom-scrolling issue.
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
...>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/image_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Actually i found a simple answer for this just add "setfocusable(false)" in the associated java file.....in this case the "webview" coz its scroll there first....so add "webview.setfocusable(false)" in the associated webview java class.
Actually, we will face the scrolling problem if child inside ScrollView supports scrolling too like ListView and in your case WebView.
I found a similar question with some workaround. It supports vertical scrolling but horizontal scrolling is not supported if HTML page exceeds the WebView width.
See this for reference.

android linearlayout has no known child tags Error

I have following xml in my android app:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/msngr"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo" />
</LinearLayout>
</ScrollView>
Upto this code is fine, but i am not able to add any other components like TextView,Edittext in this.
I am not getting any autosuggest for it and viewing in redline that:
<linearlayout> has no known child tags
How can i components in linear layout?
plz help me.
This is because in ScrollView you can only add one Child View... It can't add any other view in it... Because ScrollView host only one child view...
also remove space from Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo"/>
</LinearLayout>
because space not allowed......
and if you want to add child view then add it to only LInear Layout

How to prevent a scrollview from scrolling to a webview after data is loaded?

So I have a fascinating problem. Despite the fact that I'm not manually or programmatically scrolling my view, my WebView is being automatically scrolled to after the data inside it loads.
I've got a fragment in a viewpager. When I first load the pager, it works as expected and everything is shown. But once I "flip the page" the data loads and the WebView pops up to the top of the page, hiding the views above it, which is undesirable.
Does anyone know how to prevent this from happening?
My layout looks like such:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="2dp"
android:text="Some Title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/article_title"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/LL_Seperator"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#color/text"
android:orientation="horizontal" >
</LinearLayout>
<WebView
android:id="#+id/article_content"
android:layout_width="match_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/article_link"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:text="View Full Article"
android:textColor="#color/article_title"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
I'm also not giving focus to anything. By default, it seems to automatically scroll to the WebView after it has loaded. How do I prevent this?
I had the same problem, after hours of trying several ideas, what finally worked for me was simply adding the descendantFocusability attribute to the ScrollView's containing LinearLayout, with the value blocksDescendants. In your case:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
Haven't had the problem reoccur since.
You can simply add this to your LinearLayout: android:focusableInTouchMode="true". It works for me.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical" >
You should create new class extend ScrollView, then Override requestChildFocus:
public class MyScrollView extends ScrollView {
#Override
public void requestChildFocus(View child, View focused) {
if (focused instanceof WebView )
return;
super.requestChildFocus(child, focused);
}
}
Then in your xml layout, using:
<MyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background" >
That works for me. The ScrollView will not auto scroll to the WebView anymore.
Adding these line in main layout solves the problem
android:descendantFocusability="blocksDescendants"
Like this:
<com.ya.test.view.MyScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true" >
Probably there are people who have the same problem I was having, so I'll help out.
I was trying to put android:descendantFocusability="beforeDescendants" in my main ScrollView as following:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants">
/*my linearlayout or whatever to hold the views */
and it wasn't working, so I had to make a RelativeLayout the parent of the ScrollView, and place the android:descendantFocusability="beforeDescendants" in the parent aswell.
So I solved it doing the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
/*my linearlayout or whatever to hold the views */
I added: android:overScrollMode="never" in my ScrollView and set the height to wrap_content.
My view was very complex as it was legacy code with LinearLayout inside LinearLayout inside LinearLayout.
This helped me, hope it will help someone else too!
I had to use the fully qualified name for MyScrollView, otherwise I got an inflate exception.
<com.mypackagename.MyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background" >

glsurfaceview inside a scrollview, moving but not clipping

I have a scrollview with a linear layout inside. One of the elements inside this linearlayout is a glsurfaceview.
This all works correctly and when I scroll the glsurfaceview moves up and down however when the glsurfaceview reaches the top or bottom of where it should of the scrollview where it should be clipped it is not and is continued outside of the scrollview. This screenshot should make it clearer:
Don't think it's completly nessecary but here is my layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
>
<!-- LOTS OF SEEKBARS/TEXTVIEWS -->
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.4"
android:layout_marginRight="10dip"
android:layout_marginLeft="10dip"
android:orientation="horizontal" >
<android.opengl.GLSurfaceView android:id="#+id/glview"
android:layout_width="100px"
android:layout_height="250px"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_marginTop="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- OK/CANCEL BUTTONS -->
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
All help much appreciated :)
Hosting SurfaceViews inside ScrollView (or Listview, etc.) is currently not supported.
You may use below lines for removing over scrolling :
glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
glView.setZOrderOnTop(true);
glView.setZOrderMediaOverlay(true);
And for removing black background :
linearLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
linearLayout.addView(glView);
Also add below line in your manifests file :
android:hardwareAccelerated="true"
It is not about support as answered abowe. The cause of issue is that GLSurfaceView is not on the view level. In short: for you case you have GLSurfaceView at the first level, then other views with holes in views abowe GLSurfaceView (this handles by windows manager, and it dosent matter where you put GLSurfaceView on xml, it will be on first level in any case and others views on next level)
So, what you need is to force view update, and there is work around: you should put scroll with GLSurfaceView or (RecyclerView with GLSurfaceViews) "between" two FrameLayouts. something like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
---- here should be linear layout with your glView, or RecyclerView instead if required ----
</ScrollView>
--- this frame also required: ---
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
</FrameLayout>

Categories

Resources