0

I have a problem... I created this button:

<Button
    android:layout_width="0dp"
    android:layout_weight="50"
    android:layout_height="120dp"
    android:id="@+id/btnTriangle"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:background="@drawable/TriangleShapeButton" />

Now I want to add a image to that button, but adding this line doesnt work:

android:src="@drawable/triangle3"

Is it possible to add this image without using an ImageButton?

A. Vreeswijk
  • 822
  • 1
  • 19
  • 57
  • Maybe helpful: https://stackoverflow.com/a/11176758/11219312 – AmRo May 06 '19 at 16:09
  • Yeah, but he is using a ImageButton... I want to use a default button – A. Vreeswijk May 06 '19 at 16:16
  • In second part of the answer he create a custom button. "and second option if you want to create an image like button using Button View then Create an Custom button as:" – AmRo May 06 '19 at 16:29
  • @A.Vreeswijk This is possible.However, this is a bit complex way to realize it.If in xml , you should add layout up or inside the button.Else if in cs code, you should custom Button's View with coding .All of them , not a sample way . – Junior Jiang May 07 '19 at 03:04

1 Answers1

0

Yes, this will do it...

android:drawableLeft="@drawable/triangle3"

You can also use drawableStart, drawableBottom, drawableLeft, etc.

You then may want to use the drawablePadding and/or paddingLeft/paddingRight/etc. attributes to position your graphic if it is smaller than the background image (i.e., it isn't centered by default).

Robert Bruce
  • 1,088
  • 1
  • 13
  • 17
  • And is it possible to autofit the image into the button, because it uses the original size now. That doesn't fit, so it needs to be scaled for a bit. Possible? – A. Vreeswijk May 06 '19 at 22:17
  • I don't believe there's an axml attribute to do that. There's probably some way to do it in code, although that would be more complex to implement. A quick search shows these links which may have some solutions worth investigating further: https://stackoverflow.com/questions/22218486/android-fit-height-of-drawableleft-in-a-textview and https://stackoverflow.com/questions/5483828/is-it-possible-to-scale-drawableleft-drawableright-in-textview. If one of those works, consider adding an answer here as it would help other folks. Good Luck! – Robert Bruce May 07 '19 at 00:29