ListView inside two LinearLayout and scrolling issue - android

I have this structure
<LinearLayout
android:id="#+id/1">
<LinearLayout
android:id="#+id/2" />
<LinearLayout
android:id="#+id/3">
<ListView
android:id="#android:id/list" />
</LinearLayout>
</LinearLayout>
And I populate the ListView with some data. Well my problem is that the ListView becomes scrollable (while the LinearLayout number 1 fits the screen without scrolling), but what I want is this View to become full height and that the LinearLayout with id=1 becomes scrollable.
Inside ListView number 2 I have some TextViews, etc.

ListView's are scrollable by default if when the content is more than its display area.
However, LinearLayout would need to have a ScrollView in order to scroll.
Red Alert - You cannot use ScrollView and ListView together. You will end up seeing un-expected.
Alternate Solution: Prioritize what is more important to you, if scrolling the entire screen then add a ScrollView (provided your ListView items are static) else I will to wait to hear a good solution on this one. :)

Add ScrollView for ListView.
ScrollView will work for only one child view, so you have to add like this
LinearLayout
LinearLayout
LinearLayout
ScrollView
ListView...../ListView
/ScrollView
/LinearLayout

Related

How to design 2 different layers in one activity

I'm new at Android programming and couldn't solve how to do a design problem.
I have an activity with constraint layout, and it is full of textviews and buttons.
I want to put 6 imageview tool top of to the this activity. And there should be 3 of them should be seen on the screen, other ones should be scrolls to the right side. Like the screenshot I'm uploading.
Only these 6 imageviews should be scrolling horizontally.
I don't know what to do, which tools should I use. How can I do that design?
Thanks.
For the 6 ImageViews scrolling horizontally, I would advise you make use of a RecyclerView which is very powerful in dealing with showing data in a list especially for very large data sets. You can check out how to work with a RecyclerView here.
Since you only have 6 ImageViews, you can make use of a HorizontalScrollView, a LinearLayout with orientation set to horizontal i.e android:orientation="horizontal" and then place your ImageViews inside the LinearLayout.
<HorizontalScrollView ...........>
<LinearLayout ......>
<ImageView......./>
<ImageView......./>
<ImageView......./>
<ImageView......./>
<ImageView......./>
<ImageView......./>
</LinearLayout>
</HorizontalScrollView>
The easiest solution here is a HorizontalScrollView.
As HorizontalScrollView only accepts one child, wrap your ImageViews in another layout, e.g. LinearLayout or ConstraintLayout, then put that in a HorizontalScrollView.
You can use horizontal recyclerview for imageviews and below horizontal recyclerview, you can use linear layout for textviews inside scroll view.
<ScrollView>
<LinearLayout>
<RecyclerView>
</RecyclerView>
<LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>

ScrollView not scrolling which has ListView inside (listview is scrolling)

I have a ListView inside ScrollView, the ListView works fine (it scrolls) but the ScrollView is not scrolling
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I want the full page to scroll (The textview should also scroll)
TextView
ListView
item1
item2
.
.
The full page must scroll along with the textview!
This will never work because your listView is effectively a scrollView, so you have a scrollView within a scrollView. Is the textView of fixed size or can it be a large amount of text? I would consider some redesign here as this is not a very good way to design a UI.
If the textView is big which means you only see a small bit of the list then you should be able to scroll by touching the textView only, but i would make the scrollView the Parent and remove the first LinearLayout, it is not needed
Instead of ListView switch to RecyclerView and inside your MainActivity.java in onCreate() do this recyclerView.setNestedScrollingEnabled(false);
You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.
The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.

How to make ListView and ImageView scrollable into LinearLayout?

