I want the recyclerview to adjust it's height so that the items don't get hidden behind the pricebar ( see image )
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutMainContainer"
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="com.mridulahuja.groceryapp.activities.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#null" />
<RelativeLayout
android:id="#+id/layoutPricebar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#454560"
android:layout_alignParentBottom="true"
android:visibility="gone">
...
...
</RelativeLayout>
</RelativeLayout>
Code I'm using to resize recyclerview:
ViewTreeObserver pricebarvto = layoutPricebar.getViewTreeObserver();
pricebarvto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
layoutPricebar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
layoutPricebar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
pricebarHeight = layoutPricebar.getMeasuredHeight();
}
});
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) recyclerView.getLayoutParams();
params.height = relativeLayoutContainerHeight - pricebarHeight;
recyclerView.setLayoutParams(params);
where relativeLayoutContainerHeight is the height of the root RelativeLayout
But the height isn't changing. It's changing when I hard-code a big value
to pricebarHeight such as 160. And I've given pricebar the height of just 50dp.
Also I'm using this animation to display the pricebar:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromYDelta="180%"
android:toYDelta="0%">
</translate>
Use linear layout with vertical orientation instead of relative layout and give recycler view
android:layout_weight="1"
If you use RelativeLayout then you should use android:layout_marginBottom
Specifies extra space on the bottom side of this view. This space is
outside this view's bounds. Margin values should be positive.
XML
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
................
android:layout_marginBottom="50dp" />
If you want LinearLayout Then follow android:weightSum Logic .
Defines the maximum weight sum. If unspecified, the sum is computed by
adding the layout_weight of all of the children. This can be used for
instance to give a single child 50% of the total available space by
giving it a layout_weight of 0.5 and setting the weightSum to 1.0.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutMainContainer"
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="com.mridulahuja.groceryapp.activities.MainActivity">
<RelativeLayout
android:id="#+id/layoutPricebar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#454560"
android:layout_alignParentBottom="true"
android:visibility="gone">
...
...
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_above = "#id/layoutPricebar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#null" />
</RelativeLayout>
Try to put the layout pricebar with id : layoutPricebar below the recycler_view by using android:layout_below: "#+id/recycler_view"
<RelativeLayout
android:id="#+id/layoutPricebar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#454560"
android:layout_below="#+id/recycler_view"
android:layout_alignParentBottom="true"
android:visibility="gone">
if your pricebar height is 50dp make RecyclerView android:layout_marginBottom="50dp" this will solve your issue no need of java code for resizing the Recyclerview
Related
I am using the RecyclerView to inflate a Linear Vertical listViews, However, although the width of a child is set to match_parent, RecyclerView wraps that at runtime.
Here is a screenshot of the problem:
item_recyclerView
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tag_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:maxLines="2"/>
<TextView
android:id="#+id/tag_role"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/user_pic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:rotation="-45"
android:scaleType="centerCrop"
android:src="#drawable/ic_user"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.circleImageView" />
</LinearLayout>
When inflating a layout file, the layout_ attributes on the root view will be ignored unless you specify a parent ViewGroup in the inflate() call. You are not currently doing so.
In your onCreateViewHolder() method, replace this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater);
with this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);
Note also that you probably don't want to be using match_parent for the height of your RecyclerView's items:
android:layout_width="match_parent"
android:layout_height="match_parent"
This will give the appearance that only a single item is visible in your RecyclerView, since each item will be the full height of the screen. This is currently not a problem for the exact same reason that the match_parent width is being ignored.
give for your parent linearlayout weightSum 5
give for imageView weight 1 and do his width 0
and for child linearlayout weight 4
enter code here
I have a ConstraintLayout in Android studio, I want this to have the height be 60% of the screen size for whatever device it is running on, I am wondering how I can do this in xml?
Currently I have:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/recycler_view_container"
android:layout_width="match_parent"
android:layout_height="372dp" // BAD!! HARDCODED
android:background="#color/white">
</androidx.constraintlayout.widget.ConstraintLayout>
How could I do this? As you see, the layout_height is hardcoded right now.
Here is my answer. I think it should clear your requirements.
<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"
android:background="#color/colorWhite">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorRed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_percent="0.50">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You can change height percentage according to your needs. Thanks.
You can't do that directly, however you can put a guideline within your ConstraintLayout fixed at a percentage of the height. Children views can then use that constraint to set their height.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guide"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcv"
android:layout_height="0dp"
android:layout_width="0dp"
app:layout_constraintBottom_toTopOf="#id/guide"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
I'm guessing your ConstraintLayout is meant to contain a RecyclerView?
You're going to want to use an unequal LayoutWeight.
LinearLayout supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. Set the child view layout_height to 0 and assign the screen proportion as desired to each child view.
This is how I would set up the XML for your specifications, notes included in the code.
<?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:orientation="vertical"> // Note: set orientation to vertical
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp" // Note: Set the layout_height to 0
android:layout_weight="6" // Note: Set weight to 6
android:background="#color/colorAccent"/>
// Framelayout need to fill remain space
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp" // Set the layout_height to 0
android:layout_weight="4"/> // set the weight to 4
</LinearLayout>
The constraint layout has a weight of 6 and FrameLayout has a weight of 4, the LinearLayout has a total weight of 10. 6/10 is 60%, the constraint layout will fill 60% of the screen.
The question has changed after I posted this answer, you can also equal distribution, which is the same as unequal assign the same weight value to each child view, and the screen will be split equally among the child views.
<?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:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorAccent"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
ConstraintLayout has a weight of 1 and FrameLayout has a weight of 1, total layout weight is 2. 1/2 is 50%, the ConstraintLayout will take up 50 of the screen.
dynamically increase and decrease the relative layout , i want to shrink the size of relative layout when recycler view popup from bottom of screen and after using it agian the screen will return into normal size(in recycler view im sung icons to put on relative view)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_weight="1">
////////tool bar
<include layout="#layout/toolbar" />
<RelativeLayout
android:id="#+id/rel1"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_below="#+id/toolbar"
android:layout_weight="1">
<com.cmlibrary.CircleMenu
android:id="#+id/circle_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_marginTop="180dp"
/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/rel1"
android:padding="5dp" />
</RelativeLayout>
Better way is to show and hide the visibility of views as required. But if you want to increase and decrease the size then below code will help you.
private void layoutParams() {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
/* When recyclerView shows*/
layoutParams.height = layoutParams.height - params.height;
relativeLayout.setLayoutParams(layoutParams);
/* When recyclerView hidden*/
layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
relativeLayout.setLayoutParams(layoutParams);
}
Hope this will help.
I got a vertical LinearLayout, in which there's a button. I want to make this button 70% of the width of the LinearLayout and center it.
I've tried setting layout_weight to 0.7 while setting the layout_width to 0, but since it's a vertical LinearLayout, it doesn't affect the width, but can only affect the height.
How can I change the weight of the width in a vertical layout? Will I must add a nesting horizontal LinearLayout just for the button so I can set its weight?
Try using new PercentRelativeLayout, and you can achieve what you are trying without nested layouts.
include this in your dependencies :com.android.support:percent:25.1.0
And then in your code
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<Button
app:layout_widthPercent="70%"
android:layout_height="wrap_content"/>
...
</android.support.percent.PercentRelativeLayout>
linearLayout -> orientation = horizontal
button -> layout width = 0dp
-> layout height = wrap_content
-> layout weight = 1
Please check this out.
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/yourRequiredButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/container"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
Try this:
<LinearLayout>
<Space android:layout_weight=0.15/>
<Button android:layout_weight=0.7/>
<Space android:layout_weight=0.15/>
</LinearLayout>
https://developer.android.com/reference/android/widget/Space.html
I also suggest you put the values in dimens.xml file.
My view is like:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
/>
</LinearLayout>
It will give 50dp to TextView and rest of area to RecyclerView.
But if items in RecyclerView are less than, say 1 or 2, then it should shrink to the height of it's child views other wise behave like layout specifies. Can any one help??
I think the best option would be to change LinearLayout.LayoutParams of the root view dynamically. When the RecyclerViewhas a low amount of children, then set LinearLayout params to:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.setParams(params);
otherwise, leave as it is.
It is very easy to detect if RecyclerView should be wrapped. Obtain LinearLayout height:
int height = layout.getTop() - layout.getBottom()
If this height is lower than the expected maximum height (screen height or other predefined value).
A simple approach would be using RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/recyclerView" />
</RelativeLayout>