Giter VIP home page Giter VIP logo

androidsliderpreference's Introduction

Android Slider Preference Library

Screenshot

Overview

  • Slider represents a float between 0.0 and 1.0
  • Supports multiple summaries (e.g. "Low", "Medium", "High") and selects one based on the slider's position
    • Java: SliderPreference.setSummary(CharSequence[] summaries)
    • XML: android:summary="@array/string_array_of_summaries"
    • A single String still works too
  • Subclass of DialogPreference
    • Supports all dialog-specific attributes such as android:dialogMessage
    • Visually-consistent with Android's built-in preferences
    • Less error-prone than displaying the slider directly on the settings screen
  • MIT License

How To Use

Add the library to your application

Before you can add a SliderPreference to your application, you must first add a library reference:

  1. Clone or download a copy of the library
  2. Import the library into Eclipse: File menu -> Import -> Existing Project into Workspace
  3. Open your application's project properties and add a library reference to "SliderPreference"

Add a slider to your application

<!-- preferences.xml -->
<net.jayschwa.android.preference.SliderPreference
    android:key="my_slider"
    android:title="@string/slider_title"
    android:summary="@array/slider_summaries"
    android:defaultValue="@string/slider_default"
    android:dialogMessage="@string/slider_message" />
<!-- strings.xml -->
<string name="slider_title">Temperature</string>
<string-array name="slider_summaries">
    <!-- You can define as many summaries as you'd like -->
    <!-- The active summary will reflect the preference's current value -->
    <item>Freezing</item> <!-- 0.00 to 0.25 -->
    <item>Chilly</item>   <!-- 0.25 to 0.50 -->
    <item>Warm</item>     <!-- 0.50 to 0.75 -->
    <item>Boiling</item>  <!-- 0.75 to 1.00 -->
</string-array>
<item name="slider_default" format="float" type="string">0.5</item>
<string name="slider_message">Optional message displayed in the dialog above the slider</string>

It is possible to define the default value directly in the attribute. The summary can also be a regular string, instead of a string array:

<net.jayschwa.android.preference.SliderPreference
    android:summary="This summary is static and boring"
    android:defaultValue="0.5" />

Background

Sliders are recommended by Android's official design documentation for specific types of settings:

Use this pattern for a setting where the range of values are not discrete and fall along a continuum.

Slider design pattern example

Despite this recommendation, the Android SDK does not actually provide a Preference with slider functionality. Various custom implementations can be found around the web, but many have issues such as:

  • The slider is displayed directly on the settings screen
    • Higher chance of accidental clicks
    • No way to confirm or cancel potential changes
  • Discrete values are displayed to the user
    • Not ideal for this design pattern

This library aims to be as consistent as possible with the design pattern and Android's built-in Preference implementations.

License

This library is licensed under the MIT License. A copy of the license is provided in LICENSE.txt:

Copyright 2012 Jay Weisskopf

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

androidsliderpreference's People

Contributors

jayschwa avatar

Watchers

william avatar James Cloos 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.