Android Open Source - Gloomy-Dungeons-3D Seek Bar Preference






From Project

Back to project page Gloomy-Dungeons-3D.

License

The source code is released under:

MIT License

If you think the Android project Gloomy-Dungeons-3D 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

/*
 * The following code was written by Matthew Wiggins
 * and is released under the APACHE 2.0 license
 */*from   w  w  w.j  a  va  2s .c  o m*/
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * http://android.hlidskialf.com/blog/code/android-seekbar-preference
 * http://www.bryandenny.com/index.php/2010/05/25/what-i-learned-from-writing-my-first-android-application/
 *
 * Modified by restorer (added/fixed min parameter, fixed "Cancel" behaviour)
 *
 */

package zame.libs;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import zame.game.R;

public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener
{
  private static final String androidns = "http://schemas.android.com/apk/res/android";

  private Context mContext;
  private SeekBar mSeekBar;
  private TextView mSplashText;
  private TextView mValueText;

  private String mDialogMessage;
  private int mMin;
  private int mMax;
  private int mValue = 0;
  private String mSummary = "%s/%s";

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

    mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");

    TypedArray app = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference);
    mMin = app.getInt(R.styleable.SeekBarPreference_min, 0);

    mMax = attrs.getAttributeIntValue(androidns, "max", 100);
  }

  @Override
  protected View onCreateDialogView()
  {
    LinearLayout.LayoutParams params;
    LinearLayout layout = new LinearLayout(mContext);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setPadding(10, 10, 10, 10);

    mSplashText = new TextView(mContext);

    if (mDialogMessage != null) {
      mSplashText.setText(mDialogMessage);
    }

    layout.addView(mSplashText);

    mValueText = new TextView(mContext);
    mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
    mValueText.setTextSize(32);

    params = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT,
      LinearLayout.LayoutParams.WRAP_CONTENT
    );

    layout.addView(mValueText, params);

    mSeekBar = new SeekBar(mContext);
    mSeekBar.setOnSeekBarChangeListener(this);
    layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    mSeekBar.setMax(mMax - mMin);
    mSeekBar.setProgress(mValue - mMin);
    mValueText.setText(String.valueOf(mValue));

    return layout;
  }

  @Override
  protected void onBindDialogView(View v)
  {
    super.onBindDialogView(v);
    mSeekBar.setMax(mMax - mMin);
    mSeekBar.setProgress(mValue - mMin);
  }

  @Override
  protected Object onGetDefaultValue(TypedArray a, int index)
  {
    return a.getInteger(index, 0);
  }

  @Override
  protected void onSetInitialValue(boolean restore, Object defaultValue)
  {
    super.onSetInitialValue(restore, defaultValue);

    mValue = (restore ? getPersistedInt(mValue) : ((defaultValue == null) ? mValue : (Integer)defaultValue));

    if (!restore && shouldPersist()) {
      persistInt(mValue);
    }

    setSummary(String.format(mSummary, String.valueOf(mValue), String.valueOf(mMax)));
  }

  public void onProgressChanged(SeekBar seek, int value, boolean fromTouch)
  {
    value += mMin;

    mValueText.setText(String.valueOf(value));
    callChangeListener(new Integer(value));
  }

  @Override
  protected void onDialogClosed(boolean positiveResult)
  {
    if (positiveResult && (mSeekBar != null))
    {
      mValue = mSeekBar.getProgress() + mMin;
      setSummary(String.format(mSummary, String.valueOf(mValue), String.valueOf(mMax)));

      if (shouldPersist()) {
        persistInt(mValue);
      }
    }
  }

  public void onStartTrackingTouch(SeekBar seek) {}
  public void onStopTrackingTouch(SeekBar seek) {}

  public void setMin(int min)
  {
    mMin = min;
  }

  public int getMin()
  {
    return mMin;
  }

  public void setMax(int max)
  {
    mMax = max;
  }

  public int getMax()
  {
    return mMax;
  }

  public void setProgress(int progress)
  {
    mValue = progress;

    if (mSeekBar != null) {
      mSeekBar.setProgress(progress - mMin);
    }
  }

  public int getProgress()
  {
    return mValue;
  }
}




Java Source Code List

zame.game.AppConfig.java
zame.game.AppConfig.java
zame.game.Common.java
zame.game.ConfigZeemote.java
zame.game.ConfigZeemote.java
zame.game.Config.java
zame.game.GameActivityZeemoteHelper.java
zame.game.GameActivityZeemoteHelper.java
zame.game.GameActivity.java
zame.game.GamePreferencesActivity.java
zame.game.GamePreferencesActivity.java
zame.game.MenuActivityHelper.java
zame.game.MenuActivityHelper.java
zame.game.MenuActivity.java
zame.game.Renderer.java
zame.game.SoundManager.java
zame.game.ZameApplicationAnalyticsHelper.java
zame.game.ZameApplicationAnalyticsHelper.java
zame.game.ZameApplication.java
zame.game.ZameGame.java
zame.game.ZameJniRenderer.java
zame.game.engine.Action.java
zame.game.engine.AutoWall.java
zame.game.engine.Controls.java
zame.game.engine.Door.java
zame.game.engine.GameHelper.java
zame.game.engine.GameHelper.java
zame.game.engine.Game.java
zame.game.engine.Labels.java
zame.game.engine.LevelConfig.java
zame.game.engine.LevelRenderer.java
zame.game.engine.Level.java
zame.game.engine.Mark.java
zame.game.engine.Monster.java
zame.game.engine.Overlay.java
zame.game.engine.PortalTracer.java
zame.game.engine.State.java
zame.game.engine.Stats.java
zame.game.engine.TextureLoader.java
zame.game.engine.Weapons.java
zame.game.views.EndLevelView.java
zame.game.views.GameOverView.java
zame.game.views.GameView.java
zame.game.views.IZameView.java
zame.game.views.MenuViewHelper.java
zame.game.views.MenuViewHelper.java
zame.game.views.MenuView.java
zame.game.views.PreLevelView.java
zame.game.views.ZameGameView.java
zame.libs.FrameLayout.java
zame.libs.GLSurfaceView21.java
zame.libs.Grid.java
zame.libs.KeyMapPreference.java
zame.libs.LabelMaker.java
zame.libs.ListPreference.java
zame.libs.NumericSprite.java
zame.libs.SeekBarPreference.java
zame.promo.PromoView.java