Android: create space-around (equal space) between elements and borders - android

I have two imageView widgets and I would like to place them so that there is equal space between the left border and the left image, the left image and the right image, and the right image and the right border. (I wrote in the title of the question space-around because it's similar to the spacing used in CSS...)
Is it possible to achieve without manual adaptation (such as calculating margins)? And is it possible to achieve solely in the design mode without writing xml code?
Screen shot:
My XML:
<?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"
android:background="#drawable/newbackground"
tools:context=".MainActivity">
<Button
android:id="#+id/btnRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#3498db"
android:text="#string/btnRoll"
android:textColor="#ffffff"
tools:layout_editor_absoluteX="167dp"
tools:layout_editor_absoluteY="621dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#+id/imageView3"
app:srcCompat="#drawable/dice1"
tools:layout_editor_absoluteX="59dp" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/dice2"
tools:layout_editor_absoluteX="267dp"
tools:layout_editor_absoluteY="406dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

Try This
<?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"
android:background="#drawable/newbackground"
tools:context=".MainActivity">
<Button
android:id="#+id/btnRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#3498db"
android:text="btnRoll"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/btnRoll"
app:layout_constraintEnd_toStartOf="#+id/imageView3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/blue_heart" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
app:layout_constraintBottom_toTopOf="#id/btnRoll"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:srcCompat="#drawable/blue_heart" />
</androidx.constraintlayout.widget.ConstraintLayout>

Here is a proposition with a LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView ... />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView ... />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView ...
</LinearLayout>
With the layout_weight attribute, the "spacer" views will take the maximum of free space, equally

Related

How to add icon and a line following to next icon vertically

It will be better to know if it is possible to do in XML using Linear layout or other layout
You can use Relative,Linear or Constraint
Try inside Constraint layout
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivPlaceHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="#drawable/ic_baseline_library_music_24"/>
<View
android:id="#+id/viewLine"
android:layout_width="1dp"
android:layout_height="50dp"
android:background="#color/teal_200"
app:layout_constraintBottom_toTopOf="#+id/ivPlaceHolder1"
app:layout_constraintEnd_toEndOf="#+id/ivPlaceHolder"
app:layout_constraintStart_toStartOf="#+id/ivPlaceHolder"
app:layout_constraintTop_toBottomOf="#id/ivPlaceHolder" />
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivPlaceHolder1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/viewLine"
android:src="#drawable/ic_baseline_library_music_24"/>
Linear Layout Code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="12dp"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView1"
android:layout_width="32dp"
android:layout_height="32dp"
android:contentDescription="#null"
android:src="#drawable/ic_android_black_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/viewLine"
android:layout_width="2dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:background="#33E24D" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="32dp"
android:layout_height="32dp"
android:contentDescription="#null"
android:src="#drawable/ic_android_black_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
You can obtain the required result with the help of LinearLayout. Change the orientation to vertical in the LinearLayout.
android:orientation="vertical"
You can use View for obtaining the straight line between two images.
This is a sample code for the same. Hope this works for you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_launcher_background" />
<View
android:layout_width="3dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:background="#color/black" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_launcher_background" />
</LinearLayout>

Making ImageView take up size of container

I'm trying to create the following layout:
Where after a certain device width, the container will only be a certain width and will not increase in size anymore, here it is set at 400dp. If the device is less than 400dp, then it will get the width of the device instead. The imageviews inside, should be half the width of the container. The container should also be centered after 400dp.
My current code doesn't center the container and it chops off the second image and the text on the right. How can I fix this?
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:maxWidth="400dp">
<androidx.cardview.widget.CardView
android:id="#+id/postCard"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/postContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/postAuthorAvatar"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/landscape"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/postAuthorUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\#username"
app:layout_constraintBottom_toBottomOf="#id/postAuthorAvatar"
app:layout_constraintLeft_toRightOf="#id/postAuthorAvatar"
app:layout_constraintTop_toTopOf="#id/postAuthorAvatar" />
<TextView
android:id="#+id/postDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21 Sept 2021"
app:layout_constraintBottom_toBottomOf="#id/postAuthorAvatar"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#id/postAuthorAvatar" />
<TextView
android:id="#+id/postTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/postAuthorAvatar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/postTitle">
<ImageView
android:id="#+id/postImgA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="2.5dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/landscape"/>
<ImageView
android:id="#+id/postImgB"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2.5dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/landscape"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Use a Guideline in the project which runs through the middle of the layout
Code Snippet
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
<ImageView
android:id="#+id/postImgA"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
android:layoutMargin ="16dp"
app:layout_constraintStart_toStartOf="#id/guideline"
android:scaleType="centerInside"
android:src="#drawable/landscape"/>
<ImageView
android:id="#+id/postImgB"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="#id/guideline"
android:layoutMargin ="16dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/landscape"/>
then attach the images to the guideline and then to the parent give a margin to each. Use "0dp" or match constraints for it to mimic the image.

How to make this design with Cardview

i'm trying to make this design in xml but i'm not sure if it's possible what I want.
I want to have a box and three icons (generated for every cardview) on the right separated in two layouts but all in the same cardview.
Is it possible or do I have to use the cardview as the first box and put the buttons separately?
Hope the following code helps you ,
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="1dp"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent=".8"/>
<ImageView
android:id="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/guideline"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/actionLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintLeft_toRightOf="#+id/guideline"
app:layout_constraintRight_toRightOf="parent" >
<Button
android:id="#+id/likeButton"
style="?attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="100" />
<Button
android:id="#+id/disLikeButton"
style="?attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="100" />
<Button
android:id="#+id/optionButton"
style="?attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:iconGravity="end" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Using wrap_content without content going of the screen and without using layout_weight

So I have something like this:
LinearLayout:
LinearLayout - wrap_content, background black:
TextView - wrap_content
LinearLayout:
Image - 20dp
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center|start"
android:padding="5dp"
android:layout_margin="2dp">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="klgjdkljgilfdjgidfhjiogjdio;jhgiju;fgjdhjgu;hdfuighdfuk;ghduk;fhgukldhfgujkdfhyufhglfdhguldfhguklhfdulghldfughyulfhdulghdfulghul"
android:textColor="#D5D5D5" />
<LinearLayout
android:layout_width="15dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginBottom="2dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="bottom|center"
android:src="#drawable/edit" />
</LinearLayout>
</LinearLayout>
This works perfectly but when the TextView contains text larger than the screen size, the image gets out of the screen. I don't want that to happen.
Here's the example:
Does anyone know how to fix this so it doesn't go outside the screen?
Thanks!
Try this with ConstraintLayout
<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/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="klgjdkljgilfdjgidfhjiogjdio;jhgiju;fgjdhjgu;hdfuighdfuk;ghduk;fhgukldhfgujkdfhyufhglfdhguldfhguklhfdulghldfughyulfhdulghdfulghul"
android:textColor="#D5D5D5"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="#id/image"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="#+id/image"
android:layout_width="20dp"
android:layout_height="10dp"
android:layout_gravity="bottom|center"
android:src="#drawable/edit"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/message"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

RelativeLayout works, whereas ConstraintLayout Fails

Here is the code from RelativeLayout,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:gravity="center">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:minWidth="200dp"
android:minHeight="200dp"
android:src="#drawable/ic_baseline_description_24px"
android:tint="#color/grey" />
</RelativeLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/txtInputLayoutCaption"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="#dimen/dp8"
android:layout_toStartOf="#+id/imgSend"
android:hint="#string/caption">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editTextCaption"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/trebuchet" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="#+id/imgSend"
android:layout_width="#dimen/dp36"
android:layout_height="#dimen/dp36"
android:layout_alignTop="#+id/txtInputLayoutCaption"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_baseline_send_24px"
android:tint="?attr/colorPrimary" />
</RelativeLayout>
RelativeLayout Output
And here is the constraint layout code,
<?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">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:minWidth="200dp"
android:minHeight="200dp"
android:src="#drawable/ic_baseline_description_24px"
android:tint="#color/grey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/txtInputLayout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/dp8"
android:hint="#string/caption"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imgSend"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="#+id/imgSend"
android:layout_width="#dimen/dp36"
android:layout_height="#dimen/dp36"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_baseline_send_24px"
android:tint="?attr/colorPrimary"
app:layout_constraintBottom_toBottomOf="#+id/txtInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/txtInputLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the output for constraint layout code.
Note: I'm using androidx.constraintlayout:constraintlayout:2.0.0-beta2
Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.
Rules remind you of RelativeLayout, for example setting the left to the bottom of some other view.
app:layout_constraintBottom_toBottomOf="#+id/view1"
Unlike RelativeLayout, ConstraintLayout offers bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.
app:layout_constraintHorizontal_bias="0.33" <!-- from 0.0 to 1.0 -->
app:layout_constraintVertical_bias="0.53" <!-- from 0.0 to 1.0 -->
Give send button start constraint to txtInputLayoutCaption
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:minWidth="200dp"
android:minHeight="200dp"
android:src="#drawable/ic_share_active"
android:tint="#color/gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/txtInputLayout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:hint="caption"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imgSend"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="#+id/imgSend"
android:layout_width="36dp"
android:layout_height="36dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_send"
app:layout_constraintBottom_toBottomOf="#+id/txtInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/txtInputLayout"
app:layout_constraintTop_toTopOf="#+id/txtInputLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>

Categories

Resources