/slidetoact

A simple 'Slide to Unlock' Material widget for HMOS. 📱🎨🦄

Primary LanguageJavaMIT LicenseMIT

Slide To Act

Quality Gate Status License

A simple Slide to Unlock component for HarmonyOS, written in [Java].

sample-slidetoact gif

Source

Inspired from android library https://github.com/cortinico/slidetoact

Getting Started 👣

Slide To Act is distributed through JCenter. To use it you need to add the following Gradle dependency to your harmony entry gradle file (NOT the root file):

dependencies {
   implementation "com.ncorti:slidetoact:0.9.0"
}

Or you can download the .AAR artifact directly from Bintray.

Example 🚸

After setting up the Gradle dependency, you can use SlideToActView widgets inside your XML Layout files

<com.ncorti.slidetoact.SlideToActView
    ohos:id="$+id:example"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:text="Example" />

And bind them inside your Java/Kotlin code:

SlideToActView slideToActView = (SlideToActView) findComponentById(ResourceTable.Id_example);

Features 🎨

  • 100% Vectorial, no .png or other assets provided.
  • Fancy animations! 🦄
  • Easy to integrate (just a gradle compile line).
  • Works out of the box, no customization needed.
  • UX Friendly 🐣, button will bump to complete if it's over the 80% of the slider (see the following gif).

ux_friendly gif

Attributes

By the default, every SlideToActView widget fits to your app using the some default values and colors. You can customize your SlideToActView using the following custom attributes.

<com.ncorti.slidetoact.SlideToActView
    ohos:id="$+id:welcome_slider"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:areaMargin="4vp"
    ohos:animationDuration="250"
    ohos:outerColor="#FF00FF00"
    ohos:innerColor="#FF222222"
    ohos:borderRadius="2vp"
    ohos:text="Testing all the custom attributes"
    ohos:textSize="12fp"
    ohos:sliderHeight="80vp"
    ohos:sliderLocked="false"
/>

areaMargin

Use the areaMargin attribute to control the margin of the inner circular button from the outside area. If not set, this attribute defaults to 8vp.

area_margin_1

iconMargin

The attribute iconMargin let you control the margin of the icon inside the circular button. This makes the icon bigger because can take up more space in the button.

This is especially useful when you want to make the height of the slider smaller (see sliderHeight). In this case, if you don't adjust the iconMargin the image can be too much tiny. By default, the iconMargin is set to 16vp.

In next image you can see how it looks like:

icon_margin

innerColor & outerColor

Use the outerColor attribute to control the color of the external area and the color of the arrow icon. If not set, this attribute defaults to - #FF4081 #FF4081.

Use the innerColor attribute to control the color of the inner circular button, the color of the tick icon and the color of the text. If not set, this attribute defaults to - #FFFFFF #FFFFFF.

color_1

borderRadius

Use the borderRadius attribute to control the radius of the inner circular button and of the external area. A border_radius set to 0vp will result in a square slider. If not set, this attribute will render your slider as a circle (default behavior).

border_radius_1 border_radius_2

text, textSize

Use the text attribute to control the text of your slider. If not set, this attribute defaults to SlideToActView.

Use the textSize attribute to control the size of the text of your slider. A textSize set to 0fp will result in hiding the text. If not set, this attribute defaults to 16fp.

slider_text

sliderHeight

Use the sliderHeight attribute to control the desired height of the widget. If not set, the widget will try to render with 72vp of height.

slider_height_1 slider_height_2

sliderLocked

Use the sliderLocked attribute to lock the slider (this is a boolean attribute). When a slider is locked, will always bump the button to the beginning (default is false).

locked_slider gif

You can also toggle this attribute programmatically with the provided setter.

SlideToActView sta = (SlideToActView) findComponentById(ResourceTable.Id_slider);
sta.setLocked(true);

animationDuration

Use the animationDuration attribute to set the duration of the complete and reset animation (in milliseconds).

You can also set animation duration programmatically with the provided setter.

val sta = (SlideToActView) findComponentById(ResourceTable.Id_slider);
sta.setAnimationDuration(600);

sliderReversed

Use the sliderReversed attribute to reverse the slider (this is a boolean attribute). When a slider is reversed, the cursor will appear on the right and will progress to the left. (default is false).

reversed_slider gif

You can also toggle this attribute programmatically with the provided setter.

SlideToActView sta = findComponentById(ResourceTable.Id_slider);
sta.setReversed(true);

sliderIcon

You can set a custom icon by setting the sliderIconattribute to a drawable resource.

custom_icon

ohos:sliderIcon="$graphic:custom_icon"

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

SlideToActView sta = findComponentById(ResourceTable.Id_slider);
sta.setSliderIcon(new VectorElement(getContext(), ResourceTable.Graphic_custom_icon));

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

completeIcon

You can set a custom complete icon by setting the completeIconattribute to a drawable resource.

custom_complete_iconcon

app:complete_icon="$graphic:slidetoact_ic_check"

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

SlideToActView sta = findComponentById(ResourceTable.Id_slider);
slider.setCompleteIcon(new VectorElement(getContext(), ResourceTable.Graphic_slidetoact_ic_check));

sliderIconColor

You can set a custom color for the icon by setting the sliderIconColor attribute.

custom_icon

This attribute defaults to the outerColor if set. If outerColor is not set, this attribute defaults to - #FF4081 #FF4081.

bumpVibration

You can make the device vibrate when the cursor "bumps" to the end of the sliding path by setting the period of vibration through bumpVibration attribute in your layout XML (default is 0)

ohos:bumpVibration="200"

Note that the period of vibration is in milliseconds

You can achieve the same programmatically using the setter:

SlideToActView sta = findComponentById(ResourceTable.Id_slider);
sta.setBumpVibration(200);

Event callbacks

You can use the OnSlideCompleteListener and the OnSlideResetListener to simply interact with the widget. If you need to perform operations during animations, you can provide an OnSlideToActAnimationEventListener. With the latter, you will be notified of every animation start/stop.

You can try the Event Callbacks in the Demo app to better understand where every callback is called.

event_log

Future Work

  1. Adding Elevation support.
  2. Adding clipChildren support to obtain this behavior:

custom_icon

  1. Adding Text style and custom text feature.

Testing ⚙️

Once you're able to build successfully, you can run Junit test cases locally.

Make sure your tests are all green ✅ locally before submitting PRs.

Contributing 🤝

Looking for contributors! Don't be shy. 😁 Feel free to open issues/pull requests to help me improve this project.

  • When reporting a new Issue, make sure to attach Screenshots, Videos or GIFs of the problem you are reporting.
  • When submitting a new PR, make sure tests are all green. Write new tests if necessary.

Honorable mentions 🎖

License 📄

This project is licensed under the MIT License - see the License file for details