Android Button color not changing programatically - android

I have this piece of code, which is expected to change the color of the button to BLUE, but it does not have any effect.
The code goes into this if statement but does not change the color. On the other hand , the same statement when used earlier does actually change the color of the button. Why is this so?
if(t.equals("a"))
{
Toast toast5=Toast.makeText(getApplicationContext(),"a found", Toast.LENGTH_SHORT);
toast5.show();
btn6.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
}

// Get Handle for the Tab buttons
Button btnTab1 = (Button) findViewById(R.id.button_tab1);
Button btnTab2 = (Button) findViewById(R.id.button_tab1);
// set the colors correctly
btnTab1.setBackgroundResource(R.color.lightblue);
btnTab2.setBackgroundResource(R.color.darkblue);

Use
btn6.setBackgroundColor(Color.BLUE);

You implement for this code,
btn6.setBackgroundColor(Color.colorChoose);
btn6.setBackgroundDrawable(Drawable drawable);
btn6.setBackgroundResource(int resid);

Related

Is there a way to make a disabled button clickable in Android Studio?

In Android Studio I have a Kotlin project with a button that sends an SMS message. When the Send SMS permission for the app is denied, the button is disabled. When a user clicks the disabled button, I would like to display a toast message to the user:
Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()
However, after much research I haven't been able to make this disabled button clickable. I have tried things like smsButton.isClickable = true on the button, but it still doesn't work. If it is possible to style the button to simply look like it is disabled, that would work too. My button style is currently set to #style/Widget.MaterialComponents.Button and I haven't had any success in changing the button colors. I have tried applying a background, backgroundTint, custom theme, and creating a custom background like in this question.
I am new to Android Studio so I have limited knowledge and experience. Is there a way to achieve this functionality?
Here is what you can do:
if (isPermissions) {
//keep the button as it is or do what you have to when button is enabled
} else {
//change the button color and maybe icon two
button.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.grey))
button.setOnClickListner {
//show toast
Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()
}
}
isClickable only makes it clickable once again if the click is disabled with the same attribute.
Try:
button.isEnabled = true

How can I fix GradientDrawable cannot be cast to ColorDrawable issue?

I have a code snippet to show you below and I am trying to change view's(v) background. I'll get color code from a TextView(dragged) and change View(v)'s background by using this code. But I get an error as indicate above. How can I fix it? Where is the problem? Thanks.
ColorDrawable cd = (ColorDrawable)dragged.getBackground();
int colorCode = cd.getColor();
v.setBackgroundColor(colorCode);
If you want to just assign a background of one view to another and not just the color you can use the following code
Drawable drawable = dragged.getBackground();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
v.setBackgroundDrawable(drawable );
}else{
v.setBackground(drawable);
}
However if you want to get only the color then you will have to identify in advance what types of drawable are being assigned to view. Then use instanceof to handle how to get background color for corresponding drawable.

Testing for Resources such as Drawables and Background

I need to see if there is a way where I can test for what background a button has. For example, here is my pseudo code:
if (button background is `R.drawable.black`) {
button.setBackground(to R.drawable.white)
}
Is there a way that this if statement can be done? I know how to SET backgrounds, just curious on the test portion.
In your case, I believe that tags would be useful. Try this:
//When you set the button:
btn.setBackgroundResource(R.drawable.black);
btn.setTag(R.drawable.black);
//When you re-set the button:
if(btn.getTag().equals(R.drawable.black)) {
btn.setBackgroundResource(R.drawable.white);
btn.setTag(R.drawable.white);
}
There is a getBackgroud method
Drawable buttonBackground = button.getBackground();

What makes a button switch and keep color on click?

I want to create a Button that behaves like a switch.
It should change its color when the user clicks it, and keep the color.
So the button is white at first and when the user clicks it, the color changes to black. When the user clicks it again it switches back to white and so on.
I tried it with a simple if else construct but only managed to get the button to be white at first and when being clicked change to black, but it won´t change back to white when clicked again.
Here´s the code so far. I guess it´s a dumb simply mistake but can´t seem to get through with it. "changecolor" is a variable I declared myself.
// Select Button Safe or At-Risk
final Button button7 = (Button) findViewById(R.id.SafeBT);
button7.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks, change color
if (changecolor == 0) {
button7.setBackgroundColor(color.black);
changecolor = 1;
} else {
button7.setBackgroundColor(color.white);
changecolor = 0;
}
}
});
Tanks for advice and help in advance.
where you have declared your variable changecolor?? .
Second thingis that you can simply an UI Element which call it : ToggleButton , it is like a Switch Button ON/OFF . is that what you want ? see this link : http://developer.android.com/reference/android/widget/ToggleButton.html

transparency of button is not changed using 'setAlpha'

I am developing a simple application ,in that there is set of 6 buttons.
when i clicked on one of these button then the other buttons must be partially transparent. I tried to done that By setting alpha of 5 buttons
acre_button.getBackground().mutate().setAlpha(155);
Ui of the application not changed as i expected. i got only 3 out of 5 is get transparent.when clicking on that two button it is slowly changing it's transparency
Thanks in advance
regards,
kariyachan
Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.main_btn);
Drawable d = getResources().getDrawable(R.drawable.imagen);
d.setAlpha(60);
btn.setBackgroundDrawable(d);
}
This works for me :)
Find a button background in your android-sdk directory here: android-sdk\platforms\android-10\data\res\drawable-mdpi\btn_default_normal.9.png
You can modify it to make it semi transparent (Please note that this is a 9-patch and you shouldn't change the opacity of the black lines).
Once you have this changed button in your drawable directory you can add this to your code:
button.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparentImage));
to make it semi transparent and
button.setBackgroundDrawable(getResources().getDrawable(Android.R.drawable.btn_default_normal));
to change it back.
For anyone still hunting for a solution to this:
The method setBackgroundDrawable(Drawable d) is deprecated as of API 16
Assuming your button's id is buttonId and your drawable is named button_img,
handle this by using the following within the onCreate method:
((Button)(findViewById(R.id.buttonId))).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Drawable d = v.getResources().getDrawable(R.drawable.button_img);
d.setAlpha(40);
if (Build.VERSION.SDK_INT >= 16)
v.setBackground(d);
else
v.setBackgroundDrawable(d);
//Then call your next Intent or desired action.
}
});
Tested and works for me!
Try the following:
button.setBackgroundColor(android.R.color.transparent);

Categories

Resources