Measuring A Slider Inside A Custom View Inheriting From GridLayout

Nathan Sokalski 4,106 Reputation points
2024-10-08T23:24:55.4166667+00:00

I have a Custom View that inherits from GridLayout. The xml layout file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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="wrap_content" android:columnCount="1" android:rowCount="1" tools:ignore="HardcodedSize,RequiredSizeWidth,RequiredSizeHeight,NegativeMargin" tools:background="@color/DarkOrange">
	<androidx.recyclerview.widget.RecyclerView android:id="@+id/rvTicks" android:layout_column="0" android:layout_row="0" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:layout_marginRight="0dp" tools:background="@color/Green"/>
	<com.google.android.material.slider.Slider android:id="@+id/sldValue" android:layout_column="0" android:layout_row="0" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/Base.Theme.MaterialComponents" style="@style/SettingsSliderStyle" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" tools:valueFrom="1" tools:valueTo="10" app:trackColor="@color/Transparent" app:trackHeight="6dp" app:thumbRadius="15dp" app:tickRadiusActive="12dp" app:tickRadiusInactive="12dp" app:tickColor="#44000000" app:tickVisible="true"/>
</GridLayout>

I need to know the width of the Slider before binding the RecyclerView. I sometimes find the Measure methods somewhat confusing, and am not always sure when the values in MeasuredWidth & MeasuredHeight are updated & when I need to call Measure(…), as well as what parameters to use for MakeMeasureSpec(…) when calling it (especially for values such as match_parent & wrap_content). I need to know the Width/MeasuredWidth before binding the RecyclerView items (or figure out some way to update all the items afterwards once I do know the Width/MeasuredWidth, although knowing it before binding would be better). What/when is the best way to do this? Thanks!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,355 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,872 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,949 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.