ConstrainLayout not respecting the hierarchy - java

I have this layout
....................
<CheckBox
android:id="#+id/cb_remember_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textinput2"
android:text="#string/keep_login"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_marginTop="14dp"
app:layout_constraintTop_toBottomOf="#+id/textinput2"
app:layout_constraintBottom_toTopOf="#+id/btn_signin_login" />
<Button
android:id="#+id/btn_signin_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/cb_remember_login"
android:backgroundTint="#color/colorAccent"
android:minHeight="75dp"
android:text="#string/signin"
android:textColor="#android:color/background_light"
android:textSize="30sp" android:visibility="visible"
android:layout_marginTop="36dp"
app:layout_constraintTop_toBottomOf="#+id/cb_remember_login"
app:layout_constraintBottom_toTopOf="#+id/tv_forgot_login"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<include
layout="#layout/view_circle_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#+id/btn_signin_login"
app:layout_constraintBottom_toBottomOf="btn_signin_login"
app:layout_constraintLeft_toLeftOf="#+id/btn_signin_login"
app:layout_constraintRight_toRightOf="#+id/btn_signin_login" />
<TextView
android:id="#+id/tv_forgot_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_signin_login"
android:text="#string/forgot_pass"
android:textAlignment="center"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_marginTop="14dp"
app:layout_constraintTop_toBottomOf="#+id/btn_signin_login"
app:layout_constraintBottom_toTopOf="#+id/tv_copyrights_login"
tools:layout_editor_absoluteX="0dp" />
...........][1]][1]
Here is view_circle_progress.xml
<ProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:indeterminateTint="#color/red">
</ProgressBar>
You can see my included layout comes after the button but in the picture you can see it's actually beneath the button. Please help with a solution. Also except the included layout all the views are in a chain.

Go to your xml, click design. The on the upper left, corner you will see an icon similar to a blueprint. Click on it and you will get your layout as a blue print. Then click on the element that you want, you will see four points on all sides. Click on the upper point and drag it to the upper bar(menu, etc.). That way you will attach it and it will stay in place. If you want to make offset look on you right above the properties you will see a box representing the item with its constraints, just put the distance you want, and you will have your offset.

The problem was in Android 5.0 elevation is introduced. A button has a default elevation that's why it was always showing on top. Setting elevation value to my include layout solve the problem.

Related

How do make layout in android studio?

I am trying to make an activity which should look like the following:
here
The problem is that it does not get the same when I start the app. The buttons are cut or the text is cut. And if I change the position of one button, everything gets in the upper-left-corner.
My question is: What do I have to do that everything gets the position I want?
I used a Relative Layout. Maybe this is the problem?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button3"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_below="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="83dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="83dp"
android:layout_marginBottom="238dp"
android:text="Info"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<Button
android:id="#+id/button"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_centerInParent="true"
android:text="Kartenansicht"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_above="#+id/button2"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="71dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="73dp"
android:layout_marginBottom="18dp"
android:text="Um zu überprüfen, ob Sicherheitskameras in der Nähe sind, bitte hier drücken:"
android:textSize="20sp" />
<Button
android:id="#+id/button2"
android:layout_width="244dp"
android:layout_height="62dp"
android:layout_above="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="83dp"
android:layout_marginEnd="84dp"
android:layout_marginBottom="24dp"
android:text="Start"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
</RelativeLayout>
You have a big mess in your layout. For example:
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
These 2 parameters can't co-exist.
For your desired layout you should use LinearLayout with Orientation set to vertical. That way, all you'll need to do is write your UI objects 1 below the other and they will be placed 1 above the other. To center them, you can then use gravity and layout_gravity
android:layout_gravity="center"
android:gravity="center"
And that should be all.
Edit:
Just for you to know, Relative layout basically places any given object at position 0,0 of the layout. If you create few objects, they will be placed one on top of the other.
use the constraintlayout and for example you give each view a constraint of start and end after you can set the width to 0dp
if you would like to create the design by dragging and dropping then it would be better to use constraint layout instead of relative layout, but if you like to use relative layout anyway, you should use "centerInParent = "true" "in the middle button and set "layout_above = "#+id/your_centered_view_id" for the above button and "layout_below = "#+id/your_centered_view_id" ".

How to curve a small part in a circular cardview in android to show the online status icon occupy at the bottom

