Danny Kim
Danny Kim

Reputation: 400

edit imagebutton picture

I made an app that displays an AlertDialog once I click on an ImageButton in eclipse.

But when I was trying to input a picture onto the ImageButton, I only found a way to do it in the XML graphic layout of eclipse via right clicking on the Button and then on "edit src". Is there a way to put in the picture from written XML itself?

Upvotes: 1

Views: 255

Answers (1)

David Manpearl
David Manpearl

Reputation: 12656

Yes. You can put a picture into the written xml of an Android layout file.

With your xml file open in eclipse, there are two tabs at the bottom of the layout:

  • Graphical Layout
  • .xml

Press the ".xml" tab and you can edit the raw xml.

In order to add an image to your ImageButton, do this:

android:background="@drawable/your_image"

There are also other tags that work too, such as android:src, and the differences are described here: What is the difference between src and background of ImageView.

There are also ways to add the image or change the image programmatically in your .java file. This is useful only if you want to change the image dynamically (although, see below for changes of state)


And, this is just the beginning of what you can do...

If you don't want the default frame around the button, you can use other items, such as an ImageView to make your button.

If you want to display various graphical images depending on state such as "pressed", "selected", etc, then you can specify a "selector" file instead of "your_image". There are many examples of how to make "selector" files on this site, and how to implement the other upgrades to your button.

Have fun!

Upvotes: 2

Related Questions