selector drawable not working when applying on a button - android

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.

Related

How can I change the predefined color in Android with xml

Its not so long since I started to program in Android Studio. I was wondering why everything that I'm adding (like button, the actionBar...) has already a Lilac predefined color and how I can change this color. When I tried to change the color of the button in xml of my activity nothing happened.
android:background="#color/teal_200"
Can you help me please to know where I can change this default color and why the
android:background="#color/teal_200"
didn't worked?
Thank you!
These elements will likely be in an activity and that activity will have a theme, which you can check by looking at the android:theme of the activity (or the application, if the activity doesn't have one) in AndroidManifest.xml.
If you've started a new project from a template, this theme will likely be in res/values/themes.xml and it will define colours that will be used wherever that theme is applied.
Different elements take different colours from the theme and you need to check the documentation for each one to understand where their colours come from and how to change them.
For example, if you're using a contained MaterialButton, its documentation is here. There you can see that it takes its background colour from the colorPrimary of the theme by default and this can be overridden by setting the app:backgroundTint of the button. Therefore, if you wanted to change the colour of all the contained buttons in activities that use that theme, you'd need to change the colorPrimary of the theme. You could also change the colour of individual buttons by setting the app:backgroundTint of each button.
Note that several UI elements also use the colours set in the theme (like colorPrimary) and they'll change if you change those values. There's more info about what the colours in the theme are used for here, here and here and more general info about themes and styles here.
please change in colors.xml in your resource file
Open values -> colors.xml and add the color you want and set it in the layout XML file

Get button-clicked color of theme

Is there a way, to get the button-clicked colour based on the current theme? The best would be a xml #color that I can use.
I am planning to use this colour for my ImageButtons when pressed.
If you just want to get a specific color one time, you could hold the button down on your device and screenshot it. Then open the image in MS Paint or something to grab the colors used.
I have found an workaround. When you set this as background, the colour is changed automatically by the operating system according to the theme as if it was a button.
android:background="?android:attr/selectableItemBackground"
If you want to use the attribute on pre API 11 devices you can use:
android:background="?attr/selectableItemBackground"

change button background at runtime in 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.

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

android how to change the progressbar color programatically

How can you change the progressbar color from within the app. I already have it set in the XML but as the value gets within preset thresholds I need to change the color of the bar to alert the user. For the buttons I can set it like the example below. Is there a way to do with with the progressbar?
B.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.button2));
B.getBackground().setColorFilter( UserS.ColorBtnBack, PorterDuff.Mode.MULTIPLY);
I posted a tip as a comment on your other question, see http://colintmiller.com/2010/10/07/how-to-add-text-over-a-progress-bar-on-android/
Scroll down to "Bonus Tip", which discusses changing color. I guess you want a predefined set of colors, which you could then switch between using setProgressDrawable()
ProgressBar uses a LevelListDrawable with setProgressDrawable() to determine the color of the bar. You can try assembling a LevelListDrawable in Java, though I have never tried that -- I have only defined them via XML.

Categories

Resources