ScrollView not scrolling down - java

I am building an application and the ScrollView function is not scrolling down (not working), it only views a number of buttons and the rest of the buttons are not there!
It should view the rest of the buttons when I scroll down (buttons are retrieved from a loop)! but unfortunately it doesn't work!
Here is the 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:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".viewdevices">
<TextView
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="300dp"
android:text="#string/username"
android:textAlignment="center"
android:textSize="30dp"
android:textStyle="bold"
android:typeface="serif"
/>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:fillViewport="true"
android:fitsSystemWindows="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/username"
>
<LinearLayout
android:id="#+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<Button
android:id="#+id/devicename"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="" />
</LinearLayout>
</ScrollView>
</LinearLayout>
can anyone figure what I did wrong?

The issue with the ScrollView not scrolling might be due to the height of the ScrollView and the LinearLayout inside it. The height of the ScrollView is set to "match_parent" which means it will take up the full height of the parent layout. The height of the LinearLayout inside the ScrollView is set to "wrap_content" which means it will only be as tall as its contents.
If the number of buttons inside the LinearLayout is more than what can be displayed on the screen, the ScrollView will not be able to scroll as its height is already taking up the full height of the parent layout.
To resolve this, you can change the height of the LinearLayout inside the ScrollView to "match_parent". This will make the height of the LinearLayout expand to accommodate all its contents and the ScrollView will be able to scroll.
Here's the modified code:
<LinearLayout
android:id="#+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">

Related

How to get a whole screen in an activity to scroll when a scrollable recyclerView imakes up only half of the phone screen

I want my whole screen to scroll but only my recycler will scroll at the moment. This occurs despite all elements being placed in a linear layout inside a Scroll View.
Any ideas where the error is? Here's the relevant code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".NoodleDetailActivity">
<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">
<ImageView
android:id="#+id/noodleImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="false"
android:scaleType="centerCrop"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_bottom"
android:fontFamily="sans-serif-smallcaps"
android:hapticFeedbackEnabled="false"
android:padding="30dp"
android:text="TextView"
android:textSize="18sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/extras_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="#F2F2F2"
android:padding="0dp" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
This is what it looks visually.
demo appearance
Found out: it seems best practice is to set the recyclerView to be the whole layout and make it contain multiple view types.
I won't be using a nested scroll view.

GridView item height is mismatched with other

I am using a grid view. It shows issue in some devices. One item of the grid view looks shorter in height than others.
It looks like this in some devices, and in some its fine.
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.fueloneApp.MainDriver"
tools:showIn="#layout/app_bar_main"
android:weightSum="8">
<TextView
android:id="#+id/main_welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/dimen_20dp"
android:text="#string/welcome"
android:textColor="#color/colorPrimary"
android:textStyle="bold" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridviewMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/dimen_40dp"
android:layout_marginLeft="#dimen/dimen_40dp"
android:layout_marginRight="#dimen/dimen_40dp"
android:columnWidth="80dp"
android:horizontalSpacing="#dimen/dimen_10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"></GridView>
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:text="For Any Queries Related To Mobile App Click On Service Request"
android:textAlignment="center" />
</RelativeLayout>
Item layout
EDIT : Added item layout of the grid view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/single_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary1"
android:orientation="vertical">
<ImageView
android:id="#+id/gris_img"
android:layout_gravity="center"
android:layout_width="80dp"
android:scaleType="fitXY"
android:layout_height="100dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/grid_cat"
android:textSize="15sp"
android:textColor="#color/divider_color"
android:gravity="center"/>
</LinearLayout>
How can I manage the height of each item? What is the solution for this? Please help. thank you...
Gridview is having a tendency to adjust the height of all element with the tallest element in all. Hence this behaviour you are seeing.You can use StaggeredGridView.
i think your grid layout cell height is wrap_content. this problem same occur with me . change your layout height wrap_content to match_parent.
android:layout_height="match_parent"
for better solution please update your grid view item layout cell here.
Add Textview property :
android:maxLines="1"
android:singleLine="true"
if text not visible single line, you need change textview to autosizing-textview
enter link description here

How to change the height dynamically according to screen size?

