how to change background image of button when clicked/focused?
you can implement in a xml file for this as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" />
<item android:drawable="@drawable/image_name_while_notpressed" /> //means normal
</selector>
now save this xml file in drawable folder and name it suppos abc.xml and set it as follows
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
Hope it will help you. :)
How to change background image of button when clicked in kivy?
try that
<Control_>:
Button:
background_normal: "on.png"
size_hint: .3, .2
pos_hint: {"x":.3, "y":.3}
on_release:self.background_normal="on.png" if background_normal=="off.png" else "off.png"
How to Change the background Image of a Layout when a button clicked
To show an Array
of images as background, you need to create your Array
, and an index
to point to a position
within your Array
private int[] images; // declare your array in global scope
private int imagesIndex = 0;
Then you need to populate your Array
with Drawable Resources
. You can do it in onCreate() method
(if using Array
in `Activity).
int numOfImages = 4;
images = new int[numOfImages];
images[0] = R.drawable.ic_launcher_background;
images[1] = R.drawable.ic_launcher_foreground;
images[2] = R.drawable.fiona;
images[3] = R.drawable.shrek;
Finally in Click Listener
of your Button
you simply need to select a resource from your Array
.
@Override
public void onClick(View v) {
LinearLayout view = (LinearLayout) findViewById(R.id.layout1);
view.setBackgroundResource(images[imagesIndex]);
++imagesIndex; // update index, so that next time it points to next resource
if (imagesIndex == images.length - 1)
imagesIndex = 0; // if we have reached at last index of array, simply restart from beginning.
}
Change button appearance when focus or click?
you can use a selector XML and specify that as button background. To get an idea of how this works check this out.
Change background on button click, using CSS only?
Based on the fiddle I posted in the comments I've modified it very slightly to use the Bootstrap button CSS.
Just include Bootstrap's CSS file then use the code below.
HTML
<label for="check" class="btn btn-default">Toggle background colour</label>
<input type="checkbox" id="check" />
<div></div>
CSS
div {
width: 100%;
height: 100%;
background: #5CB85C;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
label.btn {
position: absolute;
top: 10px;
left: 10px;
z-index: 2;
}
input[type="checkbox"]{
display: none;
}
input[type="checkbox"]:checked + div{
background: #5BC0DE;
}
This uses the adjacent sibling selector.
Here it is working: http://jsfiddle.net/eYgdm/ (I've added *-user-select: none;
to prevent selection of the label text but it's not required for the label to work)
Browser support
Chrome, Firefox [Desktop & Mobile] (Gecko) ≥ 1.0, Internet Explorer ≥ 7, Opera ≥ 9 and Safari ≥ 3
References: Attribute selectors and Adjacent sibling selectors
Related Topics
Capturing Images with Mediastore.Action_Image_Capture Intent in Android
How to Test Boot_Completed Broadcast Receiver in Emulator
Google Drive API Doesn't Play Well with Proguard (Npe)
Android Studio- "Sdk Tools Directory Is Missing"
Using Webview Sethttpauthusernamepassword
Where Does Local.Properties Go for Android Project
Android Fragmenttransaction Custom Animation (Unknown Animator Name: Translate)
How to Pass Arraylist<Hashmap<String, String>>From One Activity to Another
Android - How to Check If Developer Option Is Enabled
How to Detect When the User Launches Another App? (Android)
Setting Android Theme Background Color
Gradle Flavors for Android with Custom Source Sets - What Should the Gradle Files Look Like
Highlighting the Selected Item in the Listview in Android
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
How to Access an Image from the Phone's Photo Gallery