Set dynamic width and height of item in recyclerview - android

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.

Related

How to set the all items size according to the largest item in the layout in Android?

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.

android gridLayoutManager - how to make equal spacing when recyclerview is constrained tot left and right side of screen

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.

Android ConstraintLayout with Adaptable ImageView sizes

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

Set Fixed Row Height in GridView

I have images that I want to render in a GridView. These images are all the same size (250x250). However, I want to display them in 100x100 squares in the GridView.
<GridView
android:id="#+id/sectionListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="100dp"
android:stretchMode="none"
>
</GridView >
The above is the grid view. Where I am setting the columnWidth to 100. This actually works but for some reason, the height is always 120, leaving horizontal black bars at the top and bottom of my 250px image. Somehow there are getting 10 pixels on the top and bottom. Changing the vertical and horizontal spacing doesn't help because that affects the spacing between the elements. These black bars on top and bottom are inside the gridView item.
I would look into the ImageView which I assume you would be using in your adapter to show the images. You should set android:layout_width and android:layout_height to 100dp and make sure that android:scaleType is set to centerCrop.
In the CustomAdapter
view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, rowHigh));
rowHigh is the dimension you must to change
If you are using gridview, then I think you are also using another item_layout for displaying each item, I'm guessing it is an ImageView as you are trying to display images. Hence you can limit the size of the items to 100 x 100 in that item layout(ie, android:layout_width="100dp" android:layout_height="100dp"in the item_layout.) Hope it helps.

Linearlayout background image

I have a problem with Linearlayout background. The issue is that I am setting LinearLayout background as image. Lets say image size is 300 px height. This LinearLayout holds other views lets say rows. One row is 100 px height. The problem is that when I am adding a row to this LinearLayout, row fill only 100 px of 300 px. The question would be is it possible to strech or scale LinearLayout depending on how much it's children views take place?
The code of LinearLayout:
<LinearLayout
android:id="#+id/companies_list_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="#drawable/firm_list_background"
android:orientation="vertical" />
Your question is not so clear so:
First of all when you set an image as the background of a layout it is scaled to fit or cover the whole size of the layout.
Another method to do the same think is to add an imageview in your layout with android:layout_width="match_parent"
android:layout_height="match_parent"
and set its background resource to android:scaleType:"fitXY"
So:
1: You have set your companies_list_holder height to wrap_content so if a row is 100px height. it also has 100px height so the background is scaled to fit your 100px height layout.
2: If you had set your layout to have a 300dp height it would be scaled to fit your 300dp layout etc etc. then you could add your 100dp height child rows.
Finally i think that if you want to add multiple and unknown number of rows in a layout, the best solution is a ListView like in this example:
http://www.mkyong.com/android/android-listview-example/
and your listView would handle many thinks like scrolling etc
You can first get the height of your inner row and then set this height as height of your LinearLayout using LayoutParams

Categories

Resources