I have created Spinner with the list of Font Sizes from "8" to "46" . I could be able to click the font Size and in a spinner it has shown me .
My need is, if i click the Font Size "26" inside a Spinner then it should be applied to my whole project. Like applying to my Screen, Textview appearance, Edittext - Bold/ Italic, etc. Again if i click 46 size then it should be apply to my whole project.
How could i do this by programmatically ?
To set Android Button font/text size, we can set android:textSize attribute for Button in layout XML file. To programmatically set or change Android Button font/text size, we can pass specified size to the method Button. setTextSize(specific_size).
A TextView in Android is a UI element to display text. It can be programmed in the layout file statically as well as in the main code dynamically. Thus, various attributes of a TextView such as the text, text color, text size, TextView background, and its size can be changed programmatically.
Go to File -> Settings -> Editor -> Font -> Size.
Android
documentation is not specific on the most efficient way to change the font size globally through the user’s selection at application level.
There is a problem I think with the answer given by Black Devil.
The problem is that many of the Android
widgets subclass TextView
, such as Button
, RadioButton
, and CheckBox
. Some of these are indirect subclasses of TextView
, which makes implementing customized version of TextView
in these classes very difficult.
However as pointed out by Siddharth Lele in his comment, using styles
or themes
is much better way to handle change in the text size throughout the app.
We set styles for layouts to control the look and feel of the view. Themes are essentially just collections of these styles. However, we can use a theme just for text size settings; without defining values for every property. Using a theme over styles provides us with one huge advantage: we can set a theme for the entire view programmatically.
theme.xml
<resources> <style name="FontSizeSmall"> <item name="android:textSize">12sp</item> </style> <style name="FontSizeMedium"> <item name="android:textSize">16sp</item> </style> <style name="FontSizeLarge"> <item name="android:textSize">20sp</item> </style> </resources>
Create a class to handle loading our preferences:
public class BaseActivity extends Activity { @Override public void onStart() { super.onStart(); // Enclose everything in a try block so we can just // use the default view if anything goes wrong. try { // Get the font size value from SharedPreferences. SharedPreferences settings = getSharedPreferences("com.example.YourAppPackage", Context.MODE_PRIVATE); // Get the font size option. We use "FONT_SIZE" as the key. // Make sure to use this key when you set the value in SharedPreferences. // We specify "Medium" as the default value, if it does not exist. String fontSizePref = settings.getString("FONT_SIZE", "Medium"); // Select the proper theme ID. // These will correspond to your theme names as defined in themes.xml. int themeID = R.style.FontSizeMedium; if (fontSizePref == "Small") { themeID = R.style.FontSizeSmall; } else if (fontSizePref == "Large") { themeID = R.style.FontSizeLarge; } // Set the theme for the activity. setTheme(themeID); } catch (Exception ex) { ex.printStackTrace(); } }
Finally, create activities by extending BaseActivity, like this:
public class AppActivity extends BaseActivity{ }
As most of the application have a much fewer amount of Activities than TextViews or widgets that inherit TextView. This will be exponentially so as complexity increases, so this solution requires less changes in code.
Thanks to Ray Kuhnell
you can scale to text size of your app up/down using base activity Configuration, make all activities inherent base activity.
Scale normal value is 1.0, 2.0 would double the font size and .50 would make it half.
public void adjustFontScale( Configuration configuration,float scale) { configuration.fontScale = scale; DisplayMetrics metrics = getResources().getDisplayMetrics(); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(metrics); metrics.scaledDensity = configuration.fontScale * metrics.density; getBaseContext().getResources().updateConfiguration(configuration, metrics); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); adjustFontScale( getResources().getConfiguration()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With