SoFunction
Updated on 2025-03-08

Android's example code to automatically adjust font size according to resolution


//Travel the font settings
public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//Passing in the top layer of the Activity Layout, screen width, screen height
        int adjustFontSize = adjustFontSize(screenWidth,screenHeight);
        for(int i = 0; i<(); i++ ){
            View v = (i);
            if(v instanceof ViewGroup){
                changeViewSize((ViewGroup)v,screenWidth,screenHeight);
}else if(v instanceof Button){//The button must be enlarged on the TextView, because Button also inherits TextView
                ( (Button)v ).setTextSize(adjustFontSize+2);
            }else if(v instanceof TextView){
if(()== .title_msg){//Top title
                    ( (TextView)v ).setTextSize(adjustFontSize+4);
                }else{
                    ( (TextView)v ).setTextSize(adjustFontSize);
                }
            }
        }
    }

 
//Get font size
public static int adjustFontSize(int screenWidth, int screenHeight) {
        screenWidth=screenWidth>screenHeight?screenWidth:screenHeight;
        /**
* 1. Get the view width in the onsizechanged of the view. Generally, the default width is 320, so calculate a scaling ratio
rate = (float) w/320 w is the actual width
2. Then when setting the font size ((int)(8*rate));   8 is the font size that needs to be set with a resolution width of 320
Actual font size = Default font size x rate
         */
int rate = (int)(5*(float) screenWidth/320); //I test this multiple myself, of course you can modify it after testing.
return rate<15?15:rate; //The font is too small and not good-looking
}

//Travel the font settings
public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//Passing in the top layer of the Activity Layout, screen width, screen height
  int adjustFontSize = adjustFontSize(screenWidth,screenHeight);
  for(int i = 0; i<(); i++ ){
   View v = (i);
   if(v instanceof ViewGroup){
    changeViewSize((ViewGroup)v,screenWidth,screenHeight);
}else if(v instanceof Button){//The button must be placed on the TextView, because Button also inherits TextView
    ( (Button)v ).setTextSize(adjustFontSize+2);
   }else if(v instanceof TextView){
if(()== .title_msg){//Top title
     ( (TextView)v ).setTextSize(adjustFontSize+4);
    }else{
     ( (TextView)v ).setTextSize(adjustFontSize);
    }
   }
  }
 }


//Get font size
public static int adjustFontSize(int screenWidth, int screenHeight) {
  screenWidth=screenWidth>screenHeight?screenWidth:screenHeight;
  /**
* 1. Get the view width in the onsizechanged of the view. Generally, the default width is 320, so calculate a scaling ratio
rate = (float) w/320 w is the actual width
2. Then when setting the font size ((int)(8*rate)); 8 is the font size that needs to be set with a resolution width of 320
Actual font size = Default font size x rate
   */
int rate = (int)(5*(float) screenWidth/320); //I test this multiple myself, of course you can modify it after testing
return rate<15?15:rate; //The font is too small and not good-looking
}