Android: ImageView in center screen - android

I have a background image in full screen with a RelativeLayout. And other image (100x100) in an ImageView.
I want to put the ImageView in center screen. But my image shows top left.
What is the problem?
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fondo_inicio" >
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/icono_inicio"/>
</RelativeLayout>

try this to your ImageView
android:layout_centerInParent="true"

Add the following to the ImageView.
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fondo_inicio" >
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:background="#drawable/icono_inicio"/>
</RelativeLayout>

<ImageView
android:id="#+id/pic_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="#drawable/pic" />

Try this
android:layout_gravity="center"

User layout_gravity and gravity doesn't work in case of RelativeLayout so just use
Layout_centerInParent = "true"

Related

Align image within imageView to bottom center

I have a simple imageView and I want an image to be displayed in the center of the bottom. At the moment it is displayed in the bottom right corner.
<ImageView
app:srcCompat="#android:drawable/simple_picture"
android:id="#+id/picture"
android:layout_centerHorizontal="true"
android:layout_height="300dp"
android:layout_width="330dp"
android:scaleType="fitEnd"/>
is there any way to use scaleType for bottom and center?
Thank you!
Add parent layout as Relative
<ImageView
android:id="#+id/picture"
android:layout_width="250dp"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
app:srcCompat="#drawable/oldman1" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:gravity="bottom|center">
<ImageView
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
app:srcCompat="#drawable/ic_chat" />
</LinearLayout>
Add another view as parent
You can take RelativeLayout as a parent layout and set in ImageView
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
try this ...
Look at answer for similar question. I think, using android:scaleType="matrix" can resolve problem.
If parent is linear layout then try :
android:layout_gravity="center"
If parent is relative layout then use :
android:layout_centerHorizontal="true"
I know from this question 5 years ago
But I wanted to help others
You do [[ NOT NEED TO USE scaleType ]] my friend
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center">
<ImageView
android:id="#+id/myimageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/MyImage" />
</RelativeLayout>

ImageView cannot be centered in a RelativeLayout

I've already tried adding layout_gravity, and gravity, and centerInParent.. but the image view still shows up at the left upper corner of the screen..Am I missing something?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/noHistroySign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/sign"
/>
....
I have tested your layout. Your ImageView is already in center. Its correct.
I think there is a problem in android:background="#drawable/sign" perhaps sign image is not displaying correctly or not found or not in drawable folder.
Try to change different image and other image will be in center perfectly.
Put this code in your imageview
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
In RelativeLayout using below property we centered ImageView or other child view.Just add below line in ImageView property.
android:layout_centerVertical="true"
remove
android:layout_gravity="center"
android:gravity="center"
and write
android:src="#drawable/sign"
inplace of
android:background="#drawable/sign"
Try this
<RelativeLayout 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"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/sign" />
</RelativeLayout>
Or try this
<RelativeLayout 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:gravity="center"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sign" />
</RelativeLayout>

ImageButton in Android xml (image is too small and horrible resolution)

I have the following for 3 Imagebuttons. The way the code stands below, the three buttons look the same.
Problems:
The image doesn't fill the size of the button completely
The resolution of the images are bad. I have the images saved in all res/drawable folders (drawable-hdpi, mdpi, etc). Images were loaded by "Android Icon Set" wizard.
Any ideas? I have been playing with padding and scaleType as I saw them as the solutions on other posts.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/abutton"
android:src="#drawable/a_button_icon"
android:layout_height="150dp"
android:layout_width="150dp"
android:padding="5dp"
android:scaleType="fitXY"/>
<ImageButton
android:id="#+id/button"
android:src="#drawable/b_button_icon"
android:layout_height="150dp"
android:layout_width="150dp"
android:scaleType="fitXY"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp">
<ImageButton
android:id="#+id/cbutton"
android:src="#drawable/d_icon"
android:layout_height="150dp"
android:layout_width="150dp"
android:padding="20dp"
android:scaleType="fitXY"/>
<ImageButton
android:id="#+id/dbutton"
android:src="#drawable/about_a_button"
android:layout_height="150dp"
android:layout_width="150dp"
android:padding="20dp"
android:scaleType="fitXY"/>
</LinearLayout>
</LinearLayout>
Instead of assigning drawable to the android:src attribute assign it to android:background attribute of the ImageButton.
OR
You can try setting android:adjustViewBounds="true", this will make the ImageButton adjust its bounds to preserve the aspect ratio of its drawable
PS: If you set an image to be the background of your ImageButton, then the image will scale to whatever size the ImageButton is. Other than that, src is a foreground image and background is a background image.
Edit:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- The two linear layouts will have equal widths. -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp" >
<!-- The two image button will fill the linear layout along width and have height 150dp -->
<ImageButton
android:id="#+id/abutton"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/engage_down" />
<ImageButton
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/engage_down" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp" >
<ImageButton
android:id="#+id/cbutton"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/engage_down" />
<ImageButton
android:id="#+id/dbutton"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/engage_down" />
</LinearLayout>
</LinearLayout>
Hope this helps.
Try
by removing padding and set background as 'null'.
In case of resolution, I think it was taking low resolution image.
<ImageButton
android:id="#+id/dbutton"
android:src="#drawable/about_a_button"
android:layout_height="150dp"
android:layout_width="150dp"
android:background="#null"
android:scaleType="fitXY"/>
I faced the same problem. No other worked for me. At last it was a simple issue. By default, padding is existing around ImageButton. so use:
<ImageButton>
...
android:padding="0dp"
...
</ImageButton>
It will fix the issue.

Align ImageView to bottom Right of LinearLayout

I'm triying to put an ImageView in bottom right of LinearLayout ....
I wrote the code as bellow :
style.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/my_background"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="#drawable/my_small_image" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This code results in the ImageView 'my_small_image' at the bottom right, but I need it in the bottom left.
Anyone have any ideas about this?
I hope somebody here can help me.
Best regards and thanks in advance, Fadel.
Using LinearLayout is possible:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical">
<!-- other elements in linear layout here -->
<!-- making a RelativeLayout un-desirable -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="fitEnd"
android:src="#drawable/your_image" />
</LinearLayout>
This will position the image at the center-bottom, for bottom-right use
android:layout_gravity="right"
Use RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/my_background"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/my_small_image" />
</RelativeLayout>
In LinearLayout you can use -
android:layout_gravity="bottom|right"
I would recomend Using RelativeLayout but if you have to useLinearLayout i know that android:scaleType" can help center or move image, did you try android:scaleType="fitEnd" or android:scaleType="fitXY"
for more Try This

how i place image on center of screen?

this is my code which place image on top senter of screen how i place image on center of screen? not top center my screen look like this http://imgur.com/65CD6
i wanna place on center of screen not op center
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#ffffff"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView android:id="#+id/ImageView01"
android:src="#drawable/agapplogo"
android:layout_width="wrap_content"
android:layout_marginLeft="60dip"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/TextView01"
android:text="#string/welcome"
android:layout_below="#id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
You can try to use: android:layout_centerInParent="true" like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
You can either play with android:gravity="center" in <RelativeLayout>
OR
include android:centerInParent="true" inside ImageView
place android:gravity="center" for RelativeLayout
Just give your ImageView in xml this attribute:
android:layout_centerHorizontal="true"
this may happen due to RelativeLayout,try using LinearLayout

Categories

Resources