Make Android button wallpaper when clicking on XML

Is there a way to specify an alternative background image / color for the button in the XML file that will be applied by onClick , or do I need to do Button.setBackground() in onClickListener ?

+29
android onclick android-button
Nov 08 '10 at 16:26
source share
4 answers

To change the image using code

 public void onClick(View v) { if(v == ButtonName) { ButtonName.setImageResource(R.drawable.ImageName); } } 

Or using the XML file:

 <?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/login_selected" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/login_mouse_over" /> <!-- focused --> <item android:drawable="@drawable/login" /> <!-- default --> </selector> 

In OnClick just add this code:

 ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName)); 
+94
Nov 08 2018-10-10T00:
source share

In the latest version of the SDK, you should use the setBackgroundResource method.

 public void onClick(View v) { if(v == ButtonName) { ButtonName.setBackgroundResource(R.drawable.ImageResource); } } 
+6
Jan 25 '13 at 20:52
source share

Try:

 public void onclick(View v){ ImageView activity= (ImageView) findViewById(R.id.imageview1); button1.setImageResource(R.drawable.buttonpressed);} 
+1
Jun 17 '13 at 4:48 on
source share
 public void methodOnClick(View view){ Button.setBackgroundResource(R.drawable.nameImage); } 

I recommend using the button inside LinearLayout to adjust the size of the Linear.

+1
May 11 '15 at 20:20
source share



All Articles