Skip to content

Commit

Permalink
Set icon during runtime (cortinico#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinolaomahei authored Jun 4, 2020
1 parent 032f536 commit ce34b27
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ You can set a custom icon by setting the ``slider_icon``attribute to a drawable
app:slider_icon="@drawable/custom_icon"
```

You can also set a custom icon programmatically with the provided setter.

```java
SlideToActView sta = findViewById(R.id.slider);
sta.setSliderIcon(R.drawable.custom_icon);
```

You can also disable the rotation by setting the ``rotate_icon`` attribute to false.

#### ``slider_icon_color``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ protected void onCreate(Bundle savedInstanceState) {
break;
case R.id.button_custom_icon:
setContentView(R.layout.content_custom_icon);
final SlideToActView slider = findViewById(R.id.slide_custom_icon);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()) {
case R.id.button_android_icon:
slider.setSliderIcon(R.drawable.ic_android);
break;
case R.id.button_cloud_icon:
slider.setSliderIcon(R.drawable.ic_cloud);
break;
default:
break;
}
}
};

slider.setOnSlideResetListener(new SlideToActView.OnSlideResetListener() {
@Override
public void onSlideReset(SlideToActView view) {
slider.setSliderIcon(R.drawable.custom_icon);
}
});

findViewById(R.id.button_android_icon).setOnClickListener(listener);
findViewById(R.id.button_cloud_icon).setOnClickListener(listener);
break;
case R.id.button_reversed_slider:
setContentView(R.layout.content_reversed_slider);
Expand Down
9 changes: 9 additions & 0 deletions example/src/main/res/drawable/ic_android.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>
5 changes: 5 additions & 0 deletions example/src/main/res/drawable/ic_cloud.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96z"/>
</vector>
13 changes: 12 additions & 1 deletion example/src/main/res/layout/content_custom_icon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/slide_container"
android:layout_width="match_parent"
Expand All @@ -17,5 +16,17 @@
app:text="Custom Icon"
app:slider_icon="@drawable/custom_icon"/>

<Button
android:id="@+id/button_android_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/use_android_icon" />

<Button
android:id="@+id/button_cloud_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/use_cloud_icon" />

</LinearLayout>
</ScrollView>
2 changes: 2 additions & 0 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
<string name="bump_vibration">Bump Vibration</string>
<string name="event_log">Event Log</string>
<string name="welcome_body">Press the following buttons to checkout different SlideToActView and understand each parameter. You can reset the sliders pressing the Reset button in the Toolbar. Enjoy :)</string>
<string name="use_android_icon">Use Android icon</string>
<string name="use_cloud_icon">Use Cloud icon</string>
</resources>
20 changes: 13 additions & 7 deletions slidetoact/src/main/java/com/ncorti/slidetoact/SlideToActView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ class SlideToActView @JvmOverloads constructor(
invalidate()
}

/** Custom Slider Icon */
var sliderIcon: Int = R.drawable.slidetoact_ic_arrow
set(value) {
field = value
if (field != 0) {
mDrawableArrow = parseVectorDrawableCompat(context.resources, value, context.theme)
mDrawableArrow.setTint(iconColor)
invalidate()
}
}

/** Slider cursor position (between 0 and (`mAreaWidth - mAreaHeight)) */
private var mPosition: Int = 0
set(value) {
Expand Down Expand Up @@ -190,15 +201,12 @@ class SlideToActView @JvmOverloads constructor(
private var mTickMargin: Int

/** Arrow drawable */
private val mDrawableArrow: VectorDrawableCompat
private lateinit var mDrawableArrow: VectorDrawableCompat

/** Tick drawable, is actually an AnimatedVectorDrawable */
private val mDrawableTick: Drawable
private var mFlagDrawTick: Boolean = false

/** The icon for the drawable */
private var mIcon: Int = R.drawable.slidetoact_ic_arrow

/* -------------------- PAINT & DRAW -------------------- */
/** Paint used for outer elements */
private val mOuterPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
Expand Down Expand Up @@ -333,7 +341,7 @@ class SlideToActView @JvmOverloads constructor(
)
mActualAreaMargin = mOriginAreaMargin

mIcon = getResourceId(
sliderIcon = getResourceId(
R.styleable.SlideToActView_slider_icon, R.drawable.slidetoact_ic_arrow
)

Expand Down Expand Up @@ -373,8 +381,6 @@ class SlideToActView @JvmOverloads constructor(
mAreaHeight.toFloat()
)

mDrawableArrow = parseVectorDrawableCompat(context.resources, mIcon, context.theme)

// Due to bug in the AVD implementation in the support library, we use it only for API < 21
mDrawableTick = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
context.resources.getDrawable(
Expand Down

0 comments on commit ce34b27

Please sign in to comment.