I want to know if there is a way of changing a button image when it is clicked.
Initially the button has a pause icon. When it is clicked I want the play icon to be displayed. Each time the button is clicked the icon should vary between play and pause.
Is there any way of doing this?
XML Layout file:
<ImageButton
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:src="#drawable/play_btn"
android:background="#null" />
play_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pause" android:state_selected="true" />
<item android:drawable="#drawable/play" />
</selector>
Android's Toggle Button with a custom selector sounds like what you want.
You might use something like this for your button's selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/play_icon" android:state_checked="true" />
<item android:drawable="#drawable/pause_icon" android:state_checked="false" />
</selector>
Button mPlayButton;
boolean isPlay = false;
#Override
public void onCreate(){
mPlayButton = (Button) findViewById(R.id.play);
// Default button, if need set it in xml via background="#drawable/default"
mPlayButton.setBackgroundResource(R.drawable.default);
mPlayButton.setOnClickListener(mTogglePlayButton);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
#Override
public void onClick(View v){
// change your button background
if(isPlay){
v.setBackgroundResource(R.drawable.default);
}else{
v.setBackgroundResource(R.drawable.playing);
}
isPlay = !isPlay; // reverse
}
};
Let's add an onClick field to your button's xml in your layout (requires API level 4 and onwards)
<ImageButton
android:id="#+id/play"
...
android:onClick="buttonPressed" />
Your icons are drawables pause and play.
Keep track of which icon your button has.
private boolean paused = true;
public void buttonPressed(View view) {
ImageButton button = (ImageButton) view;
int icon;
if (paused) {
paused = false;
icon = R.drawable.pause;
else {
paused = true;
icon = R.drawable.play;
}
button.setImageDrawable(
ContextCompat.getDrawable(getApplicationContext(), icon));
}
Checkout this documentation http://developer.android.com/reference/android/widget/ImageButton.html specifically the inherited methods from android.widget.imageview. You'll want to use either setImageBitmap or setImageDrawable. Update it as part of your on click code.
You can use MaterialButton instead of Button and then just use setIcon method for your button:
import com.google.android.material.button.MaterialButton;
MaterialButton mPlayButton;
boolean isPlay = false;
#Override
public void onCreate(){
mPlayButton = (MaterialButton) findViewById(R.id.play);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
#Override
public void onClick(View v){
// change your button background
if(isPlay){
v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource));
}else{
v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource2));
}
isPlay = !isPlay; // reverse
}
};
Related
I am new to Android development. I would like to achieve following functionalities but confused about how to do it:
Initially, the image button is DISABLED and the color(tint) is gray.
When click on the image button, meaning ENABLE the button and the color (tint) of the button turns to orange (or other colors) and NOT return to gray.
When click on the image button again, meaning DISABLE the button and the color turns gray again.
The code I tried is shown below:
clickbutton_mic.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<bitmap
android:src="#drawable/big_mic_icon"
android:tint="#android:color/darker_gray"
/>
</item>
<item android:state_enabled="true" >
<bitmap
android:src="#drawable/big_mic_icon"
android:tint="#android:color/holo_orange_dark"
/>
</item>
<item android:drawable="#drawable/big_mic_icon"/>
</selector>
Part of main_page.xml
<ImageButton
android:id="#+id/imageButton"
android:layout_width="72dp"
android:layout_height="73dp"
android:background="#android:color/transparent"
android:onClick="onClick"
android:src="#drawable/clickbutton_mic" />
Part of MainPage.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
ImageButton btn = (ImageButton) findViewById(R.id.imageButton);
btn.setEnabled(false);
}
public void onClick(ImageButton btn)
{
if(!btn.isEnabled()){
btn.setEnabled(true);
}
else{
btn.setEnabled(false);
}
}
The result of this code is showing the black icon (the original icon color, neither gray or orange) all the time.
I have found some questions about disable/enable image button on stackoverflow but the answers are not clear to me. So can anyone help me deal with this situation? Thank you!
Solved by myself. I misunderstood the meaning of "setEnabled". My following code achieved what I want.
private int MIC_STATUS = 0; // MIC OFF
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
final ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(MIC_STATUS == 0){ // mic is off, turn on the mic
imageButton.setColorFilter(Color.rgb(255,42,72));
MIC_STATUS = 1;
}
else if(MIC_STATUS == 1){ // mic is on, turn off the mic
imageButton.setColorFilter(Color.WHITE);
MIC_STATUS = 0;
}
}
});
}
I would like to abuse a standard button as a toggle button, but only when its longpressed. Therefore I first replaced the default style with background images for pressed, focused and default state.
<?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/btn_pressed" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="#drawable/btn_focused" /> <!-- focused -->
<item android:drawable="#drawable/btn_default" /> <!-- default -->
</selector>
I implemented both, onClickListener and OnLongClickListener following:
private OnLongClickListener mFireHoldListener = new OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Log.i(TAG, "Long FIRE");
Button btn = (Button) view;
btn.setPressed(true);
btn.invalidate();
Log.i(TAG, "isPressed: " + btn.isPressed());
return false;
}
};
If I perform a long click, the button doesn't change its background to the state_pressed. How can I keep the button pressed? Using a toggle button doesn't work as a normal click operation should be possible. If the button is pressed for a longer time, the button gets "locked".
Many Thanks
So Finally it works.It's right way as comparred to make the button state deliberatly true
#Override
public boolean onLongClick(View view) {
final Button btn = (Button) view;
btn.post(new Runnable(
public void run() {
btn.setBackgroungResource(R.drawable.btn_pressed);
}
}
return false;
}
I want to custom button ,
If user pressed it will show red color and still show red until user pressed other button
how to do this? thanks.
Try this code:
final Button b1 = (Button)findViewById(R.id.btn_1);
final Button b2 = (Button)findViewById(R.id.btn_2);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setBackgroundColor(Color.RED);
b2.setBackgroundColor(Color.WHITE);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setBackgroundColor(Color.RED);
b1.setBackgroundColor(Color.WHITE);
}
});
Use OnClickListeners to change the background of the Button using setBackgroundColor() or setBackgroundDrawable()
You can use a CheckBox for your button and set the background to a state list drawable that tests for the android:state_checked attribute.
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.RED);
}
});
button2.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.WHITE);
}
});
What you want is a state list. A quick Google found this article: http://blog.androgames.net/40/custom-button-style-and-theme/ that explains them step by step. That way you don't need any code :)
Here's a code that you need to save as an .xml file and place into your drawable folder.
The android:drawable tags point to the drawable resources for each button state. You can shorten this list if you want to.
Then you can use it as a drawable when creating your layout.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/menu_button_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/menu_button_normal" />
<item android:state_pressed="true" android:drawable="#drawable/menu_button_pressed" />
<item android:state_enabled="true" android:drawable="#drawable/menu_button_normal" />
<item android:state_enabled="false" android:drawable="#drawable/menu_button_normal"/>
</selector>
I'm not sure this is even possible. I have this button:
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="45px"
android:drawableTop="#drawable/buttontv"
android:layout_weight="1"
android:background="#null"
android:textColor="#ffffff"
android:textSize="9px"
android:text="TV"/>
And this button has this item xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/tv" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:drawable="#drawable/tv" />
</selector>
And in my application I use this code for when clicking my button:
OnClickListener b1Listener = new OnClickListener(){
#Override
public void onClick(View v) { loadUrl("http://example.org/"); v.setPressed(true); }
};
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(b1Listener);
What I would like that when I have pressed the button, the drawableTop sets to one of the items with the #drawable/tv_pressed attribute value - and stays there (as in 'this is the currently active button').
I tried adding v.setPressed(true) in the onClick function (as this was all I could find with Google) but that didn't work.
Can this be done? Or is there a better alternative to what I'm trying to accomplish?
If you need a button that gets pressed and stays active, use a ToggleButton
https://stackoverflow.com/a/9750125/1257369
button.setFocusable(true);
button.setFocusableInTouchMode(true);///add this line
or with styles.xml
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
as mentioned above by the answer, it is ideal to use a togglebutton but if you are using a listview in this scenario, then the togglebutton will block out your listview's setOnItemClickListener. You can fix that issue as well by adding the descendants as blocked in the listview layout. However some people have reported that even then, their listview items won't click after using toggle buttons.
Are you working with dialogs/dialogfragments?
This code might help you if you are, because i had a simmilar problem and this worked for me. :)
1- extend your class with DialogFragment
,Override Onstart() method.
#Override
public void onStart() {
super.onStart();
final AlertDialog D = (AlertDialog) getDialog();
if (D != null) {
Button positive = (Button) D.getButton(Dialog.BUTTON_POSITIVE);
positive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (edittext.equals("")) {
Toast.makeText(getActivity(), "EditText empty don't close",Toast.LENGTH_SHORT).show();
} else {
D.dismiss(); //dissmiss dialog
}
}
});
}}
Is there a way to specify an alternative background image/color for a Button in the XML file that is going to be applied onClick, or do I have to do a Button.setBackground() in the onClickListener?
To change the image by using code:
public void onClick(View v) {
if(v.id == R.id.button_id) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}
Or, using an 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));
In the latest version of the SDK, you would use the setBackgroundResource method.
public void onClick(View v) {
if(v == ButtonName) {
ButtonName.setBackgroundResource(R.drawable.ImageResource);
}
}
public void methodOnClick(View view){
Button.setBackgroundResource(R.drawable.nameImage);
}
i recommend use button inside LinearLayout for adjust to size of Linear.
Try:
public void onclick(View v){
ImageView activity= (ImageView) findViewById(R.id.imageview1);
button1.setImageResource(R.drawable.buttonpressed);}
I used this to change the background for my button
button.setBackground(getResources().getDrawable(R.drawable.primary_button));
"button" is the variable holding my Button, and the image am setting in the background is primary_button