- Android: change background color View onClick Button
- 7 Answers 7
- How to change color of ListView items on focus and on click
- 7 Answers 7
- Android: Change the background color of a ClickableSpan when clicked
- 3 Answers 3
- Not the answer you’re looking for? Browse other questions tagged android or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Change vector asset color programmatically when clicked
- 2 Answers 2
- Buttons
- Responding to Click Events
- Kotlin
- Using an OnClickListener
- Kotlin
- Styling Your Button
- Borderless button
- Custom background
Android: change background color View onClick Button
How I can change the background color to a View this under a button when I click the button ? I’ve tried a selector does not work because the View not change the color. What is the problem?
This is my code:
XML
JAVA
linea_verde
EDIT:
But the code not work
7 Answers 7
You are wrongly using drawable use this one one
as you say change the background color to a View this under a button when I click the button
setBackgroundColor() is for colors only, but it seems your using a state list drawable. If you are sure you want to use a different color depending on the Button ‘s state, set the state list drawable using this code:
Otherwise, just set the background color using
But in this case, use a click listener, and also make sure to actually use a color resource, e.g.:
You can also do like this.
If you would like to apply default background of selected item you could use background attribute with android ?attr .
selectableItemBackground attribute will use proper drawable according to Android version. On pre-lollipop it will use solid background, but on lollipop devices the ripple effect will be used.
Code from: com.android.support/appcompat-v7/23.0.0/res/values/values.xml
I think you were comparing the view the wrong way. I just happened to stumble to this question and since it is not marked as answered, this might help others.
Create two separate files for your shapes and change the background of your view after button click:
Источник
How to change color of ListView items on focus and on click
i have a list View in my app (this is the xml layout):
Each item of my list View is composed of two TextView:
I’m populating my listView from an ArrayAdapter, in this way:
My goal is to be able to change the text color and the backgorund of these child views whenever the parent is selected or pressed.
How can i do it?
7 Answers 7
The child views in your list row should be considered selected whenever the parent row is selected, so you should be able to just set a normal state drawable/color-list on the views you want to change, no messy Java code necessary. See this SO post.
Specifically, you’d set the textColor of your textViews to an XML resource like this one:
In your main.xml include the following in your ListView:
Here’s a good article on how to use selectors with lists.
Instead of setting it to be the android:background of the ListView, I believe you want to set android:listSelector as shown below:
Very old but I have just struggled with this, this is how I solved it in pure xml. In res/values/colors.xml I added three colours (the colour_. );
In the res/drawable folder I created listview_colours.xml which contained;
In the main_activity.xml find the List View and add the drawable to listSelector;
Play with the state_. items in the listview_colours.xml to get the effect you want.
There is also a method where you can set the style of the List View but I never managed to get it to work
Источник
Android: Change the background color of a ClickableSpan when clicked
I have a SpannableString with a ClickableSpan as follows
what I want to do is to change the foreground color of the span when clicked.
how can I do this ?
3 Answers 3
If you’re looking to get rid of the green highlight on selection, this is what you want to know:
Apparently, overriding public void updateDrawState(TextPaint ds) in your custom class would not affect the highlight color. It is only used for setting the underline color (or hiding/showing it).
All you need to do is: textView.setHighlightColor(Color.TRANSPARENT); where textView is what contains the ClickableSpan.
Hope it works for all of you. Feel free to ask any related question.
I extended the clickableSpan class and passed it a flag that lets me know that I should highlight it.
Not the answer you’re looking for? Browse other questions tagged android or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Change vector asset color programmatically when clicked
I have created the following vector asset and inserted it into an Imagebutton .
I had like that the heart will change its color to red once it was clicked and return to transparent once it was clicked again.
I couldn’t find any place that describes how to change the color of the whole heart. The closest solution I found was:
However, it changes only the border of the heart and not the whole shape.
SOLUTION:
Eventually what I used to solve this problem is to create as follows:
and inside my code:
2 Answers 2
The simplest way to solve this problem, if you want to change minor things like fill/stroke color or stroke width at runtime, is two have two versions of the same vector image with this differences, for example, one of them has only stroke color and the other one is filled:
Then in your code, switch between the two files when the user clicks:
Another way is to use one of the ready-made libraries. RichPathAnimator one of the best libraries, it’s support for changing vectors color, stroke width and even to animate vector graphics at runtime.
How to use it? First, add the library dependency to your app build.gradle. Then in your vector.xml give a name to the path that you want to change so:
In your layout replace Imagebutton with com.richpath.RichPathView and inside the vector attribute pass your vector resource in that tag:
In your Activity get a reference to the richpathview then call findRichPathByName(YOUR_PATH_NAME)
If don’t want to use RichPathView instead of your ImageView or ImageButton , and you want to change some sort of things in your vector path, then consider using VectorChildFinder.
Источник
Buttons
A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.
Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:
- With text, using the Button class:
- With an icon, using the ImageButton class:
- With text and an icon, using the Button class with the android:drawableLeft attribute:
Key classes are the following:
Responding to Click Events
When the user clicks a button, the Button object receives an on-click event.
To define the click event handler for a button, add the android:onClick attribute to the element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
For example, here’s a layout with a button using android:onClick :
Within the Activity that hosts this layout, the following method handles the click event:
Kotlin
The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:
- Be public
- Return void
- Define a View as its only parameter (this will be the View that was clicked)
Using an OnClickListener
You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.
To declare the event handler programmatically, create an View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener) . For example:
Kotlin
Styling Your Button
The appearance of your button (background image and font) may vary from one device to another, because devices by different manufacturers often have different default styles for input controls.
You can control exactly how your controls are styled using a theme that you apply to your entire application. For instance, to ensure that all devices running Android 4.0 and higher use the Holo theme in your app, declare android:theme=»@android:style/Theme.Holo» in your manifest’s element. Also read the blog post, Holo Everywhere for information about using the Holo theme while supporting older devices.
To customize individual buttons with a different background, specify the android:background attribute with a drawable or color resource. Alternatively, you can apply a style for the button, which works in a manner similar to HTML styles to define multiple style properties such as the background, font, size, and others. For more information about applying styles, see Styles and Themes.
Borderless button
One design that can be useful is a «borderless» button. Borderless buttons resemble basic buttons except that they have no borders or background but still change appearance during different states, such as when clicked.
To create a borderless button, apply the borderlessButtonStyle style to the button. For example:
Custom background
If you want to truly redefine the appearance of your button, you can specify a custom background. Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button’s current state.
You can define the state list in an XML file that defines three different images or colors to use for the different button states.
To create a state list drawable for your button background:
- Create three bitmaps for the button background that represent the default, pressed, and focused button states.
To ensure that your images fit buttons of various sizes, create the bitmaps as Nine-patch bitmaps.
Источник