I have a ScrollView whose unique child is a LinearLayout:
<ScrollView
android:id="#+id/d_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/d_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparency"
android:orientation="vertical"/>
</ScrollView>
I later dynamically add children to the LinearLayout by using a LayoutInflator (let's say 5 children in this example), and they all have a weight of 1 (so equal heights). I want to only have 4.5 children visible at a time. To do this, I have used a post method to do change the layout_height property of the LinearLayout once the height of the ScrollView is known:
if (n > visibleItems) { // here n = 5 and visibleItems = 4.5
dScroll.setFillViewport(false);
dScroll.post(() -> {
int height = dScroll.getHeight();
ViewGroup.LayoutParams lp = dLayout.getLayoutParams();
lp.height = (int) Math.round(n * height / visibleItems);
dLayout.setLayoutParams(lp);
});
}
However, when I do this, the LinearLayout and its children behave as if their height was set to wrap_content (which is not the case). When using the Layout Inspector in Android Studio, I find that the layout_height of the LinearLayout is set to 1071 (the correct computed value), but the getHeight() method returns 578.
I have tried several combinations of requestLayout(), invalidate() and forceLayout() but it didn't change anything.
Keeping the fillViewport property of the ScrollView to true makes the LinearLayout take the height of the ScrollView, so in this case 964px instead of the 1071 that I want.
Edit: the problem really comes from LinearLayout. If I add a RelativeLayout between the ScrollView and LinearLayout like so:
<ScrollView
android:id="#+id/d_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/d_rel"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/d_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparency"
android:orientation="vertical"/>
</RelativeLayout>
</ScrollView>
and apply the new height to the RelativeLayout instead:
if (n > visibleItems) { // here n = 5 and visibleItems = 4.5
dScroll.setFillViewport(false);
dScroll.post(() -> {
int height = dScroll.getHeight();
ViewGroup.LayoutParams lp = dRel.getLayoutParams();
lp.height = (int) Math.round(n * height / visibleItems);
dRel.setLayoutParams(lp);
});
}
then the RelativeLayout takes the correct height (1071px), but the LinearLayout within it (that is in match_parent height) is still only 578px high.
I want to set Max Height of RecylerView.I am able to set max height using below code.Below code makes height 60% of current screen.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int a = (displaymetrics.heightPixels * 60) / 100;
recyclerview1.getLayoutParams().height = a;
But now problem is that, if it have no item then also its height is 60%.
So I want to set its height 0 when no item in it.
I want to achieve like something.
if(recyclerview's height > maxHeight)
then set recyclerview's height to maxHeight
else dont change the height of recyclerview.
How can i set it?
Please help me, I am stuck with it
ConstraintLayout offers maximum height for its children.
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="300dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Here is something, you need. Subclass the RecyclerView and override onMeasure as following:
#Override
protected void onMeasure(int widthSpec, int heightSpec) {
heightSpec = MeasureSpec.makeMeasureSpec(Utils.dpsToPixels(240), MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, heightSpec);
}
Make sure, you give proper number of pixels as the first argument in makeMeasureSpec(). I personally needed RecyclerView, that is 240dps at most.
I think this will work at least it worked for me
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/Id_const_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/Id_my_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_top_space"
android:scrollbars="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="#+id/Id_const_layout"
app:layout_constraintEnd_toEndOf="#+id/Id_const_layout"
app:layout_constraintHeight_max="150dp"
app:layout_constraintHeight_min="30dp"
app:layout_constraintStart_toStartOf="#+id/Id_const_layout"
app:layout_constraintTop_toTopOf="#+id/Id_const_layout"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
You can change the constraintHeight_max and constraintHeight_min according to your needs.
We can optimize #Fattum 's method a little bit.
We can use app:maxHeight to set the maxHeight. One can use dp or px as you like.
public class MaxHeightRecyclerView extends RecyclerView {
private int mMaxHeight;
public MaxHeightRecyclerView(Context context) {
super(context);
}
public MaxHeightRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
}
public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
}
private void initialize(Context context, AttributeSet attrs) {
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightScrollView_maxHeight, mMaxHeight);
arr.recycle();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mMaxHeight > 0) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
values/attrs.xml
<declare-styleable name="MaxHeightScrollView">
<attr name="maxHeight" format="dimension" />
</declare-styleable>
After trying way too many complicated suggestions, I finally figured this out through trial and error.
First, wrap the RecyclerView in some sort of layout. In my case, I used a constraint layout, and I even had other elements in that layout above and below the RecyclerView. Set the height of the layout to auto adjust by setting it to 0dp, then set the default layout height constraint as wrap and define a layout max height constraint.
Then, on your RecyclerView, set the height to wrap_content and layout_constrainedHeight to true.
Here is what it should look like:
<android.support.constraint.ConstraintLayout
android:layout_width="600px"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="450px">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintTop_ToTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
Hope this helps!
write this statements inside if else
and let me know ,
ViewGroup.LayoutParams params=recyclerview.getLayoutParams();
params.height=100;
recyclerview.setLayoutParams(params);
ill add my 2 cents i needed a recycler view with a max height and min height and wrap content between the 2
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/group_card_recycler_view_holder"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="#dimen/group_recycler_view_height"
app:layout_constraintHeight_min="#dimen/card_sentence_height"
android:layout_marginTop="#dimen/activity_horizontal_margin_8dp"
app:layout_constraintTop_toBottomOf="#id/group_name_container"
app:layout_constraintBottom_toTopOf="#id/myButtonLayout">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constrainedHeight="true"
android:id="#+id/group_card_recycler_view"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use WRAP_CONTENT in your RecyclerView. It will auto measure your recyclerview according to its content.
You can also calculate current height and set max height. So Recyclerview will use wrap_content attribute value until max height.
public static void getTotalHeightofRecyclerView(RecyclerView recyclerView) {
RecyclerView.Adapter mAdapter = recyclerView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getItemCount(); i++) {
View mView = recyclerView.findViewHolderForAdapterPosition(i).itemView
mView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
}
if (totalHeight > 100) {
ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
params.height = 100;
recyclerView.setLayoutParams(params);
}
}
The simplest way is to use ConstraintLayout and setting height constraint on Recycleview.
<android.support.constraint.ConstraintLayout
android:id="#+id/CL_OUTER_RV_Choose_Categories "
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/RV_Choose_Categories"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constrainedHeight="true" >
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
Please note that before Lollipop, the recycleview will not scroll. As a solution, add the below code in the activity's oncreate.
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
ConstraintLayout CL_OUTER_RV_Choose_Categories = findViewById(R.id.CL_OUTER_RV_Choose_Categories);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) CL_OUTER_RV_Choose_Categories.getLayoutParams();
lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
}
This will ensure that height is fixed only on supported devices, and the complete recycle view is shown on older devices.
Do let me know if there is any better way.
If you have a RecyclerView designed to hold items of equal physical size and you want a no-brainer way to limit its height without extending RecyclerView, try this:
int maxRecyclerViewHeightPixels = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
MAX_RECYCLERVIEW_HEIGHT_DP,
getResources().getDisplayMetrics()
);
MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(getContext(), myElementList);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
ViewGroup.LayoutParams params = myRecyclerView.getLayoutParams();
if (myElementList.size() <= MAX_NUMBER_OF_ELEMENTS_TO_DISPLAY_AT_ONE_TIME) {
myRecyclerView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
//LinearLayout must be replaced by whatever layout type encloses your RecyclerView
}
else {
params.height = maxRecyclerViewHeightPixels;
myRecyclerView.setLayoutParams(params);
}
This works both for Linear and Grid RecyclerViews, you just need to play with the numbers a bit to suit your taste. Don't forget to set the height to WRAP_CONTENT after you populate the RecyclerView.
You may also have to set the height again every time the size of myElementList changes, but that's not a big issue.
I just had this same problem, and wanted to share a new, up-to-date answer. This one works on at least Android 9 and Android Studio v. 4.0.
Wrap the RecyclerView in a ConstraintLayout, as some of the answers here have suggested. But it does not appear you can set the maximum height of that layout in XML. Instead, you must set it in code, as follows: Give the ConstraintLayout an ID, and then, in your onCreate or onViewCreated method for the activity or fragment, respectively, in question - for example, to set a max height of 200 dp:
findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // activity
view.findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // fragment
Annoyingly, ConstraintLayout does expose an XML attribute android:minHeight but no android:maxHeight.
Even more annoyingly, RecyclerView itself apparently comes with an XML attribute named android:maxHeight, but that attribute does not work.
You can just set a specific height to any value in the xml code and set the visibility to gone. Then you set the visibility to Visible in Java, when you want to inflate it. Here is an example;
.xml
android:visibility="gone"
.java
recyclerView.setVisibility(View.VISIBLE);
I have a custom view with Yellow background. I plan to add a Red background TextView, with match_parent for both width and height on it. Here's what I had done.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainView = (LinearLayout)this.findViewById(R.id.screen_main);
RateAppBanner rateAppBanner = new RateAppBanner(this);
mainView.addView(rateAppBanner);
}
}
RateAppBanner.java
public class RateAppBanner extends LinearLayout {
public RateAppBanner(Context context) {
super(context);
setOrientation(HORIZONTAL);
LayoutInflater.from(context).inflate(R.layout.rate_app_banner, this, true);
this.setBackgroundColor(Color.YELLOW);
}
}
rate_app_banner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#ffffffff"
android:background="#ffff0000"
android:text="Hello World" />
</LinearLayout>
Now, I would like to have a fixed width & height custom view. I realize, after I'm having fixed width & height custom view, the added TextView doesn't obey match_parent attribute.
Here's the change I had done on custom view.
RateAppBanner.java
public class RateAppBanner extends LinearLayout {
...
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = 320;
int desiredHeight = 50;
desiredWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, desiredWidth, getResources().getDisplayMetrics());
desiredHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, desiredHeight, getResources().getDisplayMetrics());
super.onMeasure(desiredWidth, desiredHeight);
//MUST CALL THIS
setMeasuredDimension(desiredWidth, desiredHeight);
}
I realize the added TextView doesn't match_parent anymore!
Now, we can see the Yellow custom view is having fixed size 320x50. I expect the Red TextView will fill up the entire custom view due to match_parent attribute.
However, that's not the case. I believe my implementation for custom view's onMeasure isn't correct. May I know what is the correct way to fix this?
The complete source code can be downloaded from abc.zip
After lots of trial and error and doing research work, final found answer.
You have set measurements for layout but not for child view, so for that you need to put this in onMeasure method,
super.onMeasure(
MeasureSpec.makeMeasureSpec(desiredWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(desiredHeight, MeasureSpec.EXACTLY));
Reference link : Inflated children of custom LinearLayout don't show when overriding onMeasure
And finally it's working :)
I want to resize my LinearLayout (or a view) to a dimension which is relative to the parent or itself. For example, I want the width to be 1/3 of the parent's width. Or, the height should be same as its own width. I don't want to use any constants , so that it works for all devices.
adding code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout android:id="#+id/ll_board"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
...
</LinearLayout>
code:
public class GMActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
LinearLayout board_layout = (LinearLayout)findViewById(R.id.ll_board);
// I wanted to resize board_layout here ..
// getParent().getWidth() returns 0
Log.d("gm", "layout: " + ((LinearLayout) board_layout.getParent()).getWidth());
// ..
}
}
getWidth() is giving 0. Is it too early to call this? If yes, what is the correct place to call this?
Basically my intention is to make the width of the layout a fraction of the screen size width, and, height same as its own width.
Considering layout your LinearLayout and that its parent it's another LinearLayout:
Get the parent's width:
int parentWidth = ((LinearLayout) layout.getParent()).getWidth();
Get the view's width:
int viewWidth = ((LinearLayout) layout).getWidth();
set the
view.setHeight(viewWidth );
view.setWidth(parentWidth / 3);
I found height=width solution (square shaped layout) in LinearLayout in Square Form
I am having two text views in Linear layout having horizontal orientation. Width of text views are wrap_content. If the sum of width of two text views is less than the screen width it is fine. If the sum of width exceeds the screen width then i need to change the orientation from horizontal to vertical.
I tried using getWidth() in onCreate of the activity but it returned 0. I can try creating a custom view with onSizeChanged() function but i am using two text views so i am not sure that when onSizeChanged() in one text view will not make sure that the other textview is fully drawn to get the width. Any suggestions is really helpful for me.
<LinearLayout android:id="#+id/status_container"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView android:id="#+id/view1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="#+id/view2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
// In OnCreate() function
TextView view1 = (TextView) findViewById(R.id.view1);
TextView view2 = (TextView) findViewById(R.id.view2);
view1.setText("Good Morning,");
view2.setText("I am Ron");
int view1_width = view1.getWidth();
int view2_width = view2.getWidth();
if ((view1_width + view2_width) > screen_width) {
// Change the Linear Layout orientation to Vertical
}
Here view1_width and view2_width are returning 0. I want to check if the view1_width + view2_width is greater than the screen width then i need to change the orientation into vertical, or else Horizontal orientation is fine.
-Ron
Add this to your activity's onCreate
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//You should be able to get the width and height over here.
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});