Blank space when using ScrollView Activity in combination with adjustPan - android

My Linear Layout was taking too much space. So I decided to put it inside a ScrollView.
The layout has an EditText at the bottom for user input. To make the layout resize itself in AndroidManifest.xml for the Activity I put:
android:windowSoftInputMode="adjustPan"
It was OK for when I just had a Linear Layout, but now in combination with Scroll View I get a blank space, I think Android is trying to make space for the keyboard.
Layout.xml:
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.LinearLayoutCompat
android:isScrollContainer="false"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#color/colorPrimary">
...
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>
Before clicking keyboard
After clicking keyboard

Replace with following code
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.LinearLayoutCompat
android:isScrollContainer="false"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#color/colorPrimary">
...
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>

Related

How to Set a Button at a fixed position even when respective Activity has ScrollView

Is there a way to use some non scrollable view when the activity has ScrollView component?
I want that my button behaves similar to the button in the question below, I want it fixed on the bottom of the UI too:
how to set a button at a fixed location on the bottom of UI?
Here's my layout code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent ">
<FrameLayout
android:id="#+id/map_frameview"
android:layout_width="match_parent"
android:layout_height="200dp">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
</LinearLayout>
</LinearLayout>
</ScrollView>
Thanks in advance!
The link you have attached in your question shows how you can do it. You can use RelativeLayout for this. The idea is, you need to tell a button to stay on bottom of the screen & tell the scrollview to stay above the Button along with it's scrollable content like:
<?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">
<Button
android:id="#+id/btnMy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/btnMy">
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/btnMy"
android:orientation="vertical">
<FrameLayout
android:id="#+id/map_frameview"
android:layout_width="match_parent"
android:layout_height="200dp">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>

In a fragment,when an image is set as the background of a RelativeLayout in ScrollView, the RelativeLayout is misbehaving

I have an activity with this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity" />
Then I have this fragment layout, which I am transacting in the fragment_container of the activity:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_login">
</RelativeLayout>
</ScrollView>
The drawable bg_login is:
But the strange thing that is happening is, the background image(bg_login) hides all the content in RelativeLayout, in the preview pane as well as when I run the app. I can't find out why this is happening! Please help.
I dont know what you want to do, are you want have multiple background in relative layouts when scrolled or not.
But i assuming you, the scroll view when have a children element with background is not best practice.
So scoll view must set to a children from parent have a background for good practice.
This may be can help you :
<?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"
android:background="#drawable/bg_login">
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>

Keep toolbar fixed and other view not

I have the following layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<!-- Content -->
</ScrollView>
<include layout="#layout/fixed_layout_when_keyboard_not_visible" />
</RelativeLayout>
In AndroidManifest I have the android:windowSoftInputMode set to adjustResize which doesn't 'squeeze' the content when keyboard is visible and keeps the Toolbar fixed at all times, even when I scroll.
However, I have another view at the bottom, out of the ScrollView, which I want to be fixed as well but at the same time not block any content when keyboard pops up- right now it moves with the keyboard as of the following property layout_gravity="bottom".
I could just toggle the visibility of that layout when keyboard shows/hides but that doesn't seem the most elegant solution. What would you suggest? Any help very much appreciated :)
You should solve the issue by telling the ScrollView to not register as a scroll container by adding android:isScrollContainer="false". Your new ScrollViewshould look like this:
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:isScrollContainer="false">
<!-- Content -->
</ScrollView>
Try below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<!-- Content -->
</ScrollView>
</RelativeLayout>

How to Scroll A Part of An Activity

I have a question that i couldn't Find Any Answer For it.
How can I Make An Layout Scrolling And Make Another Layout that doesn't Scroll?
On the other hand i want to make a layout Including Some buttons that scrolls if necessary and i want to put Two buttons at the bottom of the screen and i don't want them to disappear when scrolling.
<?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:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons and TextViews
</RelativeLayout>
</scrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
// two Moveless Buttons
</RelativeLayout>
But it doesn't Work.
Your ScrollView has width and height that matches the parent. This means it takes up the entire space available leaving nothing for the RelativeLayout. You probably want to wrap what you already have inside a LinearLayout and then use layout_weight attribute to divide the space up.
I'm not sure I understand what You want to do. But try this and give feedback
( code example not tested in Android Studio)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons an TextViews
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp (for example)>"
android:layout_alignParentBottom="true">
// two Moveless Buttons
</RelativeLayout>
</RelativeLayout>
Make your root a vertical linearlayout with a scrollview, to hold the scrollable views, and a RelativeLayout, to hold your buttons.
You can take advantage of the LinearLayout's weighting ability by applying a weight of 1 to the Scrollview so it takes up as much room it can while the button-holding-RelativeLayout will be the size of your biggest button.
Here is an example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons an TextViews
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
// two Moveless Buttons
</RelativeLayout>
</LinearLayout>
HTHS!

Horizontal ScrollView in fragment Android

I would like to make my ScrollView filling out the full layout of a fragment and scrolling horizontally, not vertically.
How can I achieve that?!
Like the album covers
Thanks
Its not vertically, it is horizontally
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
// Add your ImageButtons
</LinearLayout>
</HorizontalScrollView>
You have to set the scrollview to
android:fillViewport="true"
Below is the complete code
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myscrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
</ScrollView>

Categories

Resources