Java Android ScrollView buttons always at the top - java

Hi I have a ScrollView and inside I have a LinearLayout and inside I have a buttons. I don't know how I can have this buttons always at the top when I scroll a view. Now when I scroll view a buttons disappear.
My code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:weightSum="1">
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:layout_marginTop="0dp">
<Button
android:id="#+id/cpic"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:text="Zdjęcie" />
<Button
android:id="#+id/up"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:text="Wyślij" />
</LinearLayout>
<ImageView
android:id="#+id/Imageprev"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="58dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>

what I understand from your question i think you can achieve it by putting your button outside your scroll view inside XML , this would give you a fix button.
hope this helps.

Related

I'm using a framelayout and inside it I want a listview and a floating action button, but the listview doesn't extend

Why is this not working?
It's a problem with the listview.
I've tried in many ways and ending up with the button not showing
<FrameLayout 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"
tools:context=".fragments.notes.NotesFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_baseline_add_24" />
</LinearLayout>
</ScrollView>
</FrameLayout>```
The first problem is that you cannot put a ListView inside a ScrollView, ListView itself is scrollable and the second problem it's with the margin if you use a bottom menu, I also encountered this problem. Try this one
<FrameLayout 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"
tools:context=".fragments.notes.NotesFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="70dp"
android:src="#drawable/ic_baseline_add_24" />
</RelativeLayout>
</FrameLayout>```

Android - LinearLayout not scrolling inside ScrollVIew

I have the problem with LinearLayout not being scrollable inside the ScrollView, instead it just appears to go beyond the screen frame (look closely at the bottom of the screenshot linked below).
I have the following structure in my XML layout:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<!-- More TextInputLayouts -->
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src = "#drawable/ic_done"
android:tint = "#color/white"/>
</android.support.design.widget.CoordinatorLayout>
No solutions found were able to fix the problem: I tried fillViewPort="true" and it didn't do it for me, I also tried commenting CoordinatorLayout - same result, layout_heights seem to be set properly as well.
Moreover, I tried to adjust LinearLayout's height progrmatically, what didn't help just as well.
I'm stuck with this problem for a while now and would really appreciate any help in this regard :)
Screenshot
It works for me with the XML below. I didn't change much, just added an orientation to the LinearLayout and some margin and awful background colors so you can see which view is moving and which is fixed while you scroll.
When you drag up and down, the teal box (the LinearLayout) moves up and down as expected within the red box (the ScrollView). If your LinearLayout is taller than the screen, it will go off the bottom, that's the point of the ScrollView.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:background="#ff0000"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_margin="16dp"
android:background="#008080"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
you put linear layout orientation in xml code
like..
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
or
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
Try this:-
<LinearLayout 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:background="#color/whiteColor"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- vertical ScrollView to make all the items or views scrollable -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<!-- LinearLayout Inside ScrollView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- create a Linear Layout with horizontal orientation and weightSum property -->
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<EditText
android:id="#+id/lastName"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.4"
android:background="#color/editTextBack"
android:hint="Last Name"
android:imeOptions="actionNext"
android:paddingLeft="10dp"
android:singleLine="true"
android:textColor="#color/blackColor" />
</LinearLayout>
<!-- create a Linear Layout with horizontal orientation and weightSum property -->
<LinearLayout
android:id="#+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<!-- place one TextView and one EditText inside layout using weight property -->
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Address"
android:textColor="#color/blackColor" />
</LinearLayout>
</ScrollView>
</LinearLayout>

ScrollView very slow while Scrolling

I have ScrollView which looks like the following:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.onecode.humam.kh.Main2Activity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:fillViewport="true"
android:id="#+id/sc">
<RelativeLayout
android:id="#+id/partner_detail_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<view
android:id="#+id/recycler_view"
android:layout_below="#id/recycler_view"
android:layout_width="fill_parent"
android:layout_height="match_parent"
class="android.support.v7.widget.RecyclerView"/>
<ImageButton
android:id="#+id/button_next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:minHeight="25dp"
android:minWidth="70dp"
android:src="#android:drawable/ic_media_next"
android:onClick="x"
android:visibility="invisible"/>
</RelativeLayout>
</ScrollView>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pb"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="false" />
</RelativeLayout>
It's very slow when scrolling, does anyone know a solution on how to solve it?
What causes the lag in scrolling is that you have nested scrollable views.
You cannot have RecyclerView inside a ScrollView.
[EDIT]
If you really need nested scrollable views that, consider using NestedSrcollView
Try not to use RecyclerView inside ScrollView

A button wont show up

I have this xml:
<RelativeLayout 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:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gelasoft.answeringball.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/sphereIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/magicBallDescr"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sphereIcon"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/launch_codes"
android:layout_width="match_parent"
android:layout_height="106dp"
android:hint="#string/textHint"
android:inputType="text" />
<Button
android:id="#+id/btnAsk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btnAskQ"
/>
</LinearLayout>
</RelativeLayout>
Everything is perfect, but the button won't show up.
What am I missing here? I know that is something basic even for a beginner, but I find it hard to understand as a newbie in the xml world.
Your inner LinearLayout has the orientation 'horizontal'. I guess your view can't show the TextView, EditText and Button in one row. There isn't that much space.

scrollview, tablelayout and linearlayout doesnt work together in android

I have a layout xml file which crashes and couldnt understand why. here is my xml, but I am not posting the entire xml.
<LinearLayout
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:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:background="#drawable/gradient"
android:gravity="center">
.......
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/sw_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#EEEEEE" >
<TableRow
android:id="#+id/tableRowDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
..........
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/btn_date"
android:layout_height="35dp"
android:padding="5dp"
android:drawableLeft="#drawable/calendar"
android:text="Tarih" />
<Button
android:id="#+id/btn_converter"
android:layout_height="35dp"
android:padding="5dp"
android:drawableLeft="#drawable/calendar"
android:text="Hesap" />
</LinearLayout>
</ScrollView>
</LinearLayout>
If I remove the last linearlayout. it works perfectly but I need to add button at the bottom of the screen. How can I add the buttons? I want to add the buttons and center them horizontally
Scrollview will allow only one child, so you have to kept that TableLayout and LinearLayout in one more separate layout as child of ScrollView.
Like this
<ScrollView
<LinearLayout
<TableLayout />
<LinearLayout />
</LinearLayout>
</ScrollView>

Categories

Resources