Giter Site home page Giter Site logo

distilledviewprefs's Introduction

DistilledViewPrefs

Android Arsenal

Dialog for Android TextView to improve readability. Mostly used for applications where the main focus is to read. Spin-off from the blog https://medium.com/@qvdev/improving-readability-3baa2d943936

Please feel free to post feedback on any topic. Improvements, code related or suggestions. Fork it if you feel for it as well. :)

Screens

Alt text Alt text

Implementation example

Add this to your gradle build file

repositories {
    maven { url "https://github.com/QVDev/DistilledViewPrefs/raw/master/releases/" }
}

dependencies {
    compile 'distilledview.utils.qvdev.com.distilled:distilledview-lib:1.0'
}

Register for preference changes. Take the default as the library does as well

//Let your class implement
implements SharedPreferences.OnSharedPreferenceChangeListener

//Register for it
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mSharedPreferences.registerOnSharedPreferenceChangeListener(this);

Then initialize the TextView

    private void initTextView() {
        mDemoTextView = (TextView) findViewById(R.id.example_text_view);
        setTextViewSizeWithScale(mSharedPreferences.getFloat(DistilledPagePrefs.DISTILLED_PREF_USER_SET_FONT_SCALE, DistilledPagePrefs.DEFAULT_FONT_SCALE));
        setTextViewMode(mSharedPreferences.getInt(DistilledPagePrefs.DISTILLED_PREF_USER_SET_FONT_MODE, DistilledPagePrefs.DEFAULT_FONT_MODE));
        setTextViewTypeFace(mSharedPreferences.getInt(DistilledPagePrefs.DISTILLED_PREF_USER_SET_FONT_TYPEFACE, DistilledPagePrefs.DEFAULT_TYPE_FACE));
    }

And now the listeners including example to change the TextView

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String preferenceName) {
        if (preferenceName.contentEquals(DistilledPagePrefs.DISTILLED_PREF_USER_SET_FONT_SCALE)) {
            float newValue = sharedPreferences.getFloat(preferenceName, 1);
            setTextViewSizeWithScale(newValue);
        } else if (preferenceName.contentEquals(DistilledPagePrefs.DISTILLED_PREF_USER_SET_FONT_MODE)) {
            setTextViewMode(sharedPreferences.getInt(preferenceName, DistilledPagePrefs.DEFAULT_FONT_MODE));
        } else if (preferenceName.contentEquals(DistilledPagePrefs.DISTILLED_PREF_USER_SET_FONT_TYPEFACE)) {
            setTextViewTypeFace(sharedPreferences.getInt(preferenceName, DistilledPagePrefs.DEFAULT_TYPE_FACE));
        }
    }

    private void setTextViewSizeWithScale(float value) {
        mDemoTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,
                DEFAULT_FONT_SIZE * value);
    }

    private void setTextViewMode(int mode) {
        mDemoTextView.setBackgroundResource(DistilledPagePrefsView.getBackgroundColorFromMode(mode));
        mDemoTextView.setTextColor(getResources().getColor(DistilledPagePrefsView.getTexColorFromMode(mode)));
    }

    private void setTextViewTypeFace(int typeFace) {
        mDemoTextView.setTypeface(DistilledPagePrefsView.getTypeFaceForPosition(typeFace));
    }

And last but not least call the dialog where you want it via

public void showDistilledDialog() {
        DistilledPagePrefsView.showDialog(this);
    }

distilledviewprefs's People

Contributors

qvdev avatar

Watchers

Michael jentsch avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.