Why all my controls(buttons, textfields) are all in upper left of my emulator when I test run it?
When you first add widgets to your App, it automatically goes to the Top-Left of the screen. Try adding some Constraints to your widgets.
To make a View position in the center of your App, add these lines of code to the bottom of a widget in your XML:
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
Example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
/>
It Looks like you may be using a constraintLayout. Make sure that in your layout you have a vertical and horizontal constraint set on each control.
Your views are not setup up correctly in your layout. First of all, if you could give the code for your layout (activity_main.xml or something similar) it will be easier to solve your problem.
The likely culprit is either that you are using a RelativeLayout and haven't set the view to be in relative position to another or, more likely, that you are using a ConstraintLayout and haven't linked your views correctly.
If you are using constraint layout the easiest way to link your views is from the Design tab, looking at the UI of your app. Click your TextView/EditText/etc and drag from the little squares that appear on the sides to literally link to either the side of the screen or to another view.
Related
When using android:ellipsize="end" property, 3 dots are vertically centered instead of appearing in the bottom of the view.
How should I make 3 dots appear in the bottom?
<TextView
android:id="#+id/widgetTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:text="#string/lorem_ipsum_short"
android:maxLines="1"
android:ellipsize="end"
android:layout_marginStart="#dimen/spacing_4"
android:paddingEnd="#dimen/spacing_2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#id/widgetTimeArrived"
app:layout_constraintStart_toStartOf="parent"
/>
How it looks
It's hard to tell without access to your full layout, but that's not the standard behavior, so you may be using a custom Font/Theme.
This is how it looks in a blank layout with a ConstraintLayout:
Notice I pinned the "end" to the parent, since I don't know what your widgetTimeArrived is.
I am getting your expected results by implementing your code. I think #Martin Marconcini is right about some custom theme or it might be some views alignment or constraint issue.Your full xml might be helpful to understand that.
Here are the results:
Image a ConstraintLayout which includes three vertically stacked items:
#+id/top
#+id/middle1 or #+id/middle2 (one gets View.VISIBLE, one gets View.GONE)
#+id/bottom
The top of #+id/middle1 and #+id/middle2 is constrained to the bottom of #+id/top, no issue here.
The interesting case is #+id/bottom. That shall always be placed below #+id/middle1 or #+id/middle2, depending on which one is visible (the other will get View.GONE). Is there any way to model this in the layout file or do I have to change the top constraint for #+id/bottom while switching between showing #+id/middle1 and #+id/middle2?
As proposed by CommonsWare, it can be done with a Barrier as follows:
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:barrierAllowsGoneWidgets="false"
app:constraint_referenced_ids="middle1,middle2" />
Note that the IDs in app:constraint_referenced_ids are written without the #id/ prefix.
As my middle1 and middle2 have different heights and I set one of them to View.GONEI went with app:barrierAllowsGoneWidgets="false" to make sure that the Barrier position gets updated and no blank space is left over.
For more details see the official documentation or e.g. on Youtube the video ConstraintLayout Tutorial Part 4 - BARRIERS AND GROUPS - Android Studio Tutorial
Whenever I try to drag and drop "Button" or "TextView", it give the error message with a red exclamation mark. How to solve this permanently?
enter image description here
It means exactly what it says, the view has no constraints set. The constraints are what set its position on the screen and relative position to other views. The way to fix the error is to add constraints, either with the GUI or in the XML file directly.
For example, to make it centered at the top of the screen you could set the following attributes in the XML file (click the "Text" tab below the image preview to edit the XML)
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
For more details about setting up a ConstraintLayout, including how to set constraints in the GUI rather than XML, check the user guide here. Whenever you drag a new component into your layout you will see this error until you've set constraints for it.
A view must have at least one horizontal and vertical constraint or else it would be scattered when run on a real device. The warning is for you to take note of that.
See sample of a well constraint view.
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Since when you create the view (Button, TextView etc) it has only a design-time view, you should set specific positions on the screen which you intend to locate the view exactly when the application is run. That is what mean by setting constraints. If the view should have at least one constraint. Otherwise it automatically goes in to the (0,0) location (top-left corner) when your run your application.
Setting Constraints
1.GUI (Design)
Go to Design of your activity xml and drag from the anchors of the view and set the place or
Go to Attributes -> Layout -> Constraint Widget and set values for the constraints.
2.Text
Following is the xml code of constraints corresponds to the above image.
<Button>
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.117"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"></Button>
Hope this is somehow helpful. Happy coding!
I position lots of items relative to a layout guide,
and would like to position a new item nearly relative to this layout guide.
I tried with a negative layout margin without success.
android:translationX="-10dp"
android:translationY="-10dp"
This answer is now obsolete. See the accepted answer for an update.
Here is a blog posting that discusses negative margins in ConstraintLayout.
Using Spaces for negative margins
A view in a ConstraintLayout cannot have negative margins (it’s not supported). However, with an easy trick you can have similar functionality by inserting a Space (which is essentially an empty View) and setting its size to the margin you want.
Support for setting negative margin in a ConstraintLayout has been added in ConstraintLayout 2.1.0 alpha 2 on the 17th of December 2020.
You can update to it by setting the dependency to:
implementation 'androidx.constraintlayout:constraintlayout:2.1.0-alpha2'
The full changelog is available here: https://androidstudio.googleblog.com/2020/12/constraintlayout-210-alpha-2.html which includes:
ConstraintLayout
supports negative margins for constraints
This means that now you can do things such as android:layout_marginTop="-25dp", which you couldn't do before!
You can use a View to hold a fixed value like 30dp like below:
<com.mapbox.mapboxsdk.maps.MapView
android:id="#+id/mapView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="45dp"
app:layout_constraintBottom_toBottomOf="#id/space"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mapbox_uiRotateGestures="false" />
<View
android:id="#+id/space"
android:layout_width="match_parent"
android:layout_height="30dp"
app:layout_constraintTop_toBottomOf="parent" />
Creating a view for position your siblings might not be the best call because the view will be drawn (even if it's invisible) it will still uses ressources.
You can try to use guidelines instead :
You can add a vertical or horizontal guideline to which you can constrain views, and the guideline will be invisible to app users. You can position the guideline within the layout based on either dp units or percent, relative to the layout's edge.
https://developer.android.com/training/constraint-layout#constrain-to-a-guideline
I'm creating this in a XML file and want a text view to be placed in a specific spot.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="200dp"
android:text="Text"
android:textColor="#color/blue"
/>
It's underneath the last button that was placed so it's around in the middle of the screen. I want it to be at the bottom of the screen of to the right.
Easy if you're using a RelativeLayout as parent.
android:layout_alignParentRight
android:layout_alignParentBottom
(check the capitalization, I'm not anywhere I can double check).
Using a RelativeLayout would be perfect if you would be specifying each views' positions. It is the most flexible layout among the Android Layouts.
Take a look at this tutorial. It discussed how to position views in a RelativeLayout.
I hope you can get ideas from it in solving your problem.