How to get the height of recyclerview item in onBindViewHolder
Short
Measure the View manually
view.measure(
View.MeasureSpec.makeMeasureSpec(recyclerViewWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
and get the height with view.getMeasuredHeight()
Background
A view will return a valid value for View.getHeight()
only after it has been measured. The measuring itself will automatically happen by the system when the view is about to be displayed on screen.
When Android wants to display the layout, it will recursively call the view.layout()
function for each view in the view tree. Each Parent tells its children the constraints they might have (width/height) and ask them to view.measure()
themselves. As a result, the view will store the measured values BASED on the constraints in designated members (MeasuredHeight/Width). Note that at this point view.getMeasuredHeight()
will hold the value while view.getHeight()
will still be invalid. view.getHeight()
will only return a valid value once the view has an actual height in the UI hierarchy.
Recap
So, to know the height of a view element, before it has been measured and laid out by the system, we will need to invoke the view.measure()
function manually.
The measure function expects 2 parameters which derived from the view LayoutParams + the parent constraints.
In the above code sample, we are measuring the view forcing its width to be EXACTLY the width of the parent (the RecycleView), and the height is not limited.
Can we adjust items height in a recyclerview?
Yes you can, all you need to do is to make the item layout parent container wrap_content
in its height (Or width if it's horizontal scrolling). Something like this:
list_item_layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
<!-- here -->
android:layout_height="wrap_content"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</FrameLayout>
The newest recycler view libraries handle wrapping content well now and there is barely any bugs to it.
Get Width and Height of one row in RecycleView - Android
@Override
public void onBindViewHolder(final MyAdapter.MyViewHolder holder, int position)
{
.....
holder.itemView.post(new Runnable()
{
@Override
public void run()
{
int cellWidth = holder.itemView.getWidth();// this will give you cell width dynamically
int cellHeight = holder.itemView.getHeight();// this will give you cell height dynamically
}
});
.....
}
I hope this will help you!!
At what point can I get the width of a view in a RecyclerView?
Wait until your view gets rendered on UI. There are a few ways to do that. One of them is to use post()
of the view which you want to get the height.
Like
container.post(new Runnable() {
@Override
public void run() {
imageView.getLayoutParams().height = container.getWidth();
imageView.requestLayout();
}
});
And View's getWidth() and getHeight() returns 0 is an amazing post which contains few Kotlin solutions too, which are more easier than Java.
RecyclerView item height and width change dynamically Android
Set height and width dynamically according to the screen size. this is the best way to get perfect result.
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//if you need three fix imageview in width
int devicewidth = displaymetrics.widthPixels / 3;
//if you need 4-5-6 anything fix imageview in height
int deviceheight = displaymetrics.heightPixels / 4;
holder.image_view.getLayoutParams().width = devicewidth;
//if you need same height as width you can set devicewidth in holder.image_view.getLayoutParams().height
holder.image_view.getLayoutParams().height = deviceheight;
try this in adapter in getview hope you get better result in all device
EDIT:
Or to set same height same as width you can set devicewidth in height also like below:
holder.image_view.getLayoutParams().height = devicewidth;
Horizontal RecyclerView with dynamic item height
I have fixed the issue without using FlexBoxLayoutManger
.
Used LinearLayoutManager
with horizontal
orientation and the following code in onBindViewHolder in RecyclerViewAdapter
.
holder.itemView.post {
val wMeasureSpec =
View.MeasureSpec.makeMeasureSpec(holder.itemView.width, View.MeasureSpec.EXACTLY)
val hMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
holder.itemView.measure(wMeasureSpec, hMeasureSpec)
if (holder.itemView.measuredHeight > holder.itemView.height) {
holder.itemView.layoutParams =
(holder.itemView.layoutParams as ViewGroup.LayoutParams)
.apply {
height = holder.itemView.measuredHeight
}
}
}
Related Topics
Adb Error (Error: Protocol Fault (Couldn't Read Status): Invalid Argument)
Android Adb Devices Does Not Detect My Phone
How to Automatically Open App When Receive Push Notification
How to Disable Viewpager from Swiping in One Direction
Control the Default Music Player of Android or Any Other Music Player
How to Enable/Disable Floatingactionbutton Behavior
How to Clear Livedata Stored Value
How to Create a Folder in Android External Storage Directory
Android Sqlite: How to Retrieve Specific Data from Particular Column
Android Compress Video Before Upload to Server
Load Video from Firebase Storage to Videoview
Change Button Text Color When Pressed
How to Display HTML in Textview
Html5 ≪Video≫ Element on Android
Background Image Jumps When Address Bar Hides Ios/Android/Mobile Chrome
Rendering HTML in a Webview With Custom Css
How to Change Styling of Textinput Placeholder in React Native