1

I am trying to place my custom view horizontally centered on a Relative Layout. However it does not appear centered, instead it is close to the left of the screen.

Below is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5sp"
    tools:context=".FinalActivity" >

          <ImageView
          android:contentDescription="@string/none"
          android:id="@+id/imageView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:adjustViewBounds="true"
          android:layout_marginLeft="0px"
          android:layout_marginTop="0px"
          android:scaleType="fitXY"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:src="@drawable/oven_alt" />  

        <com.test.game.myviewx
        android:id="@+id/xview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
        />  

          <ImageView
          android:contentDescription="@string/none"
          android:id="@+id/imageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:adjustViewBounds="true"
          android:layout_marginLeft="0px"
          android:layout_marginTop="0px"
          android:scaleType="fitXY"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:src="@drawable/oven_ust" />


</RelativeLayout>

and here is the custom view class:

public class myviewx extends View
    {
        Context cntx;
        public myviewx(Context context) {
            super(context);
            cntx = context;
            // TODO Auto-generated constructor stub
        }

        public myviewx(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            cntx = context;
            // TODO Auto-generated constructor stub
        }
        public myviewx(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            cntx = context;
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);

            final String MY_PREFS_NAME = "MyPrefsFile";
            SharedPreferences sharedPreferences = cntx.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);


            Integer pb = sharedPreferences.getInt("pb", 0);
            Bitmap bitmap;
            Resources res = getResources();
            if (pb==1) {
                bitmap = BitmapFactory.decodeResource(res, R.drawable.p1);              
            }
            else {
                bitmap = BitmapFactory.decodeResource(res, R.drawable.p2);
            }

            //int x=500;
            //int y=500;
            //int radius=250;
            Paint paint=new Paint();
            // Use Color.parseColor to define HTML colors
            //paint.setColor(Color.parseColor("#CD5C5C"));
            //canvas.drawRect (250,250, 750, 750, paint); 
            //paint.setColor(Color.parseColor("#CD000C"));
            //canvas.drawCircle(x,x, radius, paint);
            canvas.drawBitmap(bitmap, 0, 0, paint);




        }



    }

using parameter "android:layout_centerHorizontal="true"" , imageviews are centered but "com.test.game.myviewx" custom view is not centered.

what am I missing ?

  • Have you checked to make sure that your custom view is drawing the boundaries you think it is? – Bryan Dormaier Jun 16 '18 at 22:47
  • what do you mean by that ? I simplified the custom view and it just draws a static bitmap now. When the app loads the custom view, I see the image as expected. what else should I check ? – mukaddes kizilcam Jun 16 '18 at 22:52
  • Change the background color, or draw a border around your custom view to make sure that it is not drawing a larger space than you think it is. It's possible the view's bounds are larger than the image you are drawing. – Bryan Dormaier Jun 16 '18 at 22:54
  • take a look at this case: https://stackoverflow.com/questions/13273838/onmeasure-wrap-content-how-do-i-know-the-size-to-wrap – Bryan Dormaier Jun 16 '18 at 22:55
  • @Bryan Dormaier , checked it now. no problems with the boundaries. the view is only holding my image and there's nothing surrounding it. I also checked the case you've advised me to. But I could not figure out the relation. I just want my view to be centered. – mukaddes kizilcam Jun 17 '18 at 10:22
  • You didn't check the boundaries correctly. Your custom view is drawing a width across the screen, so the layout is centering it horizontally because you are not reporting the width of your view. The link discusses reporting a width of your custom view. Centering your image using `getWidth()` just shows that your view is drawing as match parent because you have not overridden anything having to do with measuring the view. – Bryan Dormaier Jun 18 '18 at 01:36

1 Answers1

0

I had the same problem and could not find a solution. Ended up drawing the content of the view centered. You can use "this.getWidth()" and then draw everything accordingly.

  • apparently that seems to be the only way. no other suggestion was made thanks. – mukaddes kizilcam Jun 17 '18 at 20:03
  • This is a hack and not the fix. The layout is doing exactly what you've told it to do. However, when you make a custom drawing to the canvas you have to override reporting the width of your custom view. As far as the layout is concerned, your view is centered, because you have not specified the width of your view after calculating how much space it takes up. – Bryan Dormaier Jun 18 '18 at 01:34