I have a ConstraintLayout that will accept a variable amount of ImageViews... anywhere from 1 to 12. I would like the ImageViews to adpat in size according to the amount of the items. E.g. 1 or 2 items may have Width: 110 Height: 140 whereas when there are 6 or more items Width: 50 Height: 60. In other words all the items should fit into a constrained area an adapt in size accordingly.
<android.support.constraint.ConstraintLayout
android:id="#+id/ingredient_CL"
android:layout_width="match_parent"
android:layout_height="140dp"
android:animateLayoutChanges="true"
android:background="#drawable/item_border">
<ImageView
android:id="#+id/ingredient_tool_0_iv"
android:layout_width="110dp"
android:layout_height="140dp"
android:layout_marginStart="8dp"
android:scaleType="centerInside"
android:visibility="#{step.quantity > 0 ? View.VISIBLE : View.GONE}" />
<ImageView
android:id="#+id/ingredient_tool_1_iv"
android:layout_width="110dp"
android:layout_height="140dp"
android:layout_marginStart="8dp"
android:scaleType="centerInside"
android:visibility="#{step.quantity > 1 ? View.VISIBLE : View.GONE}"/>
...
Any ideas?
If you have number of items that can vary based on some condition, you can use Staggered Grid Layout Manager to achieve what you want.
Staggered Grid View : It is basically an extension to Grid View but in this each Grid is of varying size(Height and width). Staggered Grid View shows asymmetric items in view.
RecyclerView supports StaggeredGridLayoutManager
A LayoutManager that lays out children in a staggered grid
formation. It supports horizontal & vertical layout as well as an
ability to layout children in reverse.
Refer below documentation links for understanding it better.
https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager
Android - Difference between Gridlayout and Staggered Gridlayout
Related
I am using recyclerview with gridlayout, getting some of the items with large height of the content. I want to set the same height of all items that will be equal to the largest item height or the equal height of the horizontal items without changing the layout.
XML -->
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/tvSubTitle"
style="#style/darkMedium"
android:text="Fabulous Foot "
android:fontFamily="#font/muli_regular"
android:layout_marginTop="#dimen/margin_5"
android:ellipsize="end"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="#+id/imgExperience"
app:layout_constraintTop_toBottomOf="#+id/tvTitle">
</TextView>
Its parent layout is constraint and there is one more textView below it.
It is looking like this.
[![It should be look like this][2]][2]
Want to make it like this.
[2]: https://i.stack.imgur.com/p7eE0.png
could you share your layout.xml file or UI/UX design here? I can just suggest you now without much information that you should design layout with maximum height and width of each view. Use SDP and SSP library to support multiple resolution and devices.
I have a gridLayout which looks like this:
Notice how the layoutmanager forces the column spacing between the items already. this is key. i have not yet added any item decorator for any spacing. this seems to be because i have constraint the recyclerview to both ends of the screen so its filling the items as much as it can. i want to add row spacing equal to this value so the grid looks evenly separated. but how can i know the column spacing ahead of time ? or even after the view is laid out i could add a decoration possibly.
here is my recyclerview:
<RecyclerView
android:id="#+id/rv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clipChildren="false"
android:clipToPadding="false"
android:splitMotionEvents="false"
tools:background="#color/green"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:spanCount="3"
tools:listitem="#layout/grid_item"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
please note that if i do not not constraint the recyclerview to left and right then i can use the decorations fine using wrap_content. how can i resolve this issue of unequal spacing ?
i figured this out and will give you some clues. gridlayoutmanager is scaling the items such that it will fit all items according to your span count. so imagine when gridlayoutmanager takes your item, it will says how can i stretch this item such that X number of them can fit on a row ?
the best approach i found is to use constraintLayout. such as:
app:layout_constraintDimensionRatio="1:1"
and/or depending your usecase for your content you can use this so it stretches with the container:
eg.
app:layout_constraintWidth_percent=".51"
app:layout_constraintHeight_percent=".63"
for me the percentage stuff i used for the imageView so it keeps the ratio when stretched.
and not give any hard dimensions. Hard dimensions are going to make the spacing not even as its forcing a certain size when gridlayoutmanager wants to strength to fit your item. instead let the system scale the items but maintain the aspect ratio.
all these examples are for your container. inside your container you can create content as you usually do but you might still need ayout_constraintWidth_percent so it stretches when the item stretches proportionally.
after you do all that your items will be tightly bunched together with no spaces. now use a itemdecorator to add the spacing and it should work.
I got this setup:
<ScrollView 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:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="#+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.35" />
...
</android.support.constraint.ConstraintLayout>
</ScrollView>
Now when the view is not full and has no need for a scroll bar - everything works as expected - the image is 35% in relation to the screen size. But as more content appears under the image, a need for scroll bar appears and the guideline's constraintGuide of 0.35 percent seems to be calculated off whole length of the screen (not physical), so the ImageView also becomes bigger as the view becomes "longer".
Is there a way to avoid this and always have x percent of physical screen size?
The guideline you have specified is placed at a percentage distance from the top of the ConstraintLayout. Unfortunately, for your application, the guideline is tied to the overall height of the view and not a percentage of the screen. So, if the ConstraintLayout is taller than the screen size allocated to it, you will see the shift. See documentation for Guideline.
Positioning a Guideline is possible in three different ways:
specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin)
specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end)
specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)
You can specify a static offset from the top of the layout in terms of dp, but this will not accommodate different screen sizes. I don't believe there is a solution just using XML.
You can, however, calculate the number of pixels in code and set the distance on a run-time basis. You would need to change the Guideline to one that is a fixed distance from the top of the layout, calculate the distance from the top, and call setGuidelineBegin to place the guideline.
setGuidelineBegin
void setGuidelineBegin (int guidelineID,
int margin)
Set the guideline's distance form the top or left edge.
I'm asking this (propably) pretty easy question since I can't figure it out by myself and googling in for more then an Hour doesn't give me proper results.
So the question is: How can I make my items(generally those are only 16 image buttons) so they can stay in scale (comparing to gridlayout) when my picture is really large (So in short way: I load 16xImages as src on buttonImages, each Image is 640x640, and I want to have those 16 buttons in grid layout scaled comparing to grid layout width/height and column/row count). I hope this is possible to write in XML since I don't want to refactor my java code :/
Ps. I'm newbie to Android programmig
Ps2.Here is my GridLayout with button example , but it's completly wrong :/
<GridLayout
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="4"
tools:context=".GridXMLActivity"
android:layout_below="#+id/Tx1"
android:layout_above="#+id/restart"
android:layout_marginBottom="50sp"
android:layout_marginTop="50sp"
>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:src="#drawable/clear"
android:id="#+id/But1"
android:scaleType="fitXY"
android:onClick="but1"
/>
[EDIT]
I've even made such layout , but there are some problems. 1st is that my grid layout doesn't scale at all!(So when I take screen with lower or higher resolution it won't fit). That's why I wanted to make this button some kind of resizeable (in this screen each button has width and height at 60sp which I know it shouldn't)
Another problem is that I'm working on button backgrounds and I wanted to make it src.
Genereally I want to achive same thing like on the screen , but making it other/more flexible way (So the resolution and orientation won't affect my gui and sizes of it's element). If you need whole XML file I'll post it here but it's a bit mess since I've copy-paste 16 times Image Button
If you want something to scale, keeping aspect ratio and you have one dimension set to match_parent, the other must be wrap_content.
So, presuming you want the button to always fit the width but stay at the correct aspect:
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
To make all items fit in grid rather than all items fit their images, you should use weight. Weight with grid layout requires the support library before API21
<GridLayout ...
android:columnCount="2">
<ImageButton
app:layout_gravity="fill"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
... />
...
I am working on a android project in which I am using Recyclerview withgridlayoutmanger and Items are Images. I want to display four items in single row so I have set spancount to 4 and a gap of 2dp is set between each item with Itemdecoration.
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getContext(),R.dimen.offset);
fRecyclerView.addItemDecoration(itemDecoration);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),4);
Each Item height and width are currently 90dp.
Item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imagevideo_layout"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/image_video_pic"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
/>
</RelativeLayout>
Problem is as I am fixing the height and width of item, in some devices due to the screen size differences there is more gap between each item.And in some devices items are overlapping because of small screen sizes.I need only 2dp and only 4 items in single row for all the screen sizes. Is there any way to do this? Is it possible to dynamically set the relativelayouts height and width depending on the screen size? I have searched in SO, questions are on setting the spancount dynamically by fixing the height and width of each item, but I need reverse of it.