This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections
add properties to your view like app:layout_constraintStart_toStartOf="parent" and app:layout_constraintTop_toTopOf="parent" and then give margin to set where ever you want.
and you can find all property of constraint layout here ConstraintLayout
You can set the vertical constraint for your view like this
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Or you can use the UI Editor from android studio, select the dots on the top and bottom of the view and drag them to the parent, this will set the vertical constraints, you can them edit them directly from xml or UI editor.
UiEditor Screenshot
Related
This is the layout I want to design:
As you can see from the image, I want the linear layout to be constrained to both Item A and Item B and I want it aligned to left. I couldn't find any way to implement this inside a Constraint Layout. When I use this:
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="#dimen/ui_size_xs">
linear layout moves to the middle.
When I use this:
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="0dp"
android:layout_height="#dimen/ui_size_xs">
the linear layout occupies the whole area between Items A and B.
I want the linear layout to be wrap-content but should also align to the left like in the image. The only solution I could find is to create a new layout as parent of this linear layout and give 0dp to new parent linear layout and wrap-conent to this layout child layout. I don't want to do that. Is there any way to achieve this without creating any extra layout?
As mentioned in offical docs,
The default when encountering such opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes:
layout_constraintHorizontal_bias
layout_constraintVertical_bias
Try the following and see if it works
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="#dimen/ui_size_xs"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintLeft_toRightOf="item1id"
app:layout_constraintRight_toLeft="item2id">
// try different horizontal bias values to meet your need
I've encountered a problem with ConstraintLayout while simply placing 2 views in the center of screen. Like Title and Content bellow. Both should be centered vertically. So my layout looks like:
<?xml version=“1.0” encoding=“utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:id=“#+id/title”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:text=“Title”
android:textAppearance=“#style/Title1"
app:layout_constraintBottom_toTopOf=“#id/content”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent”
app:layout_constraintVertical_chainStyle=“packed” />
<EditText
android:id=“#+id/content”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:inputType=“text|textMultiLine|textNoSuggestions”
android:textAppearance=“#style/Title4"
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf=“#id/title”
tools:text=“body\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\n” />
</androidx.constraintlayout.widget.ConstraintLayout>
But when Content growth Title is moved out of screen bounds. It looks like a bug. How can I avoid this behavior and make views to be inside screen bounds?
By default, Views that have wrap_content set for a dimension do not have their constraints enforced when the content gets too big to fit them. To change this behavior and limit the dimension you need to set app:layout_constrainedHeight="true" for your EditText.
As a side note, you should also avoid using match_parent for Views contained in ConstraintLayout as stated in the documentation.
I am hoping to position views within a parent view using percentages, similar to what is possible with absolute positioning in CSS. In my scenario, I will have a variable and unpredictable number of views that will be positioned this way.
Here is an example, which represents 3 TextViews inside a square with the following positions:
1: Top 55%, Left 29%
2: Top 77%, Left 58%
3: Top 54%, Left 43%
Is this best accomplished using custom drawing? Or is it possible to dynamically position views within a certain type of parent view given these percentages? If the former, how can I handle the text? And if the latter, what type of view should the parent be, and how should I go about setting these percentages?
You could use a ConstraintLayout
https://developer.android.com/training/constraint-layout/index.html
A constraint layout will allow you to position a view based on percentages like you want using horizontal and vertical bias. For example:
<android.support.constraint.ConstraintLayout
android:id="#+id/constraint"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:id="#+id/edition_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.33"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33"
android:background="#android:color/holo_purple"
android:text="text"/>
</android.support.constraint.ConstraintLayout>
Just make sure the child view (the Textview) is constrained to the top and bottom so you can set vertical bias. And also start, end so you can set horizontal bias.
The result is this:
And this can be done programmatically also, see here for a good guide:
http://www.zoftino.com/adding-views-&-constraints-to-android-constraint-layout-programmatically
Set the view according to X & Y position, use Android Absolute Layout here is the Example
Hope this will help you!!
So I have a UI element (a single line of text) that I want horizontally centered with respect to the overall device -- unless/until it collides with other UI elements in the given view group / layout. At that point I'd like it to be either centered in the space remaining or pegged as close to being centered overall as possible without colliding. [When there's finally not enough space, then I want to use ellipses.]
Is there any way to achieve this using just standard Android layouts?
I'm currently achieving this via code that adjusts layout constraints when the view group's width changes, the text changes, or related UI elements become visible/invisible. It works fine, but I can't help thinking that some layout should just do this for me.
You can use a weighted horizontal LinearLayout like this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="i am centered"
android:ellipsize="end"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="another widget"/>
.
.
.
</LinearLayout>
The TextView with width 0dp and weight 1 will use the remaining horizontal space.
You can add additional widgets to the LinearLayout, and the TextView will always take the remaining space.
For example, if you change the Visibility of the Button to GONE, you'll see the TextView will expand to use the whole width. Similarly, if you programmatically add new widgets to the LinearLayout, the available space for the TextView will adjust.
You can further add ellipsize options to control what happens when the text does not fit in the TextView size.
I am attempting to draw the medal overlaying the corner of the score screen shown here
There is an transparent layout holding the buttons and a RelativeLayout (black edge box), which holds the interior box (grey box) that contains the rest of the data. I tried adding the medal to the top left corner of the interior RelativeLayout and giving it negative margins, but that just cuts it off at the edge of the view. Adding it to the transparent layout puts it behind the corner.
How can I force the medal to overlay that corner? I'd prefer to do it in xml if possible, but any suggestions are welcome.
You can also achieve this by setting the android:clipChildren="false" and android:clipToPadding="false" attributes on the parent view (and if that doesn't work, set it on all the ancestor view groups as well, eventually it will work).
In the xml layout, an element that define after will overlay the element that define before.
So, you could change layout to something like this:
<LinearLayout>
<LinearLayout> <!--grey box --> </LinearLayout>
<ImageView src="medal" android:layout_marginLeft="-250dip"/>
<!-- change the amount of marginLeft to your desire -->
</LinearLayout>
merge them ...
http://developer.android.com/resources/articles/layout-tricks-merge.html would help