Question

I have used this tutorial https://code.google.com/p/range-seek-bar/#Example_usage_as_Integer_range?.
Encountered runtime error of my activity stopped. Would like to seek help from you. I attempted to correct the following compile errors as listed below.

RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(20, 75, context);
  1. Context cannot be resolved to a variable

I tried adding changing context to this.

Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
  1. TAG cannot be resolved to a variable.

I tried adding "protected static final String TAG = null;" to main activity.

ViewGroup layout = (ViewGroup) findViewById(<your-layout-id>)
  1. Does the layout id refer to my main_activity.xml in my layout?

Really grateful for your feedback.

MainActivity.Java

package com.example.rangeseekbargooglecode;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.ViewGroup;

import com.example.rangeseekbargooglecode.RangeSeekBar.OnRangeSeekBarChangeListener;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String TAG = null;
        // create RangeSeekBar as Integer range between 20 and 75
        RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(20, 75, this);
        seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
                @Override
                public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                        // handle changed range values
                        Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
                }
        });

        // add RangeSeekBar to pre-defined layout
        ViewGroup layout = (ViewGroup) findViewById(R.layout.activity_main);
        layout.addView(seekBar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="109dp" />

</RelativeLayout>

Edited Code to allow TextView to display range.

public class MainActivity extends Activity {

    private TextView textview;
    protected static final String TAG = "com.example.gto_doubleseekbar";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview = (TextView) findViewById(R.id.textView1);

        // create RangeSeekBar as Integer range between 20 and 75
        RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(20, 75, this);

        seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
                @Override
                public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                        // handle changed range values
                    String powerranger = "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue;
                    Log.i(TAG, powerranger);
                    textview.setText(powerranger);  
                }
        });

        // add RangeSeekBar to pre-defined layout
        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.activity_main,null);
        layout.addView(seekBar);
        setContentView(layout);
    }
Était-ce utile?

La solution

  • RangeSeekBar seekBar = new RangeSeekBar(20, 75, context); 1. Context cannot be resolved to a variable

    this must work:

    RangeSeekBar seekBar = new RangeSeekBar(20, 75, this);
    
  • Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue); 2. TAG cannot be resolved to a variable. I tried adding "protected static final String TAG = null;" to main activity.

    Don't set it to null. Usually you use the app or component name, e.g.

    protected static final String TAG = "MyApp";
    
  • ViewGroup layout = (ViewGroup) findViewById() 3. Does the layout id refer to my main_activity.xml in my layout?

    Use this:

    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService      (Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.activity_main,null);
    layout.addView(seekBar);
    
    setContentView(layout);
    

Autres conseils

Try to implement custom RangeSeekBar.

xml file:

 <com.doondoz.utility.common_function.RangeSeekBar
                android:id="@+id/seekBarPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:left_index="0"
                android:layout_weight="0.90" />

java file:

RangeSeekBar.java

public class RangeSeekBar extends View {

private static final int DEFAULT_HEIGHT = 70;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_TICK_COUNT = 100;

private Thumb leftThumb, rightThumb;
private Thumb pressedThumb = null;
private SeekBar seekBar;
private Paint thumbPaint;

private OnRangeSeekBarChangerListener mListener;

private int mTickCount;
private int mLeftIndex = 0;
private int mRightIndex;
private int mThumbColor;
private int mThumbNormalRadius;
private int mThumbPressedRadius;

public RangeSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

public RangeSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

public RangeSeekBar(Context context) {
    super(context);
}

@SuppressWarnings("deprecation")
private void init(Context context, AttributeSet attrs) {
    Resources resources = getResources();

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RangeSeekBar);

    try {
        mTickCount = typedArray.getInteger(R.styleable.RangeSeekBar_tick_count, DEFAULT_TICK_COUNT);
        mRightIndex = mTickCount - 1;

        mThumbColor = typedArray.getColor(R.styleable.RangeSeekBar_thumb_color, getResources().getColor(R.color.thumb_default));
        mThumbNormalRadius = typedArray.getDimensionPixelSize(R.styleable.RangeSeekBar_thumb_normal_radius, 12);
        mThumbPressedRadius = typedArray.getDimensionPixelSize(R.styleable.RangeSeekBar_thumb_pressed_radius, 16);
        mLeftIndex = typedArray.getInteger(R.styleable.RangeSeekBar_left_index, 0);
        mRightIndex = typedArray.getInteger(R.styleable.RangeSeekBar_right_index, mRightIndex);

        if (mLeftIndex < 0)
            throw new IllegalArgumentException("Left index must be >= 0");

        if (mRightIndex > mTickCount)
            throw new IllegalArgumentException("Right index must be <= tick count");

    } finally {
        typedArray.recycle();
    }
    setUp();
}

