How to change Android button color onClick? - java

I wanted to change the android button color every time I click on a button. Once a user clicks a button, I want it so that the color changes. Then, when the button is pressed again, the color reverts back to what it was before. Here is my attempt:
private void setupFollowButton(Button button, final Boolean isClicked) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Boolean isClickedDummy = !isClicked;
if(isClickedDummy) {
v.setBackgroundColor(Color.parseColor("#FF0000"));
} else {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
});
}
Originally I wanted it so that isClicked = !isClicked so that I would know for certain that the isClicked variable has changed and I can change the color. However, the method I have above only changes the isClicked to false and I can't seem to change it back to true. Is there any way I can figure this out? Any help would be appreciated. Thanks!

try this:
isClicked = false;
private void setupFollowButton(Button button, final Boolean isClicked) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isClicked) {
v.setBackgroundColor(Color.parseColor("#FF0000"));
isClicked = false;
} else {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
isClicked = true;
}
}
});
}

You can xml drawable :
<Button
android:id="#+id/button1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
selector_xml_name.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector

You have to change the value of "isClickedDummy" and you have to use it as global.
Boolean isClickedDummy; // global after the declaration of your class
isClickedDummy = true; // in your onCreate()
private void setupFollowButton(Button button, final Boolean isClicked) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isClickedDummy) {
v.setBackgroundColor(Color.parseColor("#FF0000"));
isClickedDummy = false;
} else {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
isClickedDummy = true;
}
}
});
}

Do not forget to remove BackgroundResource of your button if you want to change the background color.
That is, use:
btn.setBackgroundResource(0);
After that, 'usual button layout' will disappear and I will show the changes of setBackground method.

Related

How to set and get tint color on SVG programmaticaly in android?

I have an ImageView which has an SVG as source example: android:src="#drawable/bold_svg".
Now on click I want to set the tint color to color accent or return it to white. Two states.
What I have tried:
myImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int currentColor, colorAccent;
currentColor=ImageViewCompat.getImageTintList(myImageView).getDefaultColor();
colorAccent=getResources().getColor(R.color.colorAccent);
if (currentColor==colorAccent) {
myImageView.setColorFilter(getResources().getColor(R.color.white_text_color));
} else {
myImageView.setColorFilter(getResources().getColor(R.color.colorAccent));
}
}
});
It looks like every time I click the button, currentColor it does not change, so else is the only thing that is called! What am I missing?
I managed to find the answer something like this would work:
myImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int currentColor, colorAccent;
currentColor=ImageViewCompat.getImageTintList(myImageView).getDefaultColor();
colorAccent=getResources().getColor(R.color.colorAccent);
if (currentColor==colorAccent) {
ImageViewCompat.setImageTintList(ivBold, ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.white_text_color)));
} else {
ImageViewCompat.setImageTintList(myImageView, ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)));
}
}
});

How to disable momentarily onClick event of ImageView and Button in Android Studio

I have 2 elements:
Button answer;
Imageview play;
When 'answer' is clicked - the app records the user.
When 'play' is clicked - the app plays a clue to the user.
So when one of the above is clicked, the other should be disabled and not allow an onclick event.
Problem is - I've tried both setClickable() and setEnabled() but they don't do what I expected them to do.
Also, I've tried using a boolean flag, but it also didn't do the trick.
What am I doing wrong?
Here is the attempt with the boolean flag:
answer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mIsAudioResourcesFree)
{
mIsAudioResourcesFree = false;
//RECORDS AND STOPS RECORDING
mIsAudioResourcesFree = true;
}
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mIsAudioResourcesFree)
{
mIsAudioResourcesFree = false;
//PLAYS SOME SHORT AUDIO
mIsAudioResourcesFree = true;
}
}
});

How to declare boolean inside button click?

I want to set something like this on button click:
boolean isLightOn = false;
if(!isLightOn)
{
FlashTask.flash.on();
isLightOn=true;
return;
} else {
FlashTask.flash.off();
isLightOn=false;
return;
}
But it's not working...
Thanks advance.
Hard to know without seeing all your code. But you could declare a variable as field member, then change it on every button click.
That's the simpliest way:
public class MyActivity extends Activity {
//boolean field member initialized as false by default
private boolean isLightOn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isLightOn){
FlashTask.flash.off();
} else{
FlashTask.flash.on();
}
isLightOn = !isLightOn;
}
});
}
}
Every time the button is clicked, boolean isLightOn = false; is called and isLightOn will always be false when the button is clicked. You are not preserving the previous state.
To preserve the state, make isLightOn as a class variable and remove boolean isLightOn = false; from your listener method.
You need somthing like this.
private boolean isLightOn = false; //defined as class variable;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isLightOn){
FlashTask.flash.on();
isLightOn=true;
}else {
FlashTask.flash.off();
isLightOn=false;
}
}
});
But make sure isLightOn is defined outside the listener scope.

How to make a button clickable only when a selected spinner value is selected in Android?

I want the button to be clickable only when a specific spinner value is selected. How can I do that? I tried this but it did not work. spLocation is my spinner name.
if (spLocation.getSelectedItem().equals("Bus Stop")) {
btnRoute = (Button) findViewById(R.id.btnRoute);
btnRoute.setClickable(true);
You can do this
btnRoute.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (spLocation.getSelectedItem().equals("Bus Stop")) {
// Do your action
} else {
// Do your action
}
}
});
try this:-
String S = spLocation.getSelectedItem().toString();
if(S!=null){
btnRoute.setClickable(true);
}else{
btnRoute.setClickable(false);
}

How to use setOnClickListener and setOnTouchListener + for

Why can not I use setOnClickListener and setOnTouchListener in my program?
OnTouchListener works well, but I cen't run new activity?
what am I doing wrong?
for (final ShopCategory category : gallery.getShopCategories()) {
final Button button = new Button(this);
// ... etc
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
runNewActivity(gallery.getShops(), category);
}
});
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
button.setTextColor(Color.parseColor("#333333"));
button.setBackgroundColor(Color.parseColor("#ffcc33"));
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP ) {
button.setTextColor(Color.WHITE);
button.setBackgroundColor(Color.parseColor("#333333"));
}
return false;
}
});
categoriesButtonsLL.addView(button);
You can do it better with using drawable selector to change button's show state rather than listen touch event.
(create a selector xml file in res/drawable, for example "button_bg.xml") :
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
now edit your code like this:
for (final ShopCategory category : gallery.getShopCategories()) {
final Button button = new Button(this);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
runNewActivity(gallery.getShops(), category);
}
});
button.setBackgroundResource(R.drawable.button_bg);
categoriesButtonsLL.addView(button);
}
Quick guess here. But try returning false in your onTouch. Returning true in the ActionDown means that you handled the event and no further processing is necessary. So if ActionDown is handled, onClick never happens.

Categories