float new_coef = 1.3f; // SharedPreferences settings = getSharedPreferences("MyAppSett", MODE_PRIVATE); Editor value_add = settings.edit(); // value_add.putFloat("size_coef", new_coef); value_add.commit();
package com.Example.Test; import android.app.Application; import android.content.SharedPreferences; public final class MyApp extends Application { private float size_coef; // // public float getSizeCoef() { return size_coef; } // @Override public void onCreate() { super.onCreate(); // SharedPreferences settings = getSharedPreferences("MyAppSett", MODE_PRIVATE); size_coef= settings.getFloat("size_coef", 1f); } }
<application android:name=".MyApp" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
package com.Example.Test; import android.app.Activity; import android.content.Context; import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; import android.util.TypedValue; // public final class MyTextView extends AppCompatTextView { // public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); InitSize(context); } // private void InitSize(Context context) { // Activity activity = (Activity) context; float koef = ((MyApp) activity.getApplication()).getSizeCoef(); float cur_size = getTextSize(); // // setTextSize(TypedValue.COMPLEX_UNIT_PX, cur_size * koef); } }
<com.Example.Test.MyTextView android:id="@+id/TextValue" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/TextColor" android:textSize="@dimen/TextSize" />
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="0" android:max="5" /> </LinearLayout>
final float start_value = 0.7f; // final float step = 0.15f; // int size_coef; // ... SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar); ... if (seekBar != null) { seekBar.setProgress(select_coef); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { size_coef = progress; // } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); }
// SharedPreferences settings = getSharedPreferences("MyAppSett", MODE_PRIVATE); SharedPreferences.Editor value_add = settings.edit(); // value_add.putInt("size_coef", size_coef); value_add.apply(); // Resources res = getResources(); float oef = start_value + size_coef * step; // Configuration configuration = new Configuration(res.getConfiguration()); configuration.fontScale = oef; res.updateConfiguration(configuration, res.getDisplayMetrics());
package com.Example.Test; import android.app.Application; import android.content.SharedPreferences; public final class MyApp extends Application { // @Override public void onCreate() { super.onCreate(); int size_coef; final float start_value = 0.7f; // final float step = 0.15f; // Resources res = getResources(); SharedPreferences settings = getSharedPreferences("MyAppSett", MODE_PRIVATE); try { // size_coef = settings.getInt("size_coef", 2); } catch (Exception e) { size_coef = 2; // } // float new_value = start_value + size_coef * step; // Configuration configuration = new Configuration(res.getConfiguration()); configuration.fontScale = new_value; res.updateConfiguration(configuration, res.getDisplayMetrics()); } }
<application android:name=".MyApp" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
Source: https://habr.com/ru/post/348368/
All Articles