android ConstraintLayout does not allow negative margins - android

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

Related

Max lines with height set as wrap_content

I have two TextViews in a vertical LinearLayout, one serves as a display for a book's title and the latter as a display for the book's author(s).
I need the first to have wrap_content as its height, so it takes a good part of the linear layout. However, I want it to cap out at three lines max, so that there is still some space left for the second text view;
and I need the latter to fill the remaining space (0dp and layout_weight=0dp).
I want to use specific configuration so that the author view will be always right after the title view (on its bottom).
Something like this, however the max_lines do not kick in.
I tried to set max_lines to 3 and wrap_content for the first view height, but it seem that max_lines is ignored if height is set to wrap_content.
I also tried to circumvent the problem by sort of cheating and adding a max_height, but then the two views may be spaced apart from one another.
At last I tried to convert the linear layout to a constraint layout, to see if I could access some other layout settings to no avail.
Any help?
It seems to be working fine on my side with android:max_lines="3" even with wrap_content
This is just a test layout I created
<TextView
android:id="#+id/textview1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="#dimen/_13sdp"
android:textStyle="bold"
android:text="Very Very Very Very Very Very Very Very Very Very Very Very Long Text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Very Very Short Text "
app:layout_constraintStart_toStartOf="#+id/textview1"
app:layout_constraintTop_toBottomOf="#+id/textview1" />
Screenshot ->
Hope this helps! :)
Replacing app:max_lines with android:max_lines seems to have fixed the issue.

Android Studio 3.1.3 Error: The view is not constrainted. How to solve this permanently?

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!

Android Studio - Why all controls are all in upper left

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.

Android: How do I put a solid color rectangle in a Layout?

I have a RelativeLayout that inflates just fine. I would like to add a solid color rectangle spanning the width of the layout at the top. I tried putting the following into my xml:
<view android:id="#+id/top_alert"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_above="#+id/orders_gridview"
android:layout_alignParentTop="true"
android:background="#color/table_request_assistance"
android:visibility="visible"/>
Now, when I try to inflate my Layout I get a NullPointerException at LayoutInflater.createViewFromTag (line 715):
if (name.equals(TAG_1995)) {
name is set earlier thusly:
if (name.equals("view")) {
name = attrs.getAttributeValue(null, "class");
}
Evidently there is no "class" attribute. How do I add that? I can't find anything close in http://schemas.android.com/apk/res/android. Should I add it? Is this the standard way to do this? It seems like it should be the simplest thing in the world.
For the noobs here is some more general markup. This will print a 10-pixel high grey rectangle spanning the top of its parent view at the top.
<View android:id="#+id/rectangle_at_the_top"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentTop="true"
android:background="#DDDDDD"
android:visibility="visible"/>
Explanation:
This is the rectangle's id:
android:id="#+id/rectangle_at_the_top"
This says make the View as wide as the parent:
android:layout_width="match_parent"
Note you'll sometimes see "fill_parent". That has been deprecated in favor of "match_parent".
This says make the height 10 "ensity-independent pixels high:
android:layout_height="10dp"
What is a "density-independent pixel" you ask? I'm not 100% sure, but these guys know: What's the difference between px, dp, dip, and sp in Android?
This says align the rectangle with the top of the parent View:
android:layout_alignParentTop="true"
More accurately it makes the top edge of the View the same as the top edge of the parent. Want to put something at the bottom? Yup, you guessed it: use layout_alignParentTop.
This says set the background color to a grey-ish color:
android:background="#DDDDDD"
DDDDDD is a color value. You can find examples of other color values and how Google suggests to use them here: Google's Android Color Guide
Finally, this says to make this View visible:
android:visibility="visible"
This is mostly redundant as they are visible by default. Other options include "invisible" and "gone" which sound similar but are crucially different. For more info see this discussion: What is the difference between "invisible" and "gone?"
Classes are case sensitive - in this case, you are using the View class and therefore it needs to be exactly View in your XML.

Android Eclipse placing a text view in specific places

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.

Categories

Resources