Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android button background change

I'm creating an Android app, when a user clicks a button in the app the button image should change "to show it has been pressed" then some function is being called

using the following code at the onclick function:

{
  btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.sync_active));
  sync();               
}

what happens is the sync function is called first then the button image changes !!

like image 692
Helal Ismail Avatar asked Dec 21 '25 01:12

Helal Ismail


1 Answers

create xml file using the button image like this with mybutton.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
  android:state_pressed="true" 
  android:drawable="@drawable/greencolorbutton" />
<item 
  android:drawable="@drawable/closebutton" />
</selector>

and use this in button xml code

android:background:@drawable/mybutton

you can check it from this https://stackoverflow.com/q/8132500/964741

like image 179
RajaReddy PolamReddy Avatar answered Dec 23 '25 13:12

RajaReddy PolamReddy