0

as shown below in the code, i have some views and i want to add a selector color to the view, so that when the view is clicked its color changes. exactly as happens when one clicks on an item in a listview, that's what i am trying to do.

i referred to some posts and i troed both of the following:

view.setBackgroundResource(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color
view.setBackground(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color

but it doesnt work and AndroidStudio highlight them with red color.

please let me know how to add a selector color to my vies programmatically

code:

LayoutInflater inflator = this.getLayoutInflater();
    final View view = inflator.inflate(R.layout.versicherungs_docs_footer, null);

    RelativeLayout relLay = (RelativeLayout) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_mainContainer);
    final TextView texVieShowMore = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showMore);
    final TextView texVieShowLess = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showLess);
    final TextView texVieShowMoreArrow = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showMoreArrow);
    final TextView texVieShowLessArrow = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showLessArrow);

    view.setBackgroundResource(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color
    //view.setBackground(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • So do you want to change item's background once clicked? (and by item I'm refering to a listItem) – William Kinaan Dec 27 '16 at 13:23
  • yes i wantto change the color of the background once clciked..and my views in the posted code, are normal views as you can see..it is nt a listview – Amrmsmb Dec 27 '16 at 14:21
  • it should work with setBackgroundResource, can you add the full example? – petrumo Dec 27 '16 at 14:38
  • @petrumo have you checked [this question](http://stackoverflow.com/questions/4761686/how-to-set-background-color-of-an-activity-to-white-programmatically)? – William Kinaan Dec 27 '16 at 14:41
  • yes i have just checked it..but if you realized in my question, i want to set "android.R.attr.listChoiceBackgroundIndicator" as a background to the view when clciked..and i could not find answer for that in our website – Amrmsmb Dec 27 '16 at 15:37

1 Answers1

0

You need to get the drawable that points to android.R.attr.listChoiceBackgroundIndicator as suggested in this link and set it programatically.

// Create an array of the listChoiceBackgroundIndicator attribute
int[] attrs = new int[] { listChoiceBackgroundIndicator /* index 0 */};

// Obtain the styled attributes. 'themedContext' is a context with a
// theme, typically the current Activity (i.e. 'this')
TypedArray ta = themedContext.obtainStyledAttributes(attrs);

// To get the value of the 'listChoiceBackgroundIndicator' attribute that was
// set in the theme used in 'themedContext'. The parameter is the index
// of the attribute in the 'attrs' array. The returned Drawable
// is what you are after
Drawable drawableFromTheme = ta.getDrawable(0 /* index */);

// Then, free the resources used by TypedArray
ta.recycle();

// Finally, set drawable as background
view.setBackground(drawableFromTheme);
Community
  • 1
  • 1
José Carlos
  • 654
  • 6
  • 16
  • i tried your answer, actually i did not work there is no effect when the view is clicked moreover, it is available from API 16 while i am targeting API 15 – Amrmsmb Dec 29 '16 at 07:39