1

I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.

Could you tell me the proper way?

  • use the workaround and create the TextView again
  • forget the styles and use java methods to decorate existing TextView
  • something else
Community
  • 1
  • 1
Leos Literak
  • 8,805
  • 19
  • 81
  • 156

3 Answers3

2

Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.

Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.

When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one

    textView1.setVisibility(View.GONE);
    textView2.setVisibility(View.VISIBLE);
sonallux
  • 83
  • 5
1

You can try using setTextAppearance() on the textview. The link is: setTextAppearance

Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.

Use R.style.YourStyleName as the integer argument.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Athena
  • 476
  • 1
  • 4
  • 12
1

I used these two answers to make it work:

and the code is:

ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
Community
  • 1
  • 1
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • 1
    thanks finally some understanding of how to use the inflator approach to change style at runtime. but you have to ask, is it really possible that google still (6/10/16) has no dynamic styles?? – tom Jun 09 '16 at 22:29