Change ScrollView height dynamically on child view show/hide - android

In one of the apps I am developing, I have some data that I can display as list, on map or as analytics. User can select one of those by button click. On the screen, I have a header and below it I have a ScrollView. Within the ScrollView, I have containers for ListView, Map and Analytics. I show/hide these views based on user's action. The ListView is within ScrollView and hence I have made the ListView un-scrollable and made the expanded its height so that all items can be displayed.
Now my problem is this: Because the ListView is expanded, the ScrollView height becomes really big. Now even when I hide the ListView and show the Map instead, the ScrollView height remains the same. So even if the map is displayed in a small area, I can keep on scrolling.
Is there a way to dynamically change the height of the scrollable section.

I was able to do this by dynamically adding/removing views. So, when I was to show the Map, I remove the ListView from the parent container and insert the Map container. This way, the height is re-calculated automatically.

Related

RecyclerView change items layout during scroll

I am having a requirement to implement a recyclerView with items that have a space between them. While scrolling down, the space should be smaller and smaller.
Using a collapsing layout etc so I am able to get a floating number of a position the only issue is how to change layout of the items programatically after they are already visible on the screen

ListView Only add enough items to fill screen

I have a listView that is populate via a RSS feed. The height of each item on the ListView can vary depending on the length on the content. Is there a way to only add enough items to the listView to fill the screen (the listView has a fixed height).
For example if each item only has one line of text I can fit 7, but if they have two lines of text I can only fit 5. I want to be able to programmatically decide when to stop adding items. I don't want the ListView to have to scroll.
EDIT
Let me rephrase. I don't want to just solve it but scrolling down. There may at some point in the development be content that gets added below the screen and requires scrolling but even in that case I don't want half an item showing at the bottom. Basically I don't want broken/half items showing at the bottom, if it's on the screen it should be the whole item that is showing. If not it should be below the bottom of screen.
Try using a normal Linear Layout and when adding the inflated row, override the onMeasure to know if it fits on the screen.

Android check either scrollview is scrollable or not

In my app I have a scroll view with n number of data to be listed out. There are two buttons one is named as UP placed above scroll view and the other is DOWN placed below scroll view.
Using the UP and DOWN buttons the list of views can be scrolled.
When the scroll bar is in top the Up button will be invisible and when the scroll bar reaches the bottom the DOWN button will become invisible, I have written logic for this using the getScrollX() method.
Now my problem is when there is very few data for example 3, the scroll bar will not be visible and the layout cannot be scrolled, in such a case both the UP and DOWN buttons need to be in invisible. How to do this, please suggest me a way?
You can try and use a ViewTreeObserver to check the dimension of the View inside your ScrollView. If the dimension exceeds a certain limit (such as the screen size), the ScrollView will be scrollable. See this preview SO answer for more details. Hope that helps!

ScrollView that does not render views that aren't visible

I am attempting to improve the amount time it takes to fill the items into the ScrollView. If I hard code the height (not in pixels of course) of every RelativeLayout in the LinearLayout in the other LinearLayout in the ScrollView... can I get ScrollView to display the visible ones first and then when they are displayed to load up the rest later? The ScrollView should start off scrolled to a specific location.. not just the top of the screen. Now if we have to load from the top that is OK because there is about one third above the visible area and the rest below.
You could use ViewStub to inflate extra items only when you need it, but it sounds like you are describing what ListView does already.

How to display a custom view in Android?

I am generating a custom View that contains a number of drawables that are added to the View dynamically. This means the View's size could be anything, and is likely to stretch off the screen. Where it does stretch off the screen I want scrolling to be enabled.
So far I have tried:
adding the custom view directly to my Activity - this displays the drawables ok, but with no scrolling
adding the custom view as a child to a ScrollView and setting the ScrollView as the content in the Activity - this doesn't display anything.
How do I generate a custom view of arbitrary size, display it and have scrolling where it is too big for the screen?
Adding it to a ScrollView should be fine. Remember:
A ScrollView is a FrameLayout, meaning
you should place one child in it
containing the entire contents to
scroll; this child may itself be a
layout manager with a complex
hierarchy of objects. A child that is
often used is a LinearLayout in a
vertical orientation, presenting a
vertical array of top-level items that
the user can scroll through.
To make sure your "custom view" is working fine, first try to add a LinearLayout to the ScrollView and then add drawables to the LinearLayout.

Categories

Resources