Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – View.setSelected(bool) is not working when the view is updated programatically in different location

Posted on November 11, 2022 By

Solution 1 :

This is my understanding. When setColorFilter is used, it sets the color for the image and removes any pre-set tints for the images.

myImageView.setColorFilter(ContextCompat.getColor(mContext, newColor))

So even though setSelected(bool) is being called, nothing happens

myViewObject.setSelected(isSelected);

The right method was to use setTint or setTintList using ColorStateList

Here is an example of creating ColorStateList and then you can set it to the ImageView as below

myImageView.setImageTintList(myColorStateList);

Note: in the ColorStateList, the negative value is for opposite of the attribute. For example “android.R.attr.state_enabled” indicates selected state and “-android.R.attr.state_enabled” indicates unselected

Problem :

I am running into an issue where the Views I have in the app are not being updated on viewObject.setSelected(true). The View objects contain TextView or ImageView.

myViewObject.setSelected(isSelected);

The issue occurs when I am adding a new feature where the color of the view is changed programmatically using below on specific conditions/events.

TextView myTextView = myViewObject.findViewById(R.id.custom_text_view); 
ImageView myImageView = myViewObject.findViewById(R.id.custom_image_view); 
myTextView.setTextColor(ContextCompat.getColor(mContext, newColor)); 
myImageView.setColorFilter(ContextCompat.getColor(mContext, newColor));

What I am trying to do is keep the original functionality where the View objects’ color gets updated when setSelected(bool) is called as above, and also add a new functionality where the Views’ colors are changed based on events using setColorFilter.

What I have observed is that the ViewObject.setSelected() works as expected untill I add the code to update color programmatically using setColorFilter(color). After adding code for setColorFilter(color), it works as expected but the setSelected(bool) stops working.

Thanks in advance for your time and advice

READ  [FIXED] android - Navigate from one activity to another with Navigation Component
Powered by Inline Related Posts
Android Tags:android, android-view, imageview, view

Post navigation

Previous Post: [FIXED] android – Overlapping items in RelativeLayout with ImageView and TextView
Next Post: [FIXED] android – Unable to instantiate application com.company.app.MainApplication: java.lang.ClassNotFoundException React Native Expo

Related Posts

[FIXED] How to display custom dialog on Android home screen? Kotlin Android
[FIXED] android – How to ask multiple permissions from user in flutter? Android
[FIXED] Where does the android studio generate the release and debug apk? Android
[FIXED] android – how to enable button if 2 edit text is fill? Android
[FIXED] java – line breaks in string resource Android
[FIXED] android – How to load SVG to ImageView using Retrofit + Moshi? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme