Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text size within an PreferenceScreen

Tags:

android

I have xml file that defines some preference screens like the following example

<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android"          
   android:key="root_preferencescreen">

    <PreferenceScreen  android:key="general_sett" android:title="general settings" />
     ....

    <PreferenceScreen  android:key="extras_sett" android:title="extras settings" />

</PreferenceScreen> 

I would like to be able to increase the font size for the text of the preference screen , but because the within a preference screen there is no android:textsize tag , i have no idea how to accomplish that !

like image 575
klaus johan Avatar asked Aug 31 '10 17:08

klaus johan


2 Answers

You can simply add android:textSize inside your theme for preference screen:

e.g :

<style name="settingsTheme" parent="PreferenceThemeOverlay">
    <item name="colorAccent">@color/color_ten</item>
    <item name="android:background">@color/colorPrimaryLight</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
    <item name="android:textColorPrimary">@color/colorTextBodyLight</item>
    <item name="android:textColorSecondary">@color/colorTextCaptionLight</item>
    <item name="android:textSize">14sp</item>
</style>

Your SettingsActivity class :

public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    toolbar = (Toolbar)findViewById(R.id.toolbar_settings);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.bodylayout, new PrefsFragment())
            .commit();


}

@Override
protected void onPause() {
    super.onPause();
}
}

PrefsFragment class :

public class PrefsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        final Context myContext = this.getActivity();
    //set your theme
        myContext.setTheme(R.theme.settingsTheme);

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settingspreference);
    //do other stuffs
        }
    //.....
        }
like image 155
N Kaushik Avatar answered Oct 13 '22 13:10

N Kaushik


You can make a TextView layout xml that looks something like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/listSeparatorTextViewStyle"
    android:textColor="@android:color/white"
    android:id="@+android:id/title"
/>

and set the layout of your preference category in your preferences.xml like this:

<PreferenceCategory
    android:title="Category Title"
    android:layout="@layout/pref_category"
/>

As long as the TextView has the id @+android:id/title you can make the layout look however you want. There is also a way to do this with styles that I haven't quite figured out.

like image 37
Frank Avatar answered Oct 13 '22 12:10

Frank