1

I have an ImageView inside Linear Layout and this LinearLayout is inside RelativeLayout and this Relative layout is inside ScrollView.

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/white"
android:fillViewport="true"
android:tileMode="repeat" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <LinearLayout
        android:layout_alignParentTop="true"
        android:layout_width="fill_parent" 
        android:layout_height="30dip"
        android:background="@drawable/black"
        android:tileMode="repeat">
        <ImageButton 
            android:id="@+id/buttonMainBackClosetheApp"
            android:src="@drawable/greenarrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black"
            android:tileMode="repeat"/>
      <ImageView 
          android:src="@drawable/logo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
         </LinearLayout>

I want to set the ImageView logo to centre of the LinearLayout. I have attached part of the xml which has the ImageView. I have used ScrollView as such if the application is used in small screens then user can scroll. Full XML is too big as it has been designed on dashboard style. Looking forward to your reply. thanks.

Mukunda
  • 1,593
  • 3
  • 26
  • 45

11 Answers11

0

Place android:gravity="center" in the LinearLayout. So that the ImageView will come to center.Refer the following code.

<?xml version="1.0" encoding="utf-8"?>    

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/white"
android:fillViewport="true"
android:tileMode="repeat" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >    

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/black"
        android:tileMode="repeat"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/buttonMainBackClosetheApp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black"
            android:src="@drawable/greenarrow"
            android:tileMode="repeat" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/images"/>
    </LinearLayout>
   </RelativeLayout>
 </ScrollView>
Booshan
  • 97
  • 1
  • 3
  • 11
0

Place attribute gravity for linear layout to center. android:gravity="center" If it will not work then pls upload your full xml.

Pradeep
  • 104
  • 4
0

I hope that will help you.

    <LinearLayout
            android:layout_alignParentTop="true"
            android:layout_width="fill_parent" 
            android:layout_height="30dip"
            android:background="@drawable/black"
            android:tileMode="repeat"  
            android:orientation="vertical"
            android:layout_centerHorizontal="true">
            <ImageButton 
                android:id="@+id/buttonMainBackClosetheApp"
                android:src="@drawable/greenarrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/black"
                android:tileMode="repeat"
                android:layout_gravity="center"/>
          <ImageView 
              android:src="@drawable/logo"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"/>
 </LinearLayout>
hardramoliya
  • 163
  • 1
  • 2
  • 13
0

You shoud use root layout inside ScrollView is RelativeLayout and use

android:layout_centerInParent="true"

in the view's xml you want to be in center under RelativeLayout

Nilesh
  • 1,013
  • 14
  • 21
0
<ImageView 
      android:src="@drawable/logo"
      android:layout_width="wrap_content"
      android:layout_gravity="center"
      android:layout_height="wrap_content"/>
Sunil_Suthar
  • 1,094
  • 1
  • 10
  • 20
0

Place the following attribute in LinearLayout android:layout_gravity = "center"

The above attribute will place your child views at the center in LinearLayout.

SAMD
  • 421
  • 1
  • 3
  • 7
0

How about this one?

<ImageView 
    android:src="@drawable/logo"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_height="wrap_content"/>
ramdroid
  • 6,738
  • 1
  • 16
  • 14
0

Maybe it is because you have the ImageButton within the same LinearLayout with ImageView.
So they share the same space and when you "center" ImageView then it is centered in its own space. If you want to "center" it horizontally for example, add attribute android:orientation="vertical" and then try to center it. Or put ImageButton and ImageView in seperate LinearLayouts.

tioschi
  • 389
  • 1
  • 3
  • 11
0

try this

 <ImageView
       android:id="@+id/logo"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"            
       android:layout_centerHorizontal="true"           
       android:background="@drawable/logo" />

see this

application name center alignment in android`

Community
  • 1
  • 1
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
0
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:tileMode="repeat" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <LinearLayout
        android:layout_alignParentTop="true"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:gravity="center"
        android:tileMode="repeat">
        <ImageButton 
            android:id="@+id/buttonMainBackClosetheApp"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:tileMode="repeat"/>
      <ImageView 

          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
         </LinearLayout>
         </RelativeLayout>
         </ScrollView>
SuN
  • 1,723
  • 1
  • 13
  • 14
0

may be this might help you.

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:tileMode="repeat" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:tileMode="repeat" >

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LINEAR LAYOUT"
                android:textSize="20dip" />

            <ImageButton
                android:id="@+id/buttonMainBackClosetheApp"
                android:layout_width="50dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="#FFFFFF"
                android:tileMode="repeat" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LINEAR LAYOUT"
                android:textSize="20dip" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>
Shri
  • 149
  • 2
  • 4
  • 10