0

I have the following styles in styles.xml :

<style name="tabSelected">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:background">#0000ff</item>
</style>

<style name="tabUnselected">
    <item name="android:textColor">#0000ff</item>
    <item name="android:background">#FFFFFF</item>
</style>

And I have two TextViews in my activity. When I click the first TextView I want to add the style tabSelected to it and to remove tabUnselected from it and when I click the second TextView I want to add the style tabSelected to it and to remove tabUnselected from it:

I tried the following:

exhibitionAreaTextView.Click += delegate
{
    exhibitionAreaTextView.SetTextAppearance(Resource.Style.tabSelected);
    multipurposeRoomTextView.SetTextAppearance(Resource.Style.tabUnselected);
};

multipurposeRoomTextView.Click += delegate
{
    multipurposeRoomTextView.SetTextAppearance(Resource.Style.tabSelected);
    exhibitionAreaTextView.SetTextAppearance(Resource.Style.tabUnselected);
};

But it changed only the textColor of the TextViews. I've seen another solution:

ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
textView= new TextView(newContext);

But it is making a new instance of the TextViews and I don't know if its click event will work after that and more it is 2 lines solution, if it's going to be 2 lines, I better put:

multipurposeRoomTextView.SetTextColor(#ffffff);
multipurposeRoomTextView.SetBackgroundColor(#0000ff);

It will be more readable. Is it a way to set the style with one property programatically, something like that multipurposeRoomTextView.SetStyle(MyStyle)?

Montoolivo
  • 127
  • 2
  • 3
  • 13
  • 1
    Add `parent="@android:style/Widget.TextView"` to the `style` and use `SetTextAppearance` method. _same as your first attempt_ – Mehdi Dehghani Jan 28 '20 at 14:22
  • 1
    @Montoolivo, I search some article about TextView.SetTextAppearance Method, it seems that cannot be used to change all attributes, and ContextThemeWrapper can work when you create new view, and it seems that don't works for existing view, here is the same thread that you can take a look:https://stackoverflow.com/questions/11723881/android-set-view-style-programmatically – Cherry Bu - MSFT Jan 29 '20 at 07:24

0 Answers0