Android Open Source - app_list_widget Seek Bar Preference






From Project

Back to project page app_list_widget.

License

The source code is released under:

Apache License

If you think the Android project app_list_widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 */*www  .  j  a va 2s. com*/
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.acbelter.applistwidget.ui;

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.acbelter.applistwidget.R;

public final class SeekBarPreference extends DialogPreference implements OnSeekBarChangeListener {
    // Namespaces to read attributes
    private static final String PREFERENCE_NS =
            "http://schemas.android.com/apk/res/com.acbelter.applistwidget";
    private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android";

    // Attribute names
    private static final String ATTR_DEFAULT_VALUE = "defaultValue";
    private static final String ATTR_MIN_VALUE = "minValue";
    private static final String ATTR_MAX_VALUE = "maxValue";

    // Default values for defaults
    private static final int DEFAULT_CURRENT_VALUE = 50;
    private static final int DEFAULT_MIN_VALUE = 0;
    private static final int DEFAULT_MAX_VALUE = 100;

    // Real defaults
    private final int mDefaultValue;
    private final int mMaxValue;
    private final int mMinValue;

    // Current value
    private int mCurrentValue;

    // View elements
    private SeekBar mSeekBar;
    private TextView mValueText;

    public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Read parameters from attributes
        mMinValue = attrs.getAttributeIntValue(PREFERENCE_NS, ATTR_MIN_VALUE, DEFAULT_MIN_VALUE);
        mMaxValue = attrs.getAttributeIntValue(PREFERENCE_NS, ATTR_MAX_VALUE, DEFAULT_MAX_VALUE);
        mDefaultValue = attrs.getAttributeIntValue(ANDROID_NS, ATTR_DEFAULT_VALUE,
                DEFAULT_CURRENT_VALUE);
    }

    @Override
    protected View onCreateDialogView() {
        // Get current value from preferences
        if (isPersistent()) {
            mCurrentValue = getPersistedInt(mDefaultValue);
        }

        // Inflate layout
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.dialog_seek_bar, null);

        // Setup minimum and maximum text labels
        ((TextView) view.findViewById(R.id.min_value)).setText(Integer.toString(mMinValue));
        ((TextView) view.findViewById(R.id.max_value)).setText(Integer.toString(mMaxValue));

        // Setup SeekBar
        mSeekBar = (SeekBar) view.findViewById(R.id.seek_bar);
        mSeekBar.setMax(mMaxValue - mMinValue);
        mSeekBar.setProgress(mCurrentValue - mMinValue);
        mSeekBar.setOnSeekBarChangeListener(this);

        // Setup text label for current value
        mValueText = (TextView) view.findViewById(R.id.current_value);
        mValueText.setText(Integer.toString(mCurrentValue) + "%");

        return view;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        // Return if change was cancelled
        if (!positiveResult) {
            return;
        }

        // Persist current value if needed
        if (shouldPersist()) {
            persistInt(mCurrentValue);
        }

        // Notify activity about changes (to update preference summary line)
        notifyChanged();
    }

    @Override
    public CharSequence getSummary() {
        // Format summary string with current value
        int value = getPersistedInt(mCurrentValue);
        return value + "%";
    }

    @Override
    public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
        // Update current value
        mCurrentValue = value + mMinValue;
        // Update label with current value
        mValueText.setText(Integer.toString(mCurrentValue) + "%");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seek) {
        // Not used
    }

    @Override
    public void onStopTrackingTouch(SeekBar seek) {
        // Not used
    }

    public int getValue() {
        return mCurrentValue;
    }

    public void setValue(int value) {
        mCurrentValue = value;
    }
}




Java Source Code List

com.acbelter.applistwidget.ui.AppItem.java
com.acbelter.applistwidget.ui.AppListOrderAdapter.java
com.acbelter.applistwidget.ui.AppListSelectAdapter.java
com.acbelter.applistwidget.ui.AppOrderFragment.java
com.acbelter.applistwidget.ui.AppSelectFragment.java
com.acbelter.applistwidget.ui.ConfigActivity.java
com.acbelter.applistwidget.ui.SeekBarPreference.java
com.acbelter.applistwidget.ui.SettingsActivity.java
com.acbelter.applistwidget.ui.SettingsFragment.java
com.acbelter.applistwidget.widget.AppListLargeWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetFactory.java
com.acbelter.applistwidget.widget.AppListWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetService.java
com.acbelter.applistwidget.widget.Pref.java