I have a RelativeLayout with a ViewPager and another LinearLayout. I have set the layout width of the ViewPager as match_parent and have given the layout height a fixed value of 250dp.
However, when changing the screen sizes bigger and bigger, the height of the ViewPager gets smaller.
How to make the height change according to screen size?
If I set the height to wrap_content it almost takes up the whole screen except for a smaller bit reserved for the other layout.
The code is posted below.
<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"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="#+id/SliderDots"
android:layout_below="#+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Try using this
If you want to make your screen design for multiple devices ,
You can use WeightSum with LinearLayout as Container to avoid this like below :-
<LinearLayout 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"
android:orientation="vertical"
android:weightSum="10"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.5"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="#+id/SliderDots"
android:layout_below="#+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.5"
/>
</LinearLayout>
for reference :-https://developer.android.com/reference/android/widget/LinearLayout.html
You could also use modern new ConstraintLayout https://developer.android.com/training/constraint-layout/index.html
to set percentage of viewpager view relative to screen height (33% in example below)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/SliderDots"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1" />
<LinearLayout
android:id="#+id/SliderDots"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="#+id/viewPager"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/viewPager"
app:layout_constraintVertical_weight="2" />
</android.support.constraint.ConstraintLayout>
To strictly answer your question :
How to make the height change according to screen size?
You can externalize your height value to the dimens.xml in the default values folder, like
<dimen name="view_pager_height">250dp</dimen>
and in the values-sw720dp folder you can put
<dimen name="view_pager_height">300dp</dimen>
Then use it in the layout like this:
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="#dimen/view_pager_height"
android:layout_marginBottom="8dp"/>
Read more about that here: Using new size qualifiers.
Besides that, you can use a LinearLayout as a parent, and set weights to each child. (RelativeLayouts as parents are quite expensive, as they double measure their children)
Set viewpager to be above sliderdots.
<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"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/SliderDots"/>
<LinearLayout android:id="#+id/SliderDots"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>

Scrolling in RelativeLayout

I have textview and listview in the RelativeLayout. How can I do scroll this textview when I scrolling listview? I want that textview does not to hang out from the top when I'm scrolling listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:padding="10dp"
android:paddingBottom="0dp"
android:id="#+id/info_presents_surveys"
android:text="#string/info_presents_surveys"
android:textColor="#color/empty_color"
android:background="#color/color_gray"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/info_presents_surveys">
<ListView
android:id="#+id/list_view_surveys"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/members_speakers.divider"
android:dividerHeight="1dp" />
</android.support.v4.widget.SwipeRefreshLayout>
The TextView is not within a scrolling component, it's within the parent layout RelativeLayout which does not scroll.
In order for it to scroll, you'll have to put it within a ScrollView (or similar scrolling component).
So it should be:
<ScrollView>
<RelativeLayout>
<TextView>
<SwipeRefreshLayout>
<ListView/>
</SwipeRefreshLayout>
</RelativeLayout>
</ScrollView>

Android Studio - weightSum

<?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="match_parent"
android:weightSum="10"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="2">
<TextView
android:id="#+id/centerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:textSize="28sp"
android:text="HALLO ALLE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/centerText"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:layout_weight="8"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Btn 1"
android:layout_weight="2"/>
</LinearLayout>
Hi everybody, this is my code and I have a question about this I set a weightSum in the first LinearLayout I set the weightSum of 10, which the RelativeLayout has 2/10 and the other one (LinearLayout) has 8/10, as I saw in the preview of my IDE (Android Studio), the RelativeLayout is the bigger one here's my question:
The layout_weight with less value will be the bigger one?
No, the larger value for layout_weight should be the bigger one.
When using layout_weight you should set either the height or width to 0dp; in a vertical layout you're dealing with height and in a horizontal one it's width.
So in your case your root layout is a vertical LinearLayout. Within that, your relative layout with a weight of '2' should have android:layout_height="0dp" and the same for your LinearLayout with a weight of '8'.
From the documentation:
A larger weight value allows it to expand to fill any remaining space in the parent view
The key part here is "remaining space"
This means that if the RelativeLayout height is bigger them half of its parent height, it will be bigger then the LinearLayout no matter to what value you set its android:layout_weight property
I believe you are looking for something like this: (top takes up 20% bottom takes up 80%)
<?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="match_parent"
android:weightSum="5"
android:orientation="vertical">
<!-- Specify height as 0dp-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#000000"
android:layout_weight="1">
<TextView
android:id="#+id/centerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:textSize="28sp"
android:text="HALLO ALLE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/centerText"
android:text="Button top"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<!-- Specify height as 0dp -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Btn 1"/>
</LinearLayout>
</LinearLayout>
When dealing with layout weights you supply 0dp to the layout width or height of the weighted item when parents orientation is horizontal or vertical.
When ever using weights it is better to use corresponding one to 0dp and then use the layout_weight....like
In Case of android:orientation="horizontal"
Use the following code
android:layout_width="match_parent"
android:layout_height="0dp"
In Case of android:orientation="vertical"
Use the following code
android:layout_width="0dp"
android:layout_height="match_parent"

Categories

Resources