Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A simple and customizable two or three states Switch View

NotificationsYou must be signed in to change notification settings

RiccardoMoro/RMSwitch

Repository files navigation

A simple View that works like a switch, but with more customizations.
With the option to choose between two or three states. (from v1.1.0)

** If you're upgrading from a version < 1.2.0, check the changelog of the 1.2.0 version, there are breaking changes!

[Changelog] (CHANGELOG.md)

From version 1.2.0 you can choose between three switch design:

-"Slim"
-"Large"
-"Android"

Download

Gradle:

compile'com.rm:rmswitch:1.2.2'

Min API level: 16 (Android 4.1)

Usage

To use them, just add this to your layout file

<!-- Two states switch-->    <com.rm.rmswitch.RMSwitchandroid:id="@+id/your_id"android:layout_width="wrap_content"android:layout_height="wrap_content"app:checked="true"app:forceAspectRatio="false"app:enabled="true"app:switchDesign="android"app:switchBkgCheckedColor="@color/green"app:switchBkgNotCheckedColor="@color/red"app:switchToggleCheckedColor="@color/green"app:switchToggleCheckedImage="@drawable/happy"app:switchToggleNotCheckedColor="@color/red"app:switchToggleNotCheckedImage="@drawable/sad"/><!-- Three states switch-->                    <com.rm.rmswitch.RMTristateSwitchandroid:id="@+id/your_id2"android:layout_width="wrap_content"android:layout_height="wrap_content"app:forceAspectRatio="false"app:state="left"app:enabled="true"app:switchDesign="large"app:switchBkgLeftColor="@color/red"app:switchBkgMiddleColor="@color/grey"app:switchBkgRightColor="@color/green"app:switchToggleLeftColor="@color/red"app:switchToggleLeftImage="@drawable/sad"app:switchToggleMiddleColor="@color/grey"app:switchToggleMiddleImage="@drawable/neutral"app:switchToggleRightColor="@color/green"app:switchToggleRightImage="@drawable/happy"/>

... if you need to use this View's custom xml attributes (shown in a table below or in the example above) do not forget to add this to your root layout

xmlns:app="http://schemas.android.com/apk/res-auto"

To see how it looks in the preview screen of Android Studio, build your project first

And this in your Activity

