Inconsistent object placement inside CardView with RecyclerView - android

EDIT:
ADDITIONAL DETAILS: Strangely enough, it only gets buggy if there are only <=3 elements. More than that, because every element can be scrolled away, then the layout will "fix" itself upon reshowing.
ADDITIONAL DETAILS: Link to the demo video that showing the problem.
Youtube Video - https://youtu.be/zlwi_Bz-HQo
So below screenshot, was taken from the same run from an android studio.
In the screenshot, I have 2 exact same card view (with dummy data straight from the .xml, so, I assign no data from java file).
The problem
If you guys look at the bottom right of each CardView, there's a
text view called "read more".
Weirdly enough, even if they're identical, it's placed differently.
Btw, it's actually 3 identical cards. On the first run, the top "read
more" also incorrectly placed, but it auto-corrected itself when I
completely scroll it down and back to the top.
The second problem is the 5x4 dots on the top of the card. It's differently placed from what it's seen from the editor.
(The placement is accurate on editor)
Any idea how to handle this irregularity? Thanks.
Btw, I'm not sure if you guys need the code, but just in case, here it is on Pastebin (to shorten the post length).
visit_note_timeline.xml (the cardview)
VisitNoteAdapter.java (In case you're wondering, "visit note" is just a dummy empty class)
MainActivity.java
ratings_previews.xml (the 5x4 white dots on top right of the card, under more button)
activity_main.xml
content_main.xml

Use match_parent for RecyclerView.
Fix like this:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view_timeline"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
If parent view's width is wrap_content, parent view's width can not be determined until child view's width is determed.
I don't know the result of setting layout_width="wrap_content" for parent view and layout_width="match_parent" for child view.
I guess the result may be wrong layout.
In your layout, the same situation is happend.
RecyclerView has layout_width="wrap_content" and CardView has layout_width="match_parent".

Related

I cant move any elements in the layout (Android Studio)

I cant move any elements in the layout (android studio) for some reason..
I searched alot and nothing worked with me.
All text views or buttons (all elements) stays top-left and I cant move them or even resize them...
Whats the solution for that?
And if I tried to resize the button it fills up the whole screen.
thats the code view
This is happening as you have used constraint layout in your XML file.
and the constraints as you have given in your file that makes your elements stay up at the top left.
Solution 1:
When you give start to start constraint to the parent and end to end constraint to the parent it makes your view to stick to the both side of the screen.
Even if you have given your view's height and width to wrap content it will be considered as match parent.
<androidx.constraintlayout.widget.ConstraintLayout ...>
<Button android:id="#+id/button" ...
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</>
as This will make your view to stretch to the both side of the screen and will take whole space horizontally.
all you need to do is remove the constraints mentioned below:
if you want to let your view to stick to the left side of the screen-
Remove EndtoEndof = parent constraint
if you want to let your view to stick to the right side of the screen-
Remove StarttoStartof = parent constraint
The issue with resizing the button is that you have given the constraints to the view to stick to bottom and top of the screen so it will take up whole screen vertically.
if you want to make your button to stay to the bottom
Remove toptotopof = parent
if you want to make your button to stay to the top
Remove bottomtoBottomof= parent
all you need is to understand how constraint layout works .
Look into this link it will help you understand the constraint layout from broad perspective.

Eclipse android can't make views bigger

If i try to make the thing u see in the screenshot it just falls back to original size. I can't change sizes of any view Objects. Anybody knows a fix?
http://imgur.com/2S1xoLP
In Eclipse (as you're using the designer), you can set the Width and Height of a View, within the Layout Parameters section of the Properties pane. This can be set to wrap_content, match_parent or fill_parent.
You can also do this within the XML markup of the activity you're editing. Click the .xml tab at the bottom of your designer, and you'll see all of the XML that makes up your activity. Once in there, find the problematic view and add:
android:layout_width="match_parent"
android:layout_height="match_parent"
Edit
Also, when inside of a RelativeLayout, it's possible that Eclipse will add default padding values, so regardless of what you set, your View's wont reach the parent layout's edge, until you remove them. Just FYI!

Constrain the height of RelativeLayout to specific child view

I there a way to wrap_content on a specific element inside of a parent element? For instance, I have something like the following layout:
<RelativeLayout width:match height:wrap>
<ImageView width:match height:wrap scale:fitXY />
<LinearLayout width:wrap height:wrap>
</RelativeLayout>
The parent wrap constraint is very loose, but I want it to specifically use the matching width, but always match the height of the image view.
The problem here arises when I place this view in another RelativeLayout where each view is aligned above or below another in order to fill a potentially changing superview. LinearLayout didn't really seem to stretch things to fill, so I switched to Relative, but when I did, the view described above stretched vertically when I want it to still match the height of the image view.
Is there a good solution to this problem?
You could try putting the following (pseudocode) in the onResume() method:
if(myRelativeLayout.height > myImageView.height)
myRelativeLayout.setHeight(myImageView.height);
You need to make sure to call myRelativeLayout.measure() before you do this, so the system knows what the size of the Views will be.
Just an idea for you to try, let me know if it works :)

Android TableLayout in a ListView, mysterious column shrinking

I am using TableLayout within a ListView. The data I'm presenting is tabular in nature and the TableLayout seems like a good way to ensure that the columns line up as desired. This approach worked well most of the time - see below.
Desired view:
But occasionally the Views representing the columns wrapped their text content as shown below.
After searching around for a while, I came across the following discussion on the Android Google Group. In response, I set android:stretchColumns="*" in the TableLayout and voila, the contained TextViews stopped mysteriously wrapping their text.
Here's a partial content of the layout's XML, with the change highlighted with a comment:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"> <!-- stretchColumns needed to prevent
TableLayout from unilaterally deciding to shrink the column
width and thereby causing text to wrap. -->
While I'm now getting the desired result, I'm a bit confused as to why this "fixed" my problem. The TableLayout documentation states that "The width of a column is defined by the row with the widest cell in that column." But if that's true, why did the columns apparently shrink in size in the first place causing the text to wrap?
Does anyone have a good explanation?
Containers query their children for how much space they want, and this process continues recursively. Any dynamically sized layout framework is going to work this way. From the inside out, children report up to their parents their desired size, and then on a second pass the parents report back to their children how much space is available to them. When they're not included in the stretchColumns list (either explicitly by position, or implicitly by wildcard as in the example), they ask for the bare minimum which in the case of TextView is the minimum space required to display the text wrapped. When they're included in the stretchColumns list, they report the width required to display the entire contents, and if it fits they don't get wrapped. Anyone who requests more space than is available to them gets wrapped text. I imagine the priority is given in order from least space required up to the most so if you have stretchColumns="*" the largest blocks of text are getting wrapped first (if necessary) and everyone else gets to be displayed normally.

combining wrap_content on parent and fill_parent on child

Setting two or more elements of a linear layout the same height seems to be a great problem.
I want to set four buttons in a row to the same height.
android:layout_height="wrap_content" does it for the moment but when the text on one of the buttons is longer than one line this button is increased and therefore bigger than the other ones. Due to different localisations I don't know, when and which button may have a second line.
So my idea is, to set the parent linearlayout to android:layout_height="wrap_content" and all (!) child heights to android:layout_height="fill_parent".
This works (all buttons have the same size), but I'm not sure if this causes any other problems? Because it the parent gets it's height from the childs and vice-versa.
In theory what you are describing should not work ("Because it the parent gets it's height from the childs and vice-versa".) However, we made it work in LinearLayout because it was a very common use case. I recently added similar support to FrameLayout (this feature should be part of Honeycomb.) What you are doing is therefore perfectly valid and will work just fine.
That doesn't make sense :(
Why don't you use android:singleLine="true" and some ellipsode?

Categories

Resources