I have used circular cardview to show the user status as online. But the circular has a small curve to occupy the status enable icon.
How can I acheive this in android. My code shows the circular image.
<androidx.cardview.widget.CardView
android:id="#+id/myCardView"
android:layout_width="70dp"
android:layout_height="70dp"
app:cardCornerRadius="50dp"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
>
</androidx.cardview.widget.CardView>
<ImageView
android:id="#+id/user_online_status"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_gravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="5dp"
android:layout_marginBottom="0dp"
android:visibility="visible"
android:src="#drawable/circle_green_posts"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|start"
android:layout_marginTop="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Username">
</TextView>
If I understood you question correctly, all you need to do is to put this inside some kind of parent layout (Relative, Constraint). Then simply put this circular shape to the bottom right, and above the card view.

How do I align the text in the text-area on bottom?

I want to align the text of this two labels on it's bottom, so the two values are aligned on the same height.
Sadly, I dont find an option like aligning it inside its own area.
gravity wont work. It has to be in xml.
Thank you!
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
If you want to set your textview text bottom
android:gravity="bottom"
or
if you want to set your whole component bottom
android:layout_gravity="bottom"
To align the text inside a TextView to its bottom, you can use
android:gravity="bottom"
(inside TextView tags)
You can change the gravity to bottom like below.
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:gravity="bottom"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
And also you can try android:gravity="bottom" to the layout you have used to hold the both TextView and change the 2nd TextView height into wrap_content.
Hope you got the answer.

ConstraintLayout applying margin bottom to a view, also applies it to the top of the view

I have a TextView, below it an EditText and below it another TextView. When I apply a bottom margin of 12 dp to the EditText, it's applied but it also applies to the top of the view. I don't know if this is the way ConstraintLayout works. marginTop works as expected.
https://i.stack.imgur.com/VEOUz.jpg
From what i can see in your attachment you are creating a vertical chain but you didn't restrain your "description" to the bottom. This could cause issues since the constraint layout doesn't know where the chain ends. make sure you add app:layout_constraintBottom_toBottomOf="parent" or whatever view you need as a bottom chain constraint on the description TextView.
I recreated your example and there is mirror margin for the edit text.
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
app:layout_constraintBottom_toTopOf="#id/editText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" />
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="edit text"
app:layout_constraintBottom_toTopOf="#+id/description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/name" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:textColor="#color/colorTextPrimaryLight"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
You can play around with the chainStyle to see which fits best your needs.

Customizing the buttons inside a HorizontalScrollView

I want to design a simple interface for a calculator. The calculator has 2 group of keys, the basic keys and the extra keys. I want to put the extra keys on a HorizontalScrollView. Here is the xml file:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="J" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="K" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="L" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M" />
</LinearLayout>
</HorizontalScrollView>
Here is how it looks like:
However, it works very well and I can scroll horizontally and click on any button I want. But I want to customize it more. So here is my questions:
How can I edit the width of each button so that only 5 buttons can be displayed on the screen at a time.
How can I make the scrolling by 5 buttons offset, i.e if I have the leftmost 5 buttons appear on the screen and I scroll to the right, those 5 buttons will be gone to the left and the next 5 buttons will be shown.
How to disappear the scrollbar (i.e scrolling with invisible scrollbar).
Thank you :)
if you add android:weightsum="5" to your linearlayout and then add android:layout_weight="1" to each of the buttons I think it will put 5 of them on the screen at a time. However I am not very good at using the weight attribute, anytime I use it I end up with a bunch of guess and check, however my gut tells me that it is possible to achieve what you are after with weight and weightsum somehow.
However, I do wonder does it make sense to always show 5 buttons no matter what screen you are on? To me it seems like you may want to vary it at least some so that maybe the smallest of screens only get 4 buttons, and the widest of screens might get 6, otherwise you are going to end up with a very wide range of button sizes across all of the different device densities.
add this to your HorizontalScrollView:
android:scrollbars="false"
that should take care of the scroll bar for you.
As for the part about scrolling 5 at a time, Im afraid to implement this you might have to manually handle it with the horizontalScrollView.smoothScrollBy() method, you'll have to make it do the calculations to figure out how far it needs to go in order to pass 5 buttons. Then override your onTouch listener to make the smoothScrollBy() call for you.
smoothScrollBy() - its worth looking at smoothScrollTo() also.

Categories