Home » Android » android – How to set image on centre in FrameLayout

android – How to set image on centre in FrameLayout

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m doing a project in which I want to set an image in the center.I’m using TableLayout and I want set this image in FrameLayout. If u have any idea so please suggest me. Its very urgent. Thanx to all. Here is my code:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*" >

       <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:layout_weight="1"
          android:layout_gravity="center"
          android:scaleType="center"
         >

        <com.nexbits.digitechi.DrawCanvas
        android:id="@+id/SurfaceView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         />


     </FrameLayout>

     <TableRow

     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:gravity="center_horizontal">


    <ImageButton
        android:id="@+id/imageButton5"

        android:layout_weight="1"
        android:padding="20dip"
        android:layout_gravity="center_horizontal"
        android:onClick="video"
        android:src="@drawable/ic_webcamera" />

    <ImageButton
         android:id="@+id/imageButton3"
         android:layout_weight="1"
         android:layout_gravity="center_horizontal"

         android:padding="20dip"
         android:src="@drawable/ic_twitter64" />

    <ImageButton
        android:id="@+id/imageButton2"

         android:layout_weight="1"
         android:padding="20dip"
         android:layout_gravity="center_horizontal"

         android:src="@drawable/ic_brightness64" />

      <ImageButton
          android:id="@+id/imageButton4"
          android:layout_weight="1"
          android:padding="20dip"
          android:layout_gravity="center_horizontal"
          android:src="@drawable/microscope" />
         </TableRow>
         </TableLayout>
How to&Answers:

Use this line in your Frame layout:

android:layout_gravity="center_vertical|center_horizontal"
android:foregroundGravity="center"

<com.nexbits.digitechi.DrawCanvas
    android:id="@+id/SurfaceView"
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Answer:

I achieved it by adding to Child (something inside frame layout)

  android:layout_gravity="center" 

Answer:

Did you try this:

<FrameLayout
        android:gravity="center"
        android:background="@drawable/bg"
>

where bg is an image in the drawable-hdpi/drawable-mdpi etc.It can be bg.png/bg.jpg etc.

If you want to set the image inside the framLayout in the center,then try like the following.

    <com.nexbits.digitechi.DrawCanvas
         android:id="@+id/SurfaceView"

         android:layout_width="wrap_content"
         android:gravity="center_vertical" or android:gravity="center_horizontal"

         android:layout_height="wrap_content"

     />


 </FrameLayout>