How to get ImageButton size within Android GridView?

Posted by wufoo on Stack Overflow See other posts from Stack Overflow or by wufoo
Published on 2012-12-18T17:02:10Z Indexed on 2012/12/18 17:02 UTC
Read the original article Hit count: 159

Filed under:
|
|

I'm subclassing ImageButton in order to draw lines on it and trying to figure out where the actual button coordinates are within my gridview. I am using onGlobalLayout to setup Top, Bottom, Right and Left, but these seem to be for the actual "square" within the grid, and not the actual button (see image). The purple lines are drawn in myImageButton.onDraw() using coords gathered from myImageButton.onGlobalLayout(). I thought these would be for the button, but they seem to be from something else. Not sure what. I'd like the purple lines to match the outline of the button so the lines I draw appear on the button and not just floating out in the LinearLayout somewhere. The light blue is the background color of the vertical LinearLayout holding the Textview (for the number) and myImageButton. Any way to get the actual button size?

gridview/layout/button

XML Layout:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="fill_vertical|fill_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_cell"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:gravity="center"
            android:text="TextView"
            android:textSize="10sp" />

        <com.example.icaltest2.myImageButton
            android:id="@+id/imageButton1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_margin="0dp"
            android:adjustViewBounds="false"
            android:background="@android:drawable/btn_default"
            android:scaleType="fitXY"
            android:src="@android:color/transparent" />

    </LinearLayout>
</FrameLayout>

myImageButton.java

   public myImageButton (Context context, AttributeSet attrs)
   {
       super (context, attrs);
       mBounds = new Rect();
       ViewTreeObserver vto = this.getViewTreeObserver ();
       vto.addOnGlobalLayoutListener (ogl);
       Log.d (TAG, "myImageButton");
   }


...

    OnGlobalLayoutListener ogl = new OnGlobalLayoutListener()
        {


            @Override
            public void onGlobalLayout ()
            {
                Rect b = getDrawable ().getBounds ();


                mBtnTop = b.centerY () - (b.height () / 2);
                mBtnBot = b.centerY () + (b.height () / 2);
                mBtnLeft = b.centerX () - (b.width () / 2);
                mBtnRight = b.centerX () + (b.width () / 2);

            }
        };
...

 @Override
   protected void onDraw (Canvas canvas)
   {

      super.onDraw (canvas);
      Paint p = new Paint ();
        p.setStyle (Paint.Style.STROKE);
        p.setStrokeWidth (1);

        p.setColor (Color.MAGENTA);
        canvas.drawCircle (mBtnLeft, mBtnTop, 2, p);
        canvas.drawCircle (mBtnLeft, mBtnBot, 2, p);
        canvas.drawCircle (mBtnRight, mBtnTop, 2, p);
        canvas.drawCircle (mBtnRight, mBtnBot, 2, p);
            canvas.drawRect (mBtnLeft, mBtnTop, mBtnRight, mBtnBot, p);


}

© Stack Overflow or respective owner

Related posts about android

Related posts about layout