private void setUp() {
    thumbPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    thumbPaint.setStyle(Paint.Style.FILL);
    thumbPaint.setColor(mThumbColor);

    leftThumb = new Thumb(0, 0, thumbPaint, mThumbNormalRadius, mThumbPressedRadius);
    rightThumb = new Thumb(0, 0, thumbPaint, mThumbNormalRadius, mThumbPressedRadius);

    seekBar = new SeekBar(0, 0, 0, Color.BLACK, 1, mThumbColor, 3);
    seekBar.setTickNumb(mTickCount);

}

public void setOnRangeBarChangeListener(OnRangeSeekBarChangerListener onRangeBarChangeListener) {
    mListener = onRangeBarChangeListener;
}

/**
 * Set number of ticks
 *
 * @param tickCount Default is 100
 */
public void setTickCount(int tickCount) {
    mTickCount = tickCount;
    seekBar.setTickNumb(mTickCount);
    invalidate();
}


/**
 * Set thumb's color
 *
 * @param thumbColor Default is orange
 */
public void setThumbColor(int thumbColor) {
    mThumbColor = thumbColor;
    thumbPaint.setColor(mThumbColor);
    invalidate();
}

/**
 * Set thumb's normal radius
 *
 * @param thumbRadius Default is 6dp
 */
public void setThumbNormalRadius(float thumbRadius) {
    mThumbNormalRadius = (int) (thumbRadius*getResources().getDisplayMetrics().density);
    leftThumb.radius = mThumbNormalRadius;
    rightThumb.radius = mThumbNormalRadius;
    invalidate();
}

/**
 * Set thumb's pressed radius
 *
 * @param thumbPressedRadius Default is 8dp
 */
public void setThumbPressedRadius(float thumbPressedRadius) {
    mThumbPressedRadius = (int) (thumbPressedRadius*getResources().getDisplayMetrics().density);
    leftThumb.pressedRadius = mThumbPressedRadius;
    rightThumb.pressedRadius = mThumbPressedRadius;
    invalidate();
}

/**
 * Set index for the Left Thumb
 *
 * @param leftIndex Default is 0
 */
public void setLeftIndex(int leftIndex) {
    if (leftIndex < 0) {
        throw new IllegalArgumentException("Left index must be >= 0");
    }
    mLeftIndex = leftIndex;
    leftThumb.setIndex(seekBar, mLeftIndex);
    invalidate();
}

/**
 * Set index for the Right thumb
 *
 * @param rightIndex Default is 99
 */
public void setRightIndex(int rightIndex) {
    if (rightIndex > mTickCount) {
        throw new IllegalArgumentException("Left index must be <= tick count");
    }
    mRightIndex = rightIndex;
    leftThumb.setIndex(seekBar, mRightIndex);
    invalidate();
}

/**
 * Get left index
 *
 * @return int
 */
public int getLeftIndex() {
    return mLeftIndex;
}

/**
 * Get right index
 *
 * @return int
 */
public int getRightIndex() {
    return mRightIndex;
}

/**
 * Get number of tick
 *
 * @return int
 */
public int getTickCount() {
    return mTickCount;
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    seekBar.draw(canvas, leftThumb, rightThumb);
    leftThumb.draw(canvas);
    rightThumb.draw(canvas);
    if (pressedThumb != null && pressedThumb.isAnimating) {
        invalidate();
    }
}

@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height, width;

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (heightMode == MeasureSpec.EXACTLY) {
        height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
        height = Math.min(heightSize, DEFAULT_HEIGHT);
    } else {
        height = DEFAULT_HEIGHT;
    }

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    if (widthMode == MeasureSpec.EXACTLY) {
        width = widthSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
        width = Math.min(widthSize, DEFAULT_WIDTH);
    } else {
        width = DEFAULT_WIDTH;
    }
    setMeasuredDimension(width, height);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            pressedThumb = checkThumbPressed(eventX, eventY);
            if (pressedThumb == null) {
                return super.onTouchEvent(event);
            }
            pressedThumb.setPressed(true);
            invalidate();

            setPressed(true);
            return true;
        case MotionEvent.ACTION_MOVE:
            onActionMove(eventX);
            return true;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (pressedThumb == null) {
                return super.onTouchEvent(event);
            }
            onActionUp();
            break;
    }
    return super.onTouchEvent(event);
}

