'How do i center this image inside my view?
I am simply trying to center my imageview10 inside my color view how do i go about doing this? I'm not used to using linear layout so i am not too familiar on how to do this can someone help.
<?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:orientation="vertical"
android:padding="10dp">
<View
android:id="@+id/colorView"
android:layout_width="150dp"
android:background="@color/outlineColor"
android:layout_height="150dp"
/>
<TextView
android:id="@+id/productNames"
android:layout_width="wrap_content"
android:text="Grocery"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<ImageView
android:id="@+id/imageView10"
android:layout_width="100dp"
android:layout_height="100dp"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>
Solution 1:[1]
You just need to use android:layout_gravity="center_horizontal" to center the ImageView. You also need to make the LinearLayout's width to match the parent using this attribute android:layout_width="match_parent"
This is the code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp">
<View
android:id="@+id/colorView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:background="@color/outlineColor"
/>
<TextView
android:id="@+id/productNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Grocery"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageView10"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tonnie |
