1

I am creating several textviews that all use the same style. I am attempting to use a SeekBar to update the textsize within the Style so it applies to all textviews with a minimal amount of code. I know I can use a SeekBar to set the textsize of the textviews individually but that seems like a lot of work. The problem is that everywhere I look all I find is that you cannot change the style. Is there any other work around besides doing code like below:

Define my textviews

TextView tv1 = (TextView)findViewById(R.id.tv1); TextView tv2 = (TextView)findViewById(R.id.tv2); TextView tv3 = (TextView)findViewById(R.id.tv3); 

Inside my SeekBar

progress = seekBarProgress; if(progress == 0) { tv1.setTextSize(12); tv2.setTextSize(12); tv3.setTextSize(12); } if(progress == 1) { tv1.setTextSize(14); tv2.setTextSize(14); tv3.setTextSize(14); } 

Etc etc..

I would like to be able to change one attribute of a custom style. I cannot change it all together to a different custom style because I am going to do SeekBars for Text size, text color, background color, etc. If I did custom styles for each one there would be TONS.

Since I will have a lot of textviews doing this method seems illogical. Is there a better way? Thanks.

GOT THE ANSWER!

Instead of changing the style I retrieve the child and then the child of that child and change it accordingly like below.

LinearLayout masterLayout = (LinearLayout)findViewById(R.id.masterLayout); int childCount = masterLayout.getChildCount(); for(int i = 0; i < childCount; i++) { LinearLayout innerChild = ((LinearLayout)masterLayout.getChildAt(i)); int childOfChildCount = innerChild.getChildCount(); for(int x = 0; x < childOfChildCount; x++) { ((TextView)innerChild.getChildAt(x)).setTextSize(30); } } 
1
  • 1
    Styles are static xml files that are used to simplfy the design process of your apps. You cannot change the attributes of the style, you must to use the code you posted. Commented Dec 14, 2015 at 20:36

2 Answers 2

2

What about group these TextView in only one Layout? Then change it programmatically. In my example I group all of TextViews in only one LinearLayout.

LinearLayout ll = (LinearLayout) findViewById(R.id.layout); int childCount = ll.getChildCount(); for (int i=0; i<childCount; i++){ ((TextView)ll.getChildAt(i)).setTextSize(20); } 

Be sure that you only have TextViews in your layout.

Sign up to request clarification or add additional context in comments.

4 Comments

I think that may work. Ill have to modify that a bit. I have one master vertical linearlayout that holds a number of layouts. Each of those layouts hold the textviews. I could use this same logic to cycle through each layout and then the textviews within that layout correct?
It seems a lot of work, however it will work. You'll have to check what kind of instace you're handling. Like . childView instanceof LinearLayout .. or .. childView instanceof TextView ..
Beautiful! Came up with a short block of code to cycle through each Child and Child of child resulting in exactly what I need. I have posted my code above. Thanks!
Nice. You can also store it in a List of TextView .. List<TextView> but it will require a bit more of java code to find all ids. .
0

I know that you have already implemented and accepted a solution, however, I have been thinking about this for a while for myself, and have come up with an alternative, more generic solution which may be of use. This involves four elements

  1. Creating an interface for the style changed events
  2. Creating a handler for the style changed events
  3. Extending TextView to have one or more style changed events
  4. Triggering the style change events

Although this is more code it has the advantages of being independent of layouts, and of the view classes (ie the same handler can be used for different View Classes if you also wanted to change the font size of Buttons, EditTexts etc).

The example below just implements a text size change, but the same technique could be used to implement any other style changes.

The Interface

public interface StyleChange { void onTextSizeChanged(float size); } 

The Handler

public class TextStyleHandler { private static TextStyleHandler instance; private LinkedList<StyleChange> listeners = new LinkedList<>(); public static TextStyleHandler getInstance() { if (instance == null) instance = new TextStyleHandler(); return instance; } public void register(StyleChange item) { listeners.add(item); } public void unregister(StyleChange item) { listeners.remove(item); } public void setTextSize(float f) { for (StyleChange listener:listeners) listener.onTextSizeChanged(f); } } 

The Extended TextView

public class StyledTextView extends TextView implements StyleChange { public StyledTextView(Context cx) { super(cx); init(); } public StyledTextView(Context cx, AttributeSet attrs) { super(cx, attrs); init() } public StyledTextView(Context cx, AttributeSet attrs, int defStyle) { super(cx, attrs, defStyle); init(); } private void init() { // Any other setup here (eg setting the default size // or getting current value from shared preferences) TextStyleHandler.getInstance().register(this); } public void onTextSizeChanged(float size) { setTextSize(size); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); TextStyleHandler.getInstance().unregister(this); } } 

Triggering the style change event

This can be done from your activity, and will change the style of all registered views

TextStyleHandler.getInstance().setTextSize(size); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.