change button background at runtime in android - android

I want to change my button background at RUNTIME
I know how to add a custom background to a button, but
How can I change it to borderlessButtonStyle?
in the xml was easy, I used:
style="?android:attr/borderlessButtonStyle")
How can I switch back to the default background? The following is not what I want (apperantly it is not the default used in Jelly Bean)
changeableButton.setBackgroundResource(android.R.drawable.btn_default);
Thanks for your help

I actually found a solution
You can save the initial background of your button in a Drawable object
Drawable d = button.getBackground();
Now you can modify your button's background as you wish
button.setBackgroundResource(R.drawable.custom_button);
Than you can modify it back
button.setBackgroundDrawable(d);

does this work?
changeableButton.setBackground(null);

There is a simple answer to this problem:
button.setBackgroundResource(R.drawable.backround);
you can also change back to your default backround by using an if statement and repeating the above statement by just changing the "backround" resource to your default resource.
Btw thanks for A2A.

Related

selector drawable not working when applying on a button

selector drawable xml not working when applying it to a button on the screen, whenever i change the background of the button to the drawable xml the button is not being pressed when i run the app and it's color doesn't change, when i change the background to a static color it returns to it's normal behavior and is press-able, any solutions?
selector xml
activity xml
I got this error in the past and from what I lean is that From Android Studio 4.1 many of the components started using the Material Components And sets up the default theme to be based on Theme.material components.DayNight.DarkActionBar.
And because of this, any Button elements in a layout get turned into MaterialButton, and MaterialButton that ignores android:background.
In my case, I solved this with -
app:backgroundTint="#null" //just need this
android:background="#drawable/background_button"
and if this does not work for you, you can check this answer here, which I used in the past, hope it's helps.

Android using switchpreference to set color background

How can I change the color of the app I'm trying to develop.
I have used a switch statement under preferences menu, but do not know how I can code this, so that when the user clicks on the switch, the background color is changed across the whole app.
Does anyone have any idea of how this can be done?
Thanks
You can enable the user to change the theme using preferences. This will change the background color throughout the application in accordance with the choice. The following thread is talking about the same problem: How to change current Theme at runtime in Android. Hope this helps.

Android, change only clicked color of a button

I would like to change the default orange color that appears when someone press an Android button. I have done many searches but all I found was the use of selectors.
I understand the principle, but I don't want to modify the grey aspect of the normal button (not pressed). But using selectors force to define all characteristics of all aspects (pressed or not).
I don't know how to obtain the default aspect of buttons in the light theme, so can anyone tell me where I can find the original parameters of the light theme or at least give me another means to simply change the color of the button when clicked?
You can copy the Android's selector into your project, the one that Android sets it by default to buttons, and modify only the state when the button is pressed by just changing one single drawable.
You could find the file in \android-sdk\platforms\android-10\data\res\drawable\btn_default.xml
Sorry bro...........
i guess only selector will help you......
You must go with selector.....
and selector are reusable xmls you can use in all buttons.....:)
You can refer below link.
:)
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
I tried to find a solution to this problem, but did not succeed.
Maybe this will help:
How to modify the default button state in Android without affecting the pressed and selected states?
Standard Android Button with a different color

Android UI: setting background color for Button prevents it from highlighting upon press

I need to eliminate the gap between buttons in a layout. I'm doing it by setting background color for a style and applying that style to my buttons. This eliminates the highlighting upon press, which I of course need. Here is a solution to this problem, but it requires extra actions to be taken with Java code. Can I make it statically, only with XMLs?
Also, if there is any other way to remove the gap without changing the other aspects of button's appearance - please suggest. The only requirement is it should be done with XML (however, if no other options exist, I can inherit Button class).
You can use StateList xml for background of your button

How to get a Button's background back to default (programmatically)?

So I'm setting a button's background doing this:
b.setBackgroundResource(R.drawable.custom_button1);
How do I programmatically set it back to the default (boring grey) Android button? Is there a R.android.boring_grey resource identifier I can reference without recreating those states myself? Couldn't seem to find it. Maybe my Googling skills are failing me.
Oh and by the way I tried this:
b.setBackgroundResource(0);
And the button actually disappeared (blended with black background?).
Have you tried this?
android.R.drawable.btn_default;
first get the default background of Button b; using
Drawable d = b.getBackground();
then set another background of your choice
b.setBackgroundResource(R.drawable.custom_button1);
if you need default background again use this
b.setBackgroundDrawable(d);
Use this
b.tr.setBackgroundDrawable(null);
I did it by
this.setBackgroundResource(android.R.drawable.btn_default);
try
button.setBackgroundResourses(R.drawable.yourimage);
it will set the default background of buttons.
and you can read more default properties of android widgets from given link:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
Drawable for "modern" button in XML is #android:drawable/btn_default_material
It does not exist in android.R.drawable for some reason.
EDIT: it's private, so don't use it.

Categories

Resources