Skip to content

Commit

Permalink
Add support for RatingBar and SeekBar
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Jan 2, 2017
1 parent 82897ab commit e5af822
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RatingBar;
import android.widget.SeekBar;

import com.commit451.untriggered.UntriggeredCheckBox;
import com.commit451.untriggered.UntriggeredRadioButton;
import com.commit451.untriggered.UntriggeredRatingBar;
import com.commit451.untriggered.UntriggeredSeekBar;
import com.commit451.untriggered.UntriggeredSwitchCompat;

public class MainActivity extends AppCompatActivity {

Expand All @@ -18,6 +24,10 @@ protected void onCreate(Bundle savedInstanceState) {

final ViewGroup root = (ViewGroup) findViewById(R.id.root);
final UntriggeredCheckBox checkBox = (UntriggeredCheckBox) findViewById(R.id.checkbox);
final UntriggeredRadioButton radioButton = (UntriggeredRadioButton) findViewById(R.id.radio);
final UntriggeredSwitchCompat switchCompat = (UntriggeredSwitchCompat) findViewById(R.id.switch_thing);
final UntriggeredRatingBar ratingBar = (UntriggeredRatingBar) findViewById(R.id.ratingbar);
final UntriggeredSeekBar seekBar= (UntriggeredSeekBar) findViewById(R.id.seekbar);

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
Expand All @@ -26,10 +36,52 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
.show();
}
});
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Snackbar.make(root, "Radio set to " + isChecked + " and I am triggered!", Snackbar.LENGTH_SHORT)
.show();
}
});
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Snackbar.make(root, "Switch set to " + isChecked + " and I am triggered!", Snackbar.LENGTH_SHORT)
.show();
}
});
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Snackbar.make(root, "RatingBar set to " + rating + " and I am triggered!", Snackbar.LENGTH_SHORT)
.show();
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Snackbar.make(root, "Seekbar set to " + progress + " and I am triggered!", Snackbar.LENGTH_SHORT)
.show();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBox.setCheckedUntriggered(!checkBox.isChecked());
radioButton.setCheckedUntriggered(!radioButton.isChecked());
switchCompat.setCheckedUntriggered(!switchCompat.isChecked());
seekBar.setProgressUntriggered(50);
ratingBar.setRatingUntriggered(2.5f);
}
});
}
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@
android:layout_height="wrap_content"
android:text="Hello World!" />

<com.commit451.untriggered.UntriggeredRadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<com.commit451.untriggered.UntriggeredSwitchCompat
android:id="@+id/switch_thing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<com.commit451.untriggered.UntriggeredSeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.commit451.untriggered.UntriggeredRatingBar
android:id="@+id/ratingbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeLi
}

/**
* Set the checked state without triggered the registered {@link android.widget.CompoundButton.OnCheckedChangeListener}
* Set the checked state without triggering the registered {@link android.widget.CompoundButton.OnCheckedChangeListener}
* @param checked checked
* @see {@link #setChecked(boolean)}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeLi
}

/**
* Set the checked state without triggered the registered {@link android.widget.CompoundButton.OnCheckedChangeListener}
* Set the checked state without triggering the registered {@link android.widget.CompoundButton.OnCheckedChangeListener}
* @param checked checked
* @see {@link #setChecked(boolean)}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.commit451.untriggered;

import android.content.Context;
import android.support.annotation.CallSuper;
import android.support.v7.widget.AppCompatRatingBar;
import android.util.AttributeSet;

/**
* {@link AppCompatRatingBar} that allows you to set the rating without triggering the
* {@link android.widget.RatingBar.OnRatingBarChangeListener}
*/
public class UntriggeredRatingBar extends AppCompatRatingBar {

OnRatingBarChangeListener onSeekBarChangeListener;

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

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

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

@CallSuper
@Override
public void setOnRatingBarChangeListener(OnRatingBarChangeListener listener) {
super.setOnRatingBarChangeListener(listener);
onSeekBarChangeListener = listener;
}

/**
* Set the rating without triggering the registered {@link android.widget.RatingBar.OnRatingBarChangeListener}
* @param rating rating
* @see {@link #setRating(float)}
*/
public void setRatingUntriggered(float rating) {
OnRatingBarChangeListener currentListener = onSeekBarChangeListener;
setOnRatingBarChangeListener(null);
setRating(rating);
setOnRatingBarChangeListener(currentListener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.commit451.untriggered;

import android.annotation.TargetApi;
import android.content.Context;
import android.support.annotation.CallSuper;
import android.support.v7.widget.AppCompatSeekBar;
import android.util.AttributeSet;

/**
* {@link AppCompatSeekBar} that allows you to set the progress without triggering the
* {@link android.widget.SeekBar.OnSeekBarChangeListener}
*/
public class UntriggeredSeekBar extends AppCompatSeekBar {

OnSeekBarChangeListener onSeekBarChangeListener;

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

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

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

@CallSuper
@Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener onSeekBarChangeListener) {
super.setOnSeekBarChangeListener(onSeekBarChangeListener);
this.onSeekBarChangeListener = onSeekBarChangeListener;
}

/**
* Set the progress without triggering the registered {@link android.widget.SeekBar.OnSeekBarChangeListener}
* @param progress progress
* @see {@link #setProgress(int)}
*/
public void setProgressUntriggered(int progress) {
OnSeekBarChangeListener currentListener = onSeekBarChangeListener;
setOnSeekBarChangeListener(null);
setProgress(progress);
setOnSeekBarChangeListener(currentListener);
}

/**
* Set the progress without triggering the registered {@link android.widget.SeekBar.OnSeekBarChangeListener}
* @param progress progress
* @param animate animate
* @see {@link #setProgress(int, boolean)} (int)}
*/
@TargetApi(24)
public void setProgressUntriggered(int progress, boolean animate) {
OnSeekBarChangeListener currentListener = onSeekBarChangeListener;
setOnSeekBarChangeListener(null);
setProgress(progress, animate);
setOnSeekBarChangeListener(currentListener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeLi
}

/**
* Set the checked state without triggered the registered {@link OnCheckedChangeListener}
* Set the checked state without triggering the registered {@link OnCheckedChangeListener}
* @param checked checked
* @see {@link #setChecked(boolean)}
*/
Expand Down

0 comments on commit e5af822

Please sign in to comment.