I have LinearLayout compoment which contains ListView and ImageView.
According to the documentation layout area should be scrollable if element's height is bigger than LinearLayout's area.
But scroll is not available.
I can scroll only when listview contains a lot of elements. But in this case ImageView component is not visible at all.
Using ScrollView is not allowed because ListView has his own Scroll element.
Could you please advice what should I do?
Here is part of my xml file:
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/top_header"
android:layout_above="#id/bottom_menu"
android:layout_weight="1"
android:id="#+id/sub_content_view">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/eventsListView"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="260dp"
android:id="#+id/empty_area"
android:adjustViewBounds="true"
android:background="#drawable/empty_area"
android:contentDescription="#string/contentDescription"/>
</LinearLayout>
If you ListView is not empty, you may register the ImageView as list footer, it then scrolls with the ListView. However, footers are not displayed if the list is empty, so you'd have to add the ImageView to the empty view as well.
First 2 comments than a possible solution
(1) ListView won't be scrollable when there's nothing in it. Instead it will display an empty view which you can set either programaticlly or with XML (usualy a text view that says something to the extent of "your list is empty").
(2) Your ImageView is set for a fixed height while your ListView is set to wrap_content. As a result, when your ListView contains enough elements that is is larger than the area of the screen it will push your ImageView out of view. As I have yet to see a LinearLayout or RelativeLayout suddnely become scrollable if there's more stuff on the screen than there is space (I just tried to make it happen) I'm not surprised by what you're describing.
You're right that using a ScrollView with a ListView as a child is disfavored. What I would try is - instead of putting both the ListView and ImageView in the same LinearLayout - separate the two. I don't know the rest of your XML but if you moved the ImageView out of that layout element you should be able to position it below the part containing the ListView such that - regardless of the size of the list - the ImageView will remain in the same place.
The suggestion above to use a footer may work, however, a footer is pinned to the bottom of the list. That means, if your list is really long, there will be a point at which the footer is not visible unless you scroll down far enough that it is exposed. If you want the image to remain visible all the time, e.g. have the list scroll behind the image, then you need to use a different approach.

insert more than 2 gridview in a linearlayout

im trying to insert 6 gridviews with 6 imageview above each of them ! the problem im facing is that i cant scroll my page to see all my girdviews the first gridview seems fine as it wraps all its contents and set the height to it the second gridview shows only first row and make it scrollable but i can't see my other gridviews i want a layout like
--ImageView--
--GridView1--
--ImageView--
--GridView2--
--ImageView--
--GridView3--
and so on
<LinearLayout>
<ImageView />
<GridView />
--
--
<GridView6 />
</LinearLayout>
i have tried using scrollview and all but couldn't get the results !
Thanks
Hee is how you can do this. Check the answer here. It is an expandable height GridView, which you will want to import / create in your project. What that basically means is that as more items are added to the GridView, it will just expand its height, as opposed to keeping its height set and using scrolling. This is exactly what you want.
Once you have the ExpandableHeightGridView in your project, go to your XML layout where you want the GridView to be. You can then do something like this (paraphrasing):
<ScrollView ...>
<RelativeLayout ...>
<com.example.ExpandableHeightGridView ... />
<other view items />
</RelativeLayout>
</ScrollView>
Then, in your activity where you set the GridView's adapter, you want to make sure you set it to expand. So:
ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId);
gridView.setAdapter(yourAdapter);
gridView.setExpanded(true);
The reason you want this expandable GridView is because, the fact that a standard GridView doesn't expand is what causes it to scroll. It sticks to a certain height, and then as more items fill it past its view bounds, it becomes scrollable. Now, with this, your GridView will always expand its height to fit the content within it, thus never allowing it to enter its scrolling mode. This enables you to use it inside of the ScrollView and use other view elements above or below it within the ScrollView, and have them all scroll.
This should give you the result you're looking for. Let me know if you have any questions.

How can I make the footer of a ListView stretch to fill the remaining space?

I have a ListView which can contain any number of items. When it uses less than the whole height of the screen I want to show a footer which fills the remaining space.
The footer is just a tiled image. I can't just set it as the background of the ListView or its container, because the list items are partially transparent and it must be the main background and not the footer visible through them.
Adding the footer view works fine if the footer is of a fixed height, but if the fixed height is more than the remaining height then the ListView scrolls unnecessarily.
Making the footer view fill_parent seems to have no effect, it's just invisible.
Putting the footer view into the parent layout also has no effect, because the ListView seems to fill the whole height unless it's given a fixed layout_height value.
Is this possible?
Try setting the layout weight to one, or check out trying a merge view:
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html
Put the ListView inside of another layout set the expand to fill the screen.
A LinearLayout works best for this.
Something like this:
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent" >
<ListView android:layout_height="match_parent"
android:layout_width="match_parent" >
...
</ListView>
</LinearLayout>
This should fill the LinearLayout to the bottom of the screen. You can use this behavior to control how your View looks.
Hope this helps!
Try this and tell me if it works:
Put the two in one parent layout (for example: Linear Layout)
For ListView: android:layout_hight="wrap_content"
For footer: android:layout_hight="fill_parent"

Categories

Resources