1

Been switching from linear layout and relative layout, because I wanted to attain the centering of the image.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:scrollbars="none"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
        android:id="@+id/itemItem"
        android:src="@drawable/content_picture"
        android:tag="image_item_grid_image"
        android:layout_width="wrap_content"
        android:background="@drawable/layout_bg"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center|center_horizontal"
        android:adjustViewBounds="true"
        android:contentDescription="Desc"
        android:scaleType="fitCenter" />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <RatingBar
            android:id="@+id/ratingBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout >

</ScrollView>

Display Image:

            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int height = displaymetrics.heightPixels;
            int width = displaymetrics.widthPixels;


            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            params.height = height / 2;
            params.width = width / 2;
            params.setMargins(0, 50, 0, 0);
            //mImageView.get().setImageBitmap(bitmap.get());

            if(bitmap!=null){

            Log.d("#ImageValue: ", ""+bitmap.toString());
            mImageView.get().setImageBitmap(bitmap.get());
            mImageView.get().setLayoutParams(params);

            }

The image is not centering for some reason, and I tried as much as I know but seems there is lacking.

enter image description here

rahstame
  • 2,148
  • 4
  • 23
  • 53

3 Answers3

1

Try this..

Remove gravity add android:layout_centerHorizontal="true" and android:layout_centerVertical="true"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
        android:id="@+id/itemItem"
        android:src="@drawable/content_picture"
        android:tag="image_item_grid_image"
        android:layout_width="wrap_content"
        android:background="@drawable/layout_bg"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:contentDescription="Desc"
        android:scaleType="fitCenter" />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <RatingBar
            android:id="@+id/ratingBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout >

</RelativeLayout>

EDIT:

add below lines also in your params

params.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • still not working sir. tried your instructions..I also use `layoutParams` at the bottom part – rahstame Nov 22 '13 at 11:08
  • @lordzden use `RelativeLayout` insteadof `ScrollView` – Hariharan Nov 22 '13 at 11:16
  • I replaced it with your code, still the image stays as the picture shows. Does my handling of the layout programmatically affects the xml? please see the `layoutParams code`. – rahstame Nov 22 '13 at 11:23
  • Maybe try removing your layoutParams code and seeing if it fixes the issue. I don't see why it would affect it though. – Sean Croft Nov 22 '13 at 11:31
  • The addrule does the trick!!! oh my, I did not see it commin'. Can you share why the addrule works? :) – rahstame Nov 22 '13 at 11:48
  • @lordzden there is no wrong in xml coding then i check the `layoutParams` that haven't set any **position** for the imageview. i traid that and posted to you. – Hariharan Nov 22 '13 at 11:51
0

Your Relative Layout is set to:

<RelativeLayout 
    android:layout_width="wrap_content" // will not fill parent so children will centre in relative layout, not the whole view
    android:layout_height="wrap_content"
    android:orientation="vertical" > 

you should either set the Relative Layout to fill parent for width and height or set the Scroll Views layout_gravity to center so that the Relative Layout centers within the scrollview.

Sean Croft
  • 375
  • 1
  • 2
  • 8
  • I did try adding gravity center on scrollview, still the image is not centering, by the way I have layout params at the part. please see. – rahstame Nov 22 '13 at 11:11
  • You tried adding layout_gravity as well right, which is different from gravity. – Sean Croft Nov 22 '13 at 11:19
0

Try using

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

instead of

<RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"

And comment out the logic to centre image view programmatically.

Prashant Thakkar
  • 1,383
  • 1
  • 12
  • 15