Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

LinearLayout – right-align an image

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                    <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>

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading