Android Open Source - android-gskbyte-utils Inline Seek Bar Preference






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils 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

/*******************************************************************************
 * Copyright (c) 2013 Jose Alcal Correa.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * /* www .  j  ava 2 s  .  c o m*/
 * Contributors:
 *     Jose Alcal Correa - initial API and implementation
 ******************************************************************************/
package org.gskbyte.preferences;

import org.gskbyte.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class InlineSeekBarPreference extends Preference implements
        OnSeekBarChangeListener
{

    private final String TAG = InlineSeekBarPreference.class.getSimpleName();

    private static final String NS_ANDROID = "http://schemas.android.com/apk/res/android";
    private static final String NS_GSKBYTE = "http://www.gskbyte.net/";
    private static final int DEFAULT_VALUE = 50;

    private int maxValue = 100;
    private int minValue = 0;
    private int interval = 1;
    private int currentValue;
    private String unitsLeft = "";
    private String unitsRight = "";
    private SeekBar seekBar;

    private TextView statusText;

    public InlineSeekBarPreference(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initPreference(context, attrs);
    }

    public InlineSeekBarPreference(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
        initPreference(context, attrs);
    }

    private void initPreference(Context context, AttributeSet attrs)
    {
        setValuesFromXml(attrs);
        seekBar = new SeekBar(context, attrs);
        seekBar.setMax(maxValue - minValue);
        seekBar.setOnSeekBarChangeListener(this);
    }

    private void setValuesFromXml(AttributeSet attrs)
    {
        maxValue = attrs.getAttributeIntValue(NS_ANDROID, "max", 100);
        minValue = attrs.getAttributeIntValue(NS_GSKBYTE, "min", 0);

        unitsLeft = getAttributeStringValue(attrs, NS_GSKBYTE, "unitsLeft",  "");
        String units = getAttributeStringValue(attrs, NS_GSKBYTE, "units", "");
        unitsRight = getAttributeStringValue(attrs, NS_GSKBYTE, "unitsRight", units);

        try {
            String newInterval = attrs.getAttributeValue(NS_GSKBYTE,
                    "interval");
            if (newInterval != null)
                interval = Integer.parseInt(newInterval);
        } catch (Exception e) {
            Log.e(TAG, "Invalid interval value", e);
        }

    }

    private String getAttributeStringValue(AttributeSet attrs,
            String namespace, String name, String defaultValue)
    {
        String value = attrs.getAttributeValue(namespace, name);
        if (value == null)
            value = defaultValue;

        return value;
    }

    @Override
    protected View onCreateView(ViewGroup parent)
    {

        RelativeLayout layout = null;

        try {
            LayoutInflater mInflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            layout = (RelativeLayout) mInflater.inflate(
                    R.layout.inline_seekbar_preference, parent, false);
        } catch (Exception e) {
            Log.e(TAG, "Error creating seek bar preference", e);
        }

        return layout;

    }

    @Override
    public void onBindView(View view)
    {
        super.onBindView(view);

        try {
            // move our seekbar to the new view we've been given
            ViewParent oldContainer = seekBar.getParent();
            ViewGroup newContainer = (ViewGroup) view
                    .findViewById(R.id.seekBarPrefBarContainer);

            if (oldContainer != newContainer) {
                // remove the seekbar from the old view
                if (oldContainer != null) {
                    ((ViewGroup) oldContainer).removeView(seekBar);
                }
                // remove the existing seekbar (there may not be one) and add
                // ours
                newContainer.removeAllViews();
                newContainer.addView(seekBar,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        } catch (Exception ex) {
            Log.e(TAG, "Error binding view: " + ex.toString());
        }

        updateView(view);
    }

    /**
     * Update a SeekBarPreference view with our current state
     * 
     * @param view
     */
    protected void updateView(View view)
    {

        try {
            RelativeLayout layout = (RelativeLayout) view;

            statusText = (TextView) layout.findViewById(R.id.seekBarPrefValue);
            statusText.setText(String.valueOf(currentValue));
            statusText.setMinimumWidth(30);

            seekBar.setProgress(currentValue - minValue);

            TextView unitsTextRight = (TextView) layout
                    .findViewById(R.id.seekBarPrefUnitsRight);
            unitsTextRight.setText(unitsRight);

            TextView unitsTextLeft = (TextView) layout
                    .findViewById(R.id.seekBarPrefUnitsLeft);
            unitsTextLeft.setText(unitsLeft);

        } catch (Exception e) {
            Log.e(TAG, "Error updating seek bar preference", e);
        }

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser)
    {
        int newValue = progress + minValue;

        if (newValue > maxValue)
            newValue = maxValue;
        else if (newValue < minValue)
            newValue = minValue;
        else if (interval != 1 && newValue % interval != 0)
            newValue = Math.round(((float) newValue) / interval) * interval;

        // change rejected, revert to the previous value
        if (!callChangeListener(newValue)) {
            seekBar.setProgress(currentValue - minValue);
            return;
        }

        // change accepted, store it
        currentValue = newValue;
        statusText.setText(String.valueOf(newValue));
        persistInt(newValue);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar)
    {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar)
    {
        notifyChanged();
    }

    @Override
    protected Object onGetDefaultValue(TypedArray ta, int index)
    {
        int defaultValue = ta.getInt(index, DEFAULT_VALUE);
        return defaultValue;
    }
    
    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
    {

        if (restoreValue) {
            currentValue = getPersistedInt(currentValue);
        } else {
            int temp = 0;
            try {
                temp = (Integer) defaultValue;
            } catch (Exception ex) {
                Log.e(TAG, "Invalid default value: " + defaultValue.toString());
            }

            persistInt(temp);
            currentValue = temp;
        }

    }
}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java