0

I am trying to set the size of ALL text views in my app to a certain size.

    public void rbFontClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.rbsmall:
                if (checked)
                    //eveysingletextview.setTextSize(12)
                    everysingleedittext.setTextSize(12)
                break;

            case R.id.rbmedium:
                if (checked)
                    // do nothing
                break;

            case R.id.rblarge:
                if (checked)
                    // set dimension to large font,
                break;

How would I go about referencing all the textviews? I need to do it for Edittexts aswell, so I don't want to individually reference each view, it would take forever.

TIA.

Daniel o Keeffe
  • 578
  • 2
  • 10
  • 25
  • you want to retrieve all the view in the current hierarchy and check if those are textview or edittext, have I understood correctly? – Blackbelt Nov 04 '13 at 15:58
  • 1
    You can use style XML files to define your view appearance properties (such as text size). See the following related SO question: http://stackoverflow.com/questions/3078081/setting-global-styles-for-views-in-android and also the Android documentation: http://developer.android.com/guide/topics/ui/themes.html – tiguchi Nov 04 '13 at 16:13
  • Well the idea is that the user clicks a radio button and it changes the font for all of the application to large, medium, or small. I should really have put that in the question. – Daniel o Keeffe Nov 04 '13 at 16:15

1 Answers1

1
int size = 10;
switch(view.getId()) {
            case R.id.rbsmall:
                if (checked)
                   size = 12;
                break;  

// other case

}

  ViewGroup root = (ViewGroup) findViewById(R.id.rootLayout);
  int children = root.getChildCount();
  for (int i = 0; i < children; i++) {
      View view  = root.getChildAt(i);
      if (view instanceOf TextView) {
        TextView tmp = (TextView)view;
        view.setText(size);
      }
  }

It is not elegant but it should work. Check for typo

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I understand that this will change all the text views to the size, and another if statement can be put it for edittexts. Is it just a case of findViewById(R.id.other_layout) for x amount of layouts to change it for the whole application, not just the current layout?? – Daniel o Keeffe Nov 04 '13 at 16:07
  • My assignment id due at midnight tonight - Don't worry about elegence:) – Daniel o Keeffe Nov 04 '13 at 16:08
  • just for the current layout of course. But you can think to create a static method and call it when you change activity. Imo this approch is wrong. YOu should use styles for those situation. – Blackbelt Nov 04 '13 at 16:16
  • Ok, i think I have enough to work with. I'm still new to android. I'm going to try and repeat the viewgroup 5 times within the if statements to change the text and edittext views.Don't see any reason at the moment why it won't work,just might be a bit messy. The idea is that the users can select a radion button to change the fontsize,fontcolour and background colour. I don't really understand how that would work using a static method when changing activity. Anyway thanks this is pretty much what i wanted. (y) – Daniel o Keeffe Nov 04 '13 at 16:28