Home > OS >  LinearLayout - right-align an image
LinearLayout - right-align an image

Time:02-25

I have a LinearLayout with multiple TextView and one ImageView. The first TextView has an android:gravity="left" attribute. I need the last element - ImageView to align the image to the right side. If I set the attribute android:gravity="right" or android:layout_gravity="right" it doesn't work. How can I right-align an image?

enter image description here

my xml:

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        app:layout_constraintEnd_toEndOf="@ id/sampling_interval_slider"
                        app:layout_constraintStart_toStartOf="@ id/sampling_interval_slider"
                        app:layout_constraintTop_toBottomOf="@ id/sampling_interval_slider"
                        android:paddingEnd="@dimen/sampling_interval_labels_layout_padding"
                        android:paddingStart="@dimen/sampling_interval_labels_layout_padding">

                        <TextView
                            android:id="@ id/sampling_interval_2_min"
                            style="@style/default_work_mode_slider_label"
                            android:layout_weight="0.55"
                            android:gravity="left"
                            android:text="@string/sampling_interval_2_min" />

                        <TextView ... />

                        <TextView ... />

                        <TextView ... />

                        <TextView ... />

                        <TextView ... />

                        <ImageView
                            android:id="@ id/sampling_interval_continuous"
                            android:layout_width="0dp"
                            android:gravity="right"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.55"
                            android:src="@drawable/ic_infinity_loop" />
                    </LinearLayout>

CodePudding user response:

Wrap ImageView inside ViewGroup either LinearLayout or RelativeLayout

<LinearLayout
           android:id="@ id/sampling_interval_continuous"
           android:layout_width="0dp"
           android:gravity="right"
           android:layout_height="wrap_content"
           android:layout_weight="0.55">
     <ImageView
                 android:id="@ id/sampling_interval_continuous"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:src="@drawable/ic_infinity_loop" />

</LinearLayout>

Adjust the ImageView side according to your needs

  • Related