private void onActionUp() {
    pressedThumb.onActionUp(seekBar);
    invalidate();
}

private void onActionMove(float eventX) {
    if (eventX >= seekBar.leftX && eventX <= seekBar.rightX) {
        pressedThumb.x = eventX;
        invalidate();

        if (leftThumb.x > rightThumb.x) {
            final Thumb temp = leftThumb;
            leftThumb = rightThumb;
            rightThumb = temp;
        }

        int leftIndex = seekBar.getNearestTick(leftThumb);
        int rightIndex = seekBar.getNearestTick(rightThumb);

        if (mLeftIndex != leftIndex || mRightIndex != rightIndex) {
            mLeftIndex = leftIndex;
            mRightIndex = rightIndex;

            if (mListener != null) {
                mListener.onIndexChange(this, mLeftIndex, mRightIndex);
            }
        }

    }

}

private Thumb checkThumbPressed(float eventX, float eventY) {
    Thumb result = null;
    boolean isLeftThumbPressed = leftThumb.isInTargetZone(eventX, eventY);
    boolean isRightThumbPressed = rightThumb.isInTargetZone(eventX, eventY);

    if (isLeftThumbPressed && isRightThumbPressed) {
        result = (eventX / getWidth() >= 0.5f) ? leftThumb : rightThumb;
    } else if (isLeftThumbPressed) {
        result = leftThumb;
    } else if (isRightThumbPressed) {
        result = rightThumb;
    }
    return result;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    leftThumb.x = (getPaddingLeft() + 20 + leftThumb.normalRadius / 2);
    leftThumb.y = (h + getPaddingTop() + getPaddingBottom()) / 2;
    rightThumb.y = leftThumb.y;
    rightThumb.x = (w - getPaddingRight() - 20 - rightThumb.normalRadius / 2);

    seekBar.leftX = leftThumb.x;
    seekBar.rightX = rightThumb.x;
    seekBar.y = leftThumb.y;

    leftThumb.setIndex(seekBar, mLeftIndex);
    rightThumb.setIndex(seekBar, mRightIndex);
}

@Override
protected Parcelable onSaveInstanceState() {

    Bundle bundle = new Bundle();
    SavedState state = new SavedState(super.onSaveInstanceState());

    state.tickCount = mTickCount;
    state.leftIndex = mLeftIndex;
    state.rightIndex = mRightIndex;
    state.thumbColor = mThumbColor;
    state.thumbNormalRadius = mThumbNormalRadius;
    state.thumbPressedRadius = mThumbPressedRadius;

    bundle.putParcelable(SavedState.STATE,state);
    return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (state instanceof Bundle) {
        Bundle bundle = (Bundle) state;

        SavedState savedState = bundle.getParcelable(SavedState.STATE);

        mTickCount = savedState.tickCount;
        mThumbColor = savedState.thumbColor;
        mThumbNormalRadius = savedState.thumbNormalRadius;
        mThumbPressedRadius = savedState.thumbPressedRadius;
        mLeftIndex = savedState.leftIndex;
        mRightIndex = savedState.rightIndex;

        super.onRestoreInstanceState(savedState.getSuperState());
        return;
    }
    super.onRestoreInstanceState(SavedState.EMPTY_STATE);
}

public interface OnRangeSeekBarChangerListener {
    void onIndexChange(RangeSeekBar rangeBar, int leftIndex, int rightIndex);
}

static class SavedState extends BaseSavedState {
    static final String STATE = "RangeSeekBar.STATE";

    int tickCount;
    int leftIndex;
    int rightIndex;
    int thumbColor;
    int thumbNormalRadius;
    int thumbPressedRadius;

    SavedState(Parcelable superState) {
        super(superState);
    }

    private SavedState(Parcel in) {
        super(in);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
    }

    public static final Creator<SavedState> CREATOR =
            new Creator<SavedState>() {
                public SavedState createFromParcel(Parcel in) {
                    return new SavedState(in);
                }

                public SavedState[] newArray(int size) {
                    return new SavedState[size];
                }
            };
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top