Issue
This is the code that I have written so far for each card that I am doing for a carousel. Also, not sure whether to use ImageButton or Button as I am trying to do a '-'/'+' button for users to press on.
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="22dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- ImageView for images placed above the text-->
<ImageView
android:id="@+id/imageIv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop" />
<!-- TextView is right below the image-->
<TextView
android:id="@+id/flavorTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Flavor 1"
android:textSize="15sp" />
<!-- Trying to place the button on the same row as the TextView-->
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
Solution
Replace your inner LinearLayout with a ConstraintLayout so you will be able to position views depending on constraints you define. Like in the following example:
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="22dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- ImageView for images placed above the text-->
<ImageView
android:id="@+id/imageIv"
android:layout_width="0dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<!-- TextView is right below the image-->
<TextView
android:id="@+id/flavorTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Flavor 1"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/imageIv"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<!-- Trying to place the button on the same row as the TextView-->
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
app:layout_constraintTop_toBottomOf="@id/imageIv"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/flavorTv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
Answered By - memres
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.