Matteo Mannino
Matteo Mannino

Reputation: 399

Centering an ImageView in FullScreen mode

I am simply trying to center an ImageView in a FullScreen app. I've read several posts on this site on people asking what seems like same question, and I've tried everything I've read, but I can't seem to get it to center.

Here is my code for the layout. I realize there might be some overkill here to get the centering, as a result of trying various other posts. This code puts the image on the top left part of the screen.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/RelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:layout_gravity="center"
    android:background="#000000"
    android:orientation="vertical" >

    <ImageView
       android:id="@+id/image"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_alignParentTop="false"
       android:layout_centerInParent="true"
       android:layout_gravity="center"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:adjustViewBounds="true"
       android:background="@android:color/black"
       android:contentDescription="@string/descImage"
       android:cropToPadding="false"
       android:gravity="center"
       android:src="@drawable/placeholder" />

Here is my full screen code in AndroidManifest.xml.

android:screenOrientation="landscape"
android:theme="@style/Theme.FullScreen">

Theme.FullScreen is defined as

<style name="Theme.FullScreen" parent="@android:style/Theme.Holo">
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowFullscreen">true</item>
</style>

Thanks.

Upvotes: 0

Views: 1242

Answers (5)

Dagmar
Dagmar

Reputation: 3271

fill_parent has been replaced with match_parent in later Android versions and I've using an SVG drawable resource, so I had to modify the answers above slightly.

This is what worked for me:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.MyActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/my_drawable" />
</android.widget.RelativeLayout>

Upvotes: 0

jph
jph

Reputation: 2233

Use scaleType="fitCenter" on the ImageView.

Upvotes: 0

Chansuk
Chansuk

Reputation: 792

Try this. It works find on my galaxy nexus w/ 4.2 (I just changed width & height of image view as wrap_content and remove unnecessary & meaningless attributes.)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/RelativeLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#000000">

    <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:background="@android:color/black"
            android:src="@drawable/ic_launcher" />
</RelativeLayout>

and for theme...

<style name="Theme.FullScreen" parent="android:Theme.Holo.NoActionBar.Fullscreen"/>

Upvotes: 1

Matt Clark
Matt Clark

Reputation: 28609

If you use fill_parent for the size, the entire view will not be centered in the parent, because it is the size of the parent.

If you change the size to wrap_content, the bounding box of the view will change, and that will be centered inside of the parent.

You could also use the android:scaleType property for the imageView specifying how the image resource itself should be layed out inside of the view.

The default scaleType will cause the resource image to appear in the top left corner of the ImageView, if the bouding box is larger then the actual image itself.

You can also use the android:adjustViewBounds property to have the OS automatically resize the bounding box for you, based on the image resource.

Upvotes: 0

sddamico
sddamico

Reputation: 2130

That seems very excessive for what you're trying to do. Try this?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/descImage"
        android:src="@drawable/placeholder" />

</FrameLayout>

Upvotes: 0

Related Questions