Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for showing/ hiding pins #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected void onCreate(Bundle savedInstanceState) {
final TextView tickTopLabelsButton = (TextView) findViewById(R.id.toggleTickTopLabels);
final TextView tickLabelColor = (TextView) findViewById(R.id.tickLabelColor);
final TextView tickLabelSelectedColor = (TextView) findViewById(R.id.tickLabelSelectColor);
final TextView togglePinsEnabled = (TextView) findViewById(R.id.togglePinsEnabled);

//Sets the buttons to bold.
// barColor.setTypeface(font, Typeface.BOLD);
Expand Down Expand Up @@ -416,9 +417,15 @@ public void onClick(View v) {
}
});

togglePinsEnabled.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rangebar.setPinsEnabled(!rangebar.arePinsEnabled());
}
});
}
private int getValueInDP(int value)
{

private int getValueInDP(int value) {
int valueInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
value,
getResources().getDisplayMetrics());
Expand Down
23 changes: 23 additions & 0 deletions RangeBarSample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,29 @@
</android.support.v7.widget.CardView>
</LinearLayout>

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
card_view:cardBackgroundColor="@color/white"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp">

<TextView
android:id="@+id/togglePinsEnabled"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:gravity="center_vertical|center_horizontal"
android:text="@string/pins_enabled"
android:textAllCaps="true"
android:textColor="@color/accent"
android:textSize="16sp" />
</android.support.v7.widget.CardView>

<TextView
android:id="@+id/tickStart"
style="@style/RoboTheme"
Expand Down
1 change: 1 addition & 0 deletions RangeBarSample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<string name="check_box_rounded_background">Rounded background</string>
<string name="top_tick_labels">Top tick labels</string>
<string name="bottom_tick_labels">Bottom tick labels</string>
<string name="pins_enabled">Pins enabled</string>
<string-array translatable="false" name="default_color_choice_values">
<item>#33b5e5</item>
<item>#aa66cc</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class PinView extends View {

private int pinColor;

private boolean mPinEnabled = false;

// Constructors ////////////////////////////////////////////////////////////

public PinView(Context context) {
Expand Down Expand Up @@ -125,7 +127,8 @@ public void setFormatter(com.appyvet.materialrangebar.IRangeBarFormatter mFormat
* @param pinsAreTemporary whether to show the pin initially or just the circle
*/
public void init(Context ctx, float y, float pinRadiusDP, int pinColor, int textColor,
float circleRadius, int circleColor, int circleBoundaryColor, float circleBoundarySize, float minFont, float maxFont, boolean pinsAreTemporary) {
float circleRadius, int circleColor, int circleBoundaryColor, float circleBoundarySize,
float minFont, float maxFont, boolean pinsAreTemporary, boolean pinEnabled) {

mRes = ctx.getResources();
mPin = ContextCompat.getDrawable(ctx, R.drawable.rotate);
Expand Down Expand Up @@ -172,6 +175,7 @@ public void init(Context ctx, float y, float pinRadiusDP, int pinColor, int text
}

this.pinColor = pinColor;
mPinEnabled = pinEnabled;

// Sets the minimum touchable area, but allows it to expand based on
// image size
Expand Down Expand Up @@ -274,25 +278,27 @@ public void draw(Canvas canvas) {

canvas.drawCircle(mX, mY, mCircleRadiusPx, mCirclePaint);
//Draw pin if pressed
if (mPinRadiusPx > 0 && (mHasBeenPressed || !mPinsAreTemporary)) {
mBounds.set((int) mX - mPinRadiusPx,
(int) mY - (mPinRadiusPx * 2) - (int) mPinPadding,
(int) mX + mPinRadiusPx, (int) mY - (int) mPinPadding);
mPin.setBounds(mBounds);
String text = mValue;

if (this.formatter != null) {
text = formatter.format(text);
if (mPinEnabled) {
if (mPinRadiusPx > 0 && (mHasBeenPressed || !mPinsAreTemporary)) {
mBounds.set((int) mX - mPinRadiusPx,
(int) mY - (mPinRadiusPx * 2) - (int) mPinPadding,
(int) mX + mPinRadiusPx, (int) mY - (int) mPinPadding);
mPin.setBounds(mBounds);
String text = mValue;

if (this.formatter != null) {
text = formatter.format(text);
}

calibrateTextSize(mTextPaint, text, mBounds.width());
mTextPaint.getTextBounds(text, 0, text.length(), mBounds);
mTextPaint.setTextAlign(Paint.Align.CENTER);
DrawableCompat.setTint(mPin, pinColor);
mPin.draw(canvas);
canvas.drawText(text,
mX, mY - mPinRadiusPx - mPinPadding + mTextYPadding,
mTextPaint);
}

calibrateTextSize(mTextPaint, text, mBounds.width());
mTextPaint.getTextBounds(text, 0, text.length(), mBounds);
mTextPaint.setTextAlign(Paint.Align.CENTER);
DrawableCompat.setTint(mPin, pinColor);
mPin.draw(canvas);
canvas.drawText(text,
mX, mY - mPinRadiusPx - mPinPadding + mTextYPadding,
mTextPaint);
}
super.draw(canvas);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ public class RangeBar extends View {

private boolean mOnlyOnDrag = false;

private boolean mPinsEnabled = true;

private PinTextFormatter mPinTextFormatter = new PinTextFormatter() {
@Override
public String getText(String value) {
Expand Down Expand Up @@ -405,12 +407,12 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mLeftThumb = new PinView(ctx);
mLeftThumb.setFormatter(mFormatter);
mLeftThumb.init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize,
mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary);
mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled);
}
mRightThumb = new PinView(ctx);
mRightThumb.setFormatter(mFormatter);
mRightThumb.init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize,
mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary);
mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled);

// Create the underlying bar.
final float marginLeft = Math.max(mExpandedPinRadius, mCircleSize);
Expand Down Expand Up @@ -1157,6 +1159,20 @@ public void setPinTextFormatter(PinTextFormatter pinTextFormatter) {
this.mPinTextFormatter = pinTextFormatter;
}

/**
* Sets the visibility of pins by the supplied value
*
* @param enable boolean specifying whether pins are visible
*/
public void setPinsEnabled(boolean enable) {
mPinsEnabled = enable;
createPins();
}

public boolean arePinsEnabled() {
return mPinsEnabled;
}

// Private Methods /////////////////////////////////////////////////////////

/**
Expand Down Expand Up @@ -1298,6 +1314,7 @@ private void rangeBarInit(Context context, AttributeSet attrs) {
mIsRangeBar = ta.getBoolean(R.styleable.RangeBar_mrb_rangeBar, true);

mOnlyOnDrag = ta.getBoolean(R.styleable.RangeBar_mrb_onlyOnDrag, false);
mPinsEnabled = ta.getBoolean(R.styleable.RangeBar_mrb_pinsEnabled, true);
} finally {
ta.recycle();
}
Expand Down Expand Up @@ -1352,12 +1369,12 @@ private void createPins() {
if (mIsRangeBar) {
mLeftThumb = new PinView(ctx);
mLeftThumb.init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize, mCircleColor, mCircleBoundaryColor, mCircleBoundarySize,
mMinPinFont, mMaxPinFont, mArePinsTemporary);
mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled);
}
mRightThumb = new PinView(ctx);
mRightThumb
.init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize, mCircleColor, mCircleBoundaryColor, mCircleBoundarySize
, mMinPinFont, mMaxPinFont, mArePinsTemporary);
, mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled);

float marginLeft = getMarginLeft();
float barLength = getBarLength();
Expand Down
1 change: 1 addition & 0 deletions materialrangebar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<attr name="mrb_pinRadius" format="dimension"/>
<attr name="mrb_connectingLineColors" format="reference"/>
<attr name="mrb_onlyOnDrag" format="boolean"/>
<attr name="mrb_pinsEnabled" format="boolean"/>
Copy link
Collaborator

@krazykira krazykira Mar 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a property mrb_temporaryPins and i think this one should be changed so it should have the following three values

  1. ALWAYS (This displays them all the time)
  2. NEVER (This hides them all the time)
  3. ON_TOUCH_ONLY (This displays them when we are modifying the value and then hides them )

1 & 3 can be achieved right now by changing value for mrb_temporaryPins to true and false but the 2nd one is what you have to implement and at the same time you need to change the mrb_temporaryPins

So at the end the mrb_temporaryPins would be changed to something like this


 <attr name="mrb_temporaryPins" format="enum">
            <enum name="ALWAYS" value="0" />
            <enum name="NEVER" value="1" />
            <enum name="ON_TOUCH_ONLY" value="2" />
        </attr>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, I will add it

</declare-styleable>

</resources>