RecyclerView height of item - android

I have problem with measuring height of box.
I got two boxes (overlayed) :
One is displaying text etc and second box is when settings buttons will be clicked.
I got problem like a photo below
holder.local_normal_box.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
var height: Int = holder.local_normal_box.getMeasuredHeight()
holder.local_settings_box.maxHeight = height
holder.local_settings_box.minHeight=height
Blue overlay is settings box.

Use ConstraintLayout instead of LinearLayout. It is the layout recently recommended by Google

Related

Display another composable layout if one is two wide for screen

Context:
In the app, there are dynamic questions from the backend asking users to select an option, usually two or three options.
The first choice design is to display the options as buttons in a row. But sometimes, the button texts could be very long that the buttons or the texts are not displayed properly on a mobile phone. In this case, the options should be displayed as a dropdown.
What tried:
To get the width of the button row, I tried to create a mutableState isBtnStyle var, pass a callback lambda to the button layout composable, get and pass back the row width, compare the row width with the screen width, and if the row width is bigger, display the dropdown layout.
val screenWidth = Resources.getSystem().displayMetrics.widthPixels
var isBtnStyle by remember {mutableStateOf(true)}
val rowWidth = { width: Int ->
if ( width > screenWidth ) isBtnStyle = false
}
if(isBtnStyle){
btnStyleComposable(rowWidth)
} else {
dropdownStyleComposable()
}
The problem:
The button layout composable is always displayed first for measuring the width, then the dropdown composable is displayed if the button layout is too wide for the screen.
Question:
How could I get the width of the row of buttons before the row is visually displayed, so the decision can be made on which composable layout to be displayed?
Thank you
Here is your answer.
For sake of minimum message length, I will add that I prefer to use Modifier.onPlaced.

Resizing only the height of CardView also changes the width

I currently have a recycler view that has a list of CardViews.
When I click on the card I want to be able to expand the card (change only the height). This is working fine by setting the new height to the card with an animation.
The problem is that when I click to expand the card, the width also shrinks to less than half the width of the screen. If I scroll the view away and come back the width is set correctly. Every time I try to expand the height, it expands to the right size but the width shrinks.
Is there something that I'm supposed to do to expand/collapse a card in a recycler view and not have this side effect of also resizing the card width?
I tried only setting the height in a valueAnimator and that works for the expansion.
I tried to set the width programmatically at the end of the animation to match parent but that has no effect.
for better solution pls share code with us, until that.
check your xml codes for any Wrap content value for width.
It will solve your issue.

libGDX Scrollpane not scrolling for single Label

I am adding "How to Play" instructions to a game which I have created using libGDX. The instructions scroll need to be shown only inside a fixed area of the screen, not as a full screen scroll. I have created a stage and added some background and other actors, and then some labels which are the title(outside the scroller) and the instructions text(the only thing that need to be scrolled). It also should not be scrolling horizontally, just vertically. So I set the width of the Label and wrapped the text. In short I need full control of the position and size of the scroller, it shouldn't be full screen. So I wrote the following code to add the instruction scroller:
private void addHelpContent()
{
int width = (int) (0.85*alertBounds.width);
int height = scrollStartY - scrollEndY;
int x = Constants.APP_WIDTH/2 - width/2;
BitmapFont font = Model.getInstance().createFont(Constants.ROCK_NORMAL_FONT_FILE_PATH, 20);
Label.LabelStyle labelStyle = new Label.LabelStyle();
labelStyle.font = font;
labelStyle.fontColor = Color.valueOf("A07E10FF");
Label label = new Label(getHelpString(), labelStyle);
label.setWidth(width);
label.setWrap(true);
label.pack();
label.setFillParent(true);
ScrollPane scrollPane = new ScrollPane(label);
scrollPane.setBounds(x, scrollEndY, width, height); //This should be the bounds of the scroller and the scrollable content need to be inside this
scrollPane.layout();
scrollPane.setTouchable(Touchable.enabled);
addActor(scrollPane);
}
But now I am facing two problems:
I am not able to scroll the scrollpane. The label is cut off right at the bounds making the Scrollpane useless.
The other actors except the Labels doesn't appear at first, they will appear only after I scroll the scrollpane(and it won't scroll). This problem is not there if I comment off the code to add the scroll pane. So I am sure it is related to the scrollpane itself.
Can anyone please help me solve this?
I just forgot to add the act() method in the stage class. Adding it solved the issue.
The scrollpanel is a little tricky to get it to work. From my understanding, you cannot set the size of the scrollpane as it will automatically size itself to the same size as the label.
The trick is to add your scrollpane to a table. Then when the size of the label is larger than the size of the table the scrollpane will allow the label to scroll within the table. To make sure it doesn’t scroll horizontal, set the width of the table to the same width as the label. You also shouldn’t have to set the bounds of the scrollpane, let it size itself to the label.
Then instead of adding the scrollpane, add the table that contains the scrollpane.
addActor(table);

Achieving dynamic Android LinearLayout height

I am currently designing the widget for my app (see attached screenshot). Using a LinearLayout I was able to create the following setup:
Image: weight - 2
Title section: weight - 1
Price section: weight -1
However I would like the title and price section to be of fixed height while the image's height changes (shrink or grow) when the user resizes the widget.
How can I make the height of the image dynamic without using layout_weight? Thank you
To achieve a resizable image section all I had to do was change the weight of the Image to 1 and set the actual height in dp for the title and price section.
UPDATE
A more efficient alternative is to use RealtiveLayout, this can be achieved by:
1. Having the price section having fixed height and aligned to parent bottom
2. Set the title section having fixed size and above the price
3. Set the image as match_parent height and above the title.

Android - Button height not decreasing to wrap text

I set the button's height via XML. Suppose it to be X
<Button
android:layout_height="wrap_contnet"
android:textSize = " <X> dp"
....
....
/>
If I set X to greater than 15 dp, the button's height grows to fit the text. But when I set it to something lower like 2dp, the button's height doesn't change and I can see a thick border around it as shown below:-
As shown above, text size is so small but still height doesn't decrease!
How can I force it to be of appropriate height?
Thanx in advance!
Font size does not depend on button height. If you set it too small to fit the text, you will get the result you observe
EDIT
wrap_content would usually do the trick, however Button class sets some layout parameters like margin, background so it takes more space. You may get rid of Button and use i.e. TextView class, styled by hand to match your design. onClickListener will work perfectly fine with it and you will get result you want and full control over your button
EDIT 2
As this is bit related - there's AutoFitTextView widget on Github that deals with automatic text size scalling: https://github.com/grantland/android-autofittextview
Just Set android:minHeight="xxdp"

Categories

Resources