publicclassMainActivityextendsAppCompatActivity {RMSwitchmSwitch;RMTristateSwitchmTristateSwitch;@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSwitch = (RMSwitch)findViewById(R.id.your_id);mTristateSwitch = (RMTristateSwitch)findViewById(R.id.your_id2);// Setup the switchmSwitch.setChecked(true);mSwitch.setEnabled(true);mSwitch.setForceAspectRatio(false);mSwitch.setSwitchBkgCheckedColor(Color.GREEN);mSwitch.setSwitchBkgNotCheckedColor(Color.RED);mSwitch.setSwitchToggleCheckedColor(Color.BLACK);mSwitch.setSwitchToggleNotCheckedColor(Color.BLACK);mSwitch.setSwitchDesign(RMTristateSwitch.DESIGN_ANDROID);// You can choose if use drawable or drawable resource//mSwitch.setSwitchToggleCheckedDrawableRes(android.R.drawable.ic_media_next);mSwitch.setSwitchToggleCheckedDrawable(ContextCompat.getDrawable(this,android.R.drawable.ic_media_next));//mSwitch.setSwitchToggleNotCheckedDrawableRes(android.R.drawable.ic_media_previous);mSwitch.setSwitchToggleNotCheckedDrawable(ContextCompat.getDrawable(this,android.R.drawable.ic_media_previous));// Setup the tristate switchmTristateSwitch.setState(RMTristateSwitch.STATE_LEFT);mTristateSwitch.setForceAspectRatio(true);mTristateSwitch.setEnabled(true);mTristateSwitch.setRightToLeft(false);mTristateSwitch.setSwitchDesign(RMTristateSwitch.DESIGN_LARGE);mTristateSwitch.setSwitchToggleLeftColor(Color.DKGRAY);mTristateSwitch.setSwitchToggleMiddleColor(Color.DKGRAY);mTristateSwitch.setSwitchToggleRightColor(Color.DKGRAY);mTristateSwitch.setSwitchBkgLeftColor(Color.LTGRAY);mTristateSwitch.setSwitchBkgMiddleColor(Color.LTGRAY);mTristateSwitch.setSwitchBkgRightColor(Color.LTGRAY);// You can choose if use drawable or drawable resource//mTristateSwitch.setSwitchToggleLeftDrawableRes(android.R.drawable.ic_media_previous);mTristateSwitch.setSwitchToggleLeftDrawable(ContextCompat.getDrawable(this,android.R.drawable.ic_media_previous));//mTristateSwitch.setSwitchToggleMiddleDrawableRes(android.R.drawable.ic_media_play);mTristateSwitch.setSwitchToggleMiddleDrawable(ContextCompat.getDrawable(this,android.R.drawable.ic_media_play));//mTristateSwitch.setSwitchToggleRightDrawableRes(android.R.drawable.ic_media_next);mTristateSwitch.setSwitchToggleRightDrawable(ContextCompat.getDrawable(this,android.R.drawable.ic_media_next));// Add a Switch state observermSwitch.addSwitchObserver(newRMSwitch.RMSwitchObserver() {@OverridepublicvoidonCheckStateChange(RMSwitchswitchView,booleanisChecked) {Toast.makeText(MainActivity.this,"Switch state: " +                                         (isChecked ?"checked" :"not checked"),Toast.LENGTH_LONG)                                        .show();            }        });mTristateSwitch.addSwitchObserver(newRMTristateSwitch.RMTristateSwitchObserver() {@OverridepublicvoidonCheckStateChange(RMTristateSwitchswitchView,@RMTristateSwitch.Stateintstate) {Toast                        .makeText(MainActivity.this,state ==RMTristateSwitch.STATE_LEFT ?"Left" :state ==RMTristateSwitch.STATE_MIDDLE ?"Middle" :"Right",Toast.LENGTH_SHORT).show();            }        });    }}

Supported Attributes

RMSwitch

XML AttributeJava methodDescriptionDefault value
checkedsetChecked(boolean checked)The initial state of the Switch, if checked or notfalse
enabledsetEnabled(boolean enabled)If not enabled, the Switch will not be clickable, but it is still possible to change its state programmaticallytrue
forceAspectRatiosetForceAspectRatio(boolean forceAspectRatio)Force the Switch aspect ratiotrue
switchBkgCheckedColorsetSwitchBkgCheckedColor(@ColorInt int color)The background color of the Switch if checkedyour current theme colorControlHighlight attribute
switchBkgNotCheckedColorsetSwitchBkgNotCheckedColor(@ColorInt int color)The background color of the Switch if not checkedthe same as switchBkgCheckedColor
switchToggleCheckedColorsetSwitchToggleCheckedColor(@ColorInt int color)The color of the Switch toggle if checkedyour current theme colorAccent attribute
switchToggleNotCheckedColorsetSwitchToggleNotCheckedColor(@ColorInt int color)The color of the Switch toggle if not checkedwhite
switchToggleCheckedImagesetSwitchToggleCheckedDrawableRes(@DrawableRes int drawable)The image to be shown on the toggle if checkedthe same as switchToggleNotCheckedImage if set, none otherwise
setSwitchToggleCheckedDrawable(Drawable drawable)
switchToggleNotCheckedImagesetSwitchToggleNotCheckedDrawableRes(@DrawableRes int drawable)The image to be shown on the toggle if not checkedthe same as switchToggleCheckedImage if set, none otherwise
setSwitchToggleNotCheckedDrawable(Drawable drawable)
switchDesignsetSwitchDesign(@SwitchDesign int switchDesign)The switch design, one of the RMAbstractSwitch.DESIGN_*** constants, changes the appearance of the switchRMAbstractSwitch.DESIGN_LARGE

RMTristateSwitch

XML AttributeJava methodDescriptionDefault value
statesetState(@State int state)The initial state of the Switch, if left, middle or rightleft
enabledsetEnabled(boolean enabled)If not enabled, the Switch will not be clickable, but it is still possible to change its state programmaticallytrue
forceAspectRatiosetForceAspectRatio(boolean forceAspectRatio)Force the Switch aspect ratiotrue
right_to_leftsetRightToLeft(boolean rightToLeft)The direction of the switch at every tap, if from left to right or right to leftfalse
switchBkgLeftColorsetSwitchBkgLeftColor(@ColorInt int color)The background color of the Switch if in the left stateyour current theme colorControlHighlight attribute
switchBkgMiddleColorsetSwitchBkgMiddleColor(@ColorInt int color)The background color of the Switch if in the middle statethe same as switchBkgLeftColor
switchBkgRightColorsetSwitchBkgRightColor(@ColorInt int color)The background color of the Switch if in the right statethe same as switchBkgLeftColor
switchToggleLeftColorsetSwitchToggleLeftColor(@ColorInt int color)The background color of the Switch if in the left statewhite
switchToggleMiddleColorsetSwitchToggleMiddleColor(@ColorInt int color)The background color of the Switch if in the middle stateyour current theme primaryColor attribute
switchToggleRightColorsetSwitchToggleRightColor(@ColorInt int color)The background color of the Switch Toggle if in the right stateyour current theme accentColor attribute
switchToggleLeftImagesetSwitchToggleLeftDrawableRes(@ColorInt int color)The toggle image of the Switch if in the left statethe same as the one of the other states toggle image if at least one set, none otherwise
setSwitchToggleLeftDrawable(Drawable drawable)
switchToggleMiddleImagesetSwitchToggleMiddleDrawableRes(@ColorInt int color)The toggle image of the Switch if in the middle statethe same as the one of the other states toggle image if at least one set, none otherwise
setSwitchToggleMiddleDrawable(Drawable drawable)
switchToggleRightImagesetSwitchToggleRightDrawableRes(@ColorInt int color)The toggle image of the Switch if in the right statethe same as the one of the other states toggle image if at least one set, none otherwise
setSwitchToggleRightDrawable(Drawable drawable)
switchDesignsetSwitchDesign(@SwitchDesign int switchDesign)The switch design, one of the RMAbstractSwitch.DESIGN_*** constants, changes the appearance of the switchRMAbstractSwitch.DESIGN_LARGE

The changes between the Switch states will be automatically cross-faded, to obtain a smooth experience

License

Copyright 2017 Riccardo Moro.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at   http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions and limitations under the License.

About

A simple and customizable two or three states Switch View

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp