In the image you can see a graphic (the top one) which shows what my layout looks like in design view . The graph below is the result when running the app on my device . I don't know why this "narrowing" happens.
I attach the xml code of the views of the recycler view
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:text="Texto 1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/ayuda"/>
<TextView
android:id="@+id/txt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Texto 2"
app:layout_constraintEnd_toStartOf="@+id/imgbt"
app:layout_constraintStart_toEndOf="@+id/txt_1"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/ayuda"/>
<ImageButton
android:id="@+id/imgbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:src="@drawable/icon_negro"
app:layout_constraintEnd_toStartOf="@+id/imgbt_reservar_item_row_recycler_dias"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="button"
android:textColor="#ffffff"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I would like you to tell me what I should modify so that the views of the Recycler View adapt to the width of the screen or at least to what I indicate in the XML code
Thank you very much for reading.
The thing is that you must define the rules of the ConstraintLayout correctly. When you have views which you want to adapt to the limits of the screen, with different proportions, you must define the rules so that each one meets the conditions you want.
In your case, it seems that you want to establish percentages or sizes according to your perception, therefore, it is important to be clear about the rules that you are going to define.
For example, let's say you want to create a view with the following proportions in your ConstraintLayout :
Two views with equal proportions across the full width of the container:
Your children of the ConstraintLayout should have rules defined like this:
IMPORTANT NOTE: The views that are defined with
android:layout_width="0dp"
insideConstraintLayout
indicate that they will take all the available space, for the assigned restrictions.view1: says that it has a restriction at the beginning with the parent (it will start to be drawn from there). Constraint at the end with
view2
(will end where it beginsview2
).view2: says that it has a start restriction at the end of view1 (it will start drawing where view1 ends). Restriction at the end with parent (it will end where the parent ends, that is, at the end of the container).
Logically, they are two views that are compacted in the same space, therefore, it will take all that available space that compacts them and will divide them into two equal parts, so that the result is two views that occupy 50% of the total width of the container each.
Likewise, if you want to have several views in a container that occupy equal parts except for one that you define a size, simply define the necessary rules to comply with the restrictions and assign your particular view a specific size:
Example:
So you would have something like this in code:
Solution
In order for you to arrive at your solution, you must define all the necessary rules to fulfill your constraint. You only have some defined, but not all of them to meet the design you want. For example, in your
Button
you haveapp:layout_constraintEnd_toEndOf="parent"
, but where is the rule ofapp:layout_constraintStart_toStartOf
?. Similarly, the other views lack rules. I leave it for homework, happy coding!.I think I can help you. I tried something similar to what you say you have and it takes up as much space as possible thanks to chains . Get something like this.
If you modify the margins a little you can see that it occupies all the space. This property is in the Android Studio designer. As you can see in the photo there is no horizontal since I added it to make it occupy all the space but it should come out without problems.