0

I am trying to set the image at top over ImageButton as follows

enter image description here

But I am not being able to do so. if I use following code

<LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"

>
    <ImageButton

         android:id="@+id/btn"
         android:layout_width="40dip"
         android:layout_height="40dip"
         android:src="@drawable/button_bar_top"
         android:scaleType="fitStart"
         android:adjustViewBounds="true"
    />

</LinearLayout>

And I am getting following look

enter image description here

So I want to align it to the top and I want to remove extra padding around red bar. The size of the image is 40 X 10 and width of ImageView is also same

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Hunt
  • 8,215
  • 28
  • 116
  • 256
  • Does the drawable button_bar_top have padding or something? Is it an image or xml file? – Todd Davies Jun 08 '12 at 09:05
  • i have both and image and a shape too , image doesn't have any padding – Hunt Jun 08 '12 at 09:06
  • 1
    try `android:scaleType="fitCenter"` instead of `fitStart`... And check whether original size of ur image is not smaller than the ImageButton size... – GAMA Jun 08 '12 at 09:06
  • @GAMA fitCenter will come into the horizontal center so tht doesnt work – Hunt Jun 08 '12 at 09:08
  • @Hunt : check whether original size of ur image is not smaller than the ImageButton size as sometimes `android:adjustViewBounds="true" ` doesn't show the desired effect... refer http://stackoverflow.com/questions/7719617/imageview-adjustviewbounds-not-working – GAMA Jun 08 '12 at 09:10
  • @Gama ImageButton automatically reduces the size of image which is inside the ImageButton , so bigger dimension doesnt work too – Hunt Jun 08 '12 at 09:11
  • Have you tried larger dimension image... Bcoz ur imageButton width is 40 **dip** and ur image width is 40 **px**... and ***dip=px*** is generally false.... px = dp * (dpi / 160).... – GAMA Jun 08 '12 at 09:14
  • Hey i tried that with 50 px and i said if the image is large then ImageButton is auto scaling it and reduced its size – Hunt Jun 08 '12 at 09:15
  • i tried with 62px and ImageButton with 40dip but no luck – Hunt Jun 08 '12 at 09:27

2 Answers2

1

It seems you could use Button here with android:drawableTop attribute that will do exactly what you are trying to do.

Yasir Khan
  • 2,413
  • 4
  • 20
  • 26
0

try this it wil solve the problem

  <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" >
        </ImageView>

        <ImageButton
            android:id="@+id/imgbtnNew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10sp"
            android:onClick="onNew"
            android:padding="10sp"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
  • 2
    cant understand why to set Image src for both ImageView and ImageButton , i thing ur trying to vertically align ImageView over ImageButton but this wont overlap and to overlap i got to assign negative margins and have to use FrameLayout. so i tried it but doesn't help – Hunt Jun 08 '12 at 11:11