I have an edittext and its width is set to wrap-content.I also have an recycler view and its width is set to wrap-content. i want to make recycler view width to be equals to edit text width programmatically.I used below code but its not working.width returns from getMeasuredWidth() is not equal to edittext width.
editText= findViewById(R.id.edit_text);
editText.measure(0, 0);
testRecyclerView= findViewById(R.id.employee_recycler_view);
testRecyclerView.setLayoutManager(new LinearLayoutManager(context));
ViewGroup.LayoutParams layoutParams = testRecyclerView.getLayoutParams();
layoutParams.width = editText.getMeasuredWidth();
testRecyclerView.setLayoutParams(layoutParams);
You should end with <View>.requestLayout()
to refresh with a new size (currently employeeRecyclerView
).
Also views are not correctly measured in onCreate()
. In order to get editText
width you can use post
, postDelayed
, doOnPreDraw
(onPreDrawListener
) or viewTreeObserver
.