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);
Related
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.
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.
I have been working on a layout in which I have to animate the list view in the parent view fragment. When I animate the list view I just have to use the translate animation due to which listview height remain the same and it show an empty area below it if I translate it toward screen top or bottom. Here is the animation code I am using. animate(mLvLiveFeed).setDuration(500).x(0).y(0));
It nineold animation library for animation.
What i want is to make the list view height more than parent height so its does not show empty area with it. How can I give listview more height than Match Parent.
Here is the picture of what I want.
Finally I give my view more height than Match Parent of the parent view By giving the required layout bottom margin in -ve value.
I have a RelativeLayout (that I am not married to) and two TextViews. I would like the bottom of the first TextView to line up with the center of it's parent, and the top of the second TextView to line up with the center of it's parent.
I put a green line in the picture above at the (vertical) center of the parent. The TextViews are currently in exactly the right spot, but I did this as I will describe below with margin's and aligning them to the parent and knowing the parent's size.
For various reasons I can't use just one TextView and center it. Also I don't know the height of the parent, so I can't align each to the top/bottom of the parent and margin them down/up to center them.
I also tried having another View that takes up half the height and be invisible, but layout_height doesn't take percents (or at least it gave me errors).
Any ideas how I can accomplish this?
Should I just put the two TextViews inside another layout and have that center itself?
Create a 0-height view that is centered in its parent and serves as an anchor. (Use the attribute android:layout_centerVertical="true" for this.) Position one text view above the anchor and the other below the anchor.
I've solved this problem before by using a vertical LinearLayout with the two TextViews inside it.
ps.: the Android Design Guidelines http://developer.android.com/design/patterns/pure-android.html specific points out to Don't use right-pointing carets on line items. It might not be a line item that you're doing, but in case it is, just to point it out.
I have an edit text whose height I am setting as wrap_content.
I have some layout below the edit text.
when ever i write data in it and press enter to go to new line because of wrap content the size of edit text increases and its height increases downwards.Also the layout below is pushed down.
I want the layout below not to be pushed down but the edit text should overlap the layout below. Can this be done?
If you want overlapping layouts you need to use something like a RelativeLayout, if you were to only use a LinearLayout all the layouts will be placed in order.
If you use a Relative Layout you could define that all the TextViews start in the top left but the second one could have a margin so it is offset from the top. Then when your top textbox expands it would overlap the other (assuming the first TextView is drawn afterward , to do this list it in the XML after the other)