I was having the same problem discussed over here Align Button to Bottom in ScrollView but with native Android.
Imagine you have following layout:
As the content might not fit on smaller screens/configurations you wrap your content in a scrollview.
Plus: To avoid that users miss critical content which is below the fold you want that the CTA is positioned below the content.
Screen
Layout
ScrollView: - ConstraintLayout -- //content -- Button
But now the CTA is not aligned to the bottom of the screen anymore.
For taller devices where no scrolling is necessary it would look like this:
How can we get the button still be positioned on the bottom when the content is not scrolling?
It is pretty simple to achieve that:
Make your ScrollView grow the whole screen by using android:fillViewport="true"
This will stretch the child (ConstraintLayout) to fill the whole screen if it is smaller.
Now that the child of the scrollview is full-height, you can align the CTA to the bottom of it's parent
Screen
Layout
ScrollView: - ConstraintLayout -- //content -- Button(toBottomOf=parent)
And if the content scrolls, the button will still align below it.
Related
I want to have a recycler view with list of things of unknown number and a button below the RecyclerView. I want the height of the whole ConstraintLayout be flexible with the height of my RecyclerView, i.e. if the list is short, wrap content and if the list is long, just fill up the whole screen.
Now no matter how long the list is, the ConstraintLayout will fill up the whole screen . I have already had everything wrap_content but there is still blank space at bottom.I want my button always appear on the bottom. Does anyone know how to deal with this problem?
I guess you are trying to achieve this:
Your constraint layout will be the parent (root) with match parent for both width and height so it will take the whole screen.
Then put the button, the left and right sides should connect to the constraint layout sides, and the bottom of the button should connect with the bottom of the layout too (3 constraints only: right to right, left to left and bottom to bottom). Wrap content for both width and height.
The recyclerView will connect almost the same, top to top, left to left and right to right with the constraintLayout, but the bottom should connect with the top of your button. Finally select match parent for both width and weight.
On this way, your button will be always at the bottom of the screen and the recycler view height will go from top of the screen to top of your button.
If you want to change the constraints of your layout accordingly to some conditions (like reach specific number of items on your list) you need to do it programmatically.
Select by default your recycler view height as wrap_content, and on code write a method which observe every time an item is added or removed from your list. Create a conditional which says if the recyclerView.count( )== number and if it is true change the constraintLayout and child's constraints with something like this (example):
ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.recycler,ConstraintSet.BOTTOM, R.id.button,ConstraintSet.TOP,0);
constraintSet.applyTo(constraintLayout);
I have multiple TextViews inside a horizontal LinearLayout and i need the middle text view to have ellipse=middle so that when the middle text is long enough, it pushes on both sides but the other views don't go out of bounds, but instead the middle TextView shows the '..."
Here's how it should look.
Setting the items normally, wrap_content for all in a horizontal LinearLayout will make the at ASAP text be pushed outside of the screen on Android (the above screens are from the iOS app).
Any ideas on how to accomplish this? Perhaps with a ConstraintLayout somehow ?!
Yes, i would recommend a ConstraintLayout. Top item to the top of the view, bottom item to thr bottom of the view then the middle item attached to these two views. You could also use barriers.
Here is the design for my screen:
The Green border indicates the visible screen.
At first the user would only be able to see relative View 1 and the Scrollview would not do anything.
After the user presses the button to show the 2nd relative view, the 2nd relative goes from invisible to visible and the view is now scrollable (because the screen is extended)
I have nested my layouts as followed
<RelativeLayout>
<ScrollView>
<LinearLayout>
<RelativeLayout1>
<RelativeLayout2>
</ Closing for each layout>
My problem is that my either my Relative View 1 will not stretch to fit the screen or if it does stretch to fit the screen, I will not be able to scroll down to relative view two.
I've tried to play around with match_parent,fill_parent,wrap_content on all different levels but haven't found a solution to my problem. Any suggestions on how I would create fix this?
Your design looks like you wanted to implement something expandable view. Try this Link
I think that this is a simple question but I can't figure it out, I have a Scroll View with some text inside and it works perfectly, it only enables scrolling when the content doesn't fit into the screen. The thing is that I want to scroll the content no matter if it fits or not until the last line of the text reach the top of the view leaving blank space below and obviously "hiding" the content above it. I don't know if I'm explaining myself very well, thanks in advance!
Add a dummy view with height equal to the height of device's screen beneath your textview in scrollView.
This is my problem: I have a (horizontally) very large View and, of course, I have a scroll to move it side by side. I want to put a TextView in middle, but I want it so, that when I scroll horizontally and the View moves this TextView stays in the center of the screen.
If I use android:gravity="center" or something like this, as the view is very large, I will NOT see the TextView in the center unless I am in the center of the view (Not in the beginning, and not in the end) but I need that the TextView is in the center during the whole scrolling.
I think that you can achieve that by positioning the element in the middle by setting its x-origin (with left-padding or left-margin?) to (scrollview.width/2 - textview.width/2), then add the scrollview.offset.x whenever the scrollview scrolls (add a listener to get this) so it maintains still at the relative center.
However the simplest thing may be to place a FrameLayout which contains the ScrollView and above it the TextView, which you could then center with the gravity property.
Presumably your content is all inside a ScrollView. You will need your TextView to be outside of this scroll view: you can then use the technique described in "Android overlay a view ontop of everything?" to place the TextView where you want it.