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.
Related
Trying to do this android tutorial,
Drag a Plain Text (EditText) element from the Palette pane to the
bottom of the layout, and add constraints to the left side of the
layout, the bottom of the layout, and the left side of the Send
Button. Set its attributes in the Attributes pane as follows:
Attribute
Value
id
editText_main
Right margin
8
Left margin
8
Bottom margin
16
But I do not see a way to set the layout margin. When I select the editText_main and input 8dp into the layout_marginLeft, and press Enter or Tab, the number 8dp disappears. It was swallowed without a burp.
It was not obvious, but I right clicked and then selected the Constrain menu and added constraints. Then I went into Attributes and it allowed me to modify the current value.
It seems like a design bug.
I'd advise you not to use the drag pane only, Some features are much easier with the xml editor
Goto you xml file add margins like this.
<EditText
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp" />
It is not a bug, a constraint layout child CANNOT have a child with margins because it has a set position (if you just drag and drop it from the palette), the position wont change no matter what, so adding margins is useless, when its constrained those margins become useful because lets say i constraint a button to the left but i don't want it completely to the left, so lets add a marginLeft of 8 dp, in that case it is relevant to have a margin thats why the editor wasn't letting you add them until you constrained it.
You can see this in the "Text" tab, the position is absolute, margins are useless when the position is absolute. So the editor prevents you from adding them.
Whenever you add a constraint to it the editor gets rid of the absolute position and lets you add margins.
Maybe your tutorial didn't specify what layout to use since it used to not be relevant until constraint layout was implemented.
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".
I am developing an app with an activity with member reactions on a hike event. The reactions are the yellow "balloons" which are made using a LinearLayout. Each item is constructed from a XML file (listitem_deelnemerreactie.xml) which defines the layout for a reaction item. The top level of this layout file is a LinearLayout my itself.
I want some spacing between the separate elements, as well as some right margin. The most straightforward way to so this should be: setting a bottom and right margin on the top-level LinearLAyout element of the listitem_deelnemerreactie.xml layout file.
But setting the bottom margin on the LinearLayout has no effect on the vertical spacing, though the right margin does have an effect.
The only way to be able to set a vertical margin appears to be: setting is in the Java code, after attaching the inflated view to the container.
See the two images for the effect and the code.
Though setting the margins in the code is a working workaround, I still think it is strange this cannot be achieved in the XML. Why is the bottom margin attribute ignored while the right margin is not?
Any ideas?
Have you tried to set an android:padding="10dp" for example on your elements to spaced them ?
I want to make an about button in the top left. I tried:
android:layout_gravity:"top|left"
but it doesn't work , I searched and all what I found was using RelativeLayout and if I use that I'll have to make all my layout from beginning and it's not that good like the linear layout.
Couldn't post the code here. So this is my code on pastebin
http://pastebin.com/5EjgyB0K
Here you have given android:layout_gravity="center" to the Linear Layout so it is going to set gravity of the layout and as center and your About Button is child of layout its to going to set in center and you have given Margin_top also.Try to remove gravity amd Margin_top and you can see the result, the button will be top|left of the screen.
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 :)