SettusBlake
SettusBlake

Reputation: 95

Change background of ImageButton

I plan to replace the background (drawable) of the Activity over a ImageButton. How can I proceed?

It is one Day Background and one Night Background. It should always change when I click on the button. Or how can I check what the background is currently active? I dont need a fully programmed code.

Upvotes: 1

Views: 316

Answers (1)

CocoNess
CocoNess

Reputation: 4222

Define StateListDrawable in an XML file. Use different images based on state of button. Here's example of XML. I suggest you google for a tutorial for full details

This filename is button_active.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@drawable/button_pressed"
    android:state_pressed="true" />
  <item android:drawable="@drawable/button_checked"
    android:state_checked="true" />
  <item android:drawable="@drawable/button_default" />

</selector> 

Extract from your view xml file which refers to the statelist drawable file

   android:background="@drawable/button_active"      

   />  

Upvotes: 2

Related Questions