From 64618e7c0c2c3060d4f335f8796ab36d2746de85 Mon Sep 17 00:00:00 2001 From: kishandonga Date: Thu, 21 Feb 2019 13:07:31 +0530 Subject: [PATCH] drawable function added, example code changes and update readme.md --- README.md | 41 ++- app/src/main/AndroidManifest.xml | 2 + .../example/custom_snackbar/ui/DrawableAct.kt | 107 ++++++++ .../custom_snackbar/ui/MainActivity.kt | 4 + app/src/main/res/drawable/ic_gradient.xml | 14 + app/src/main/res/layout/activity_drawable.xml | 251 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 7 + app/src/main/res/values/strings.xml | 1 + csbx/build.gradle | 4 +- .../com/kishandonga/csbx/CustomSnackbar.kt | 27 +- 10 files changed, 440 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/com/example/custom_snackbar/ui/DrawableAct.kt create mode 100644 app/src/main/res/drawable/ic_gradient.xml create mode 100644 app/src/main/res/layout/activity_drawable.xml diff --git a/README.md b/README.md index 29d42aa..0ccbb11 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Android custom snackbar is derived from the android default snackbar control - Custom view applies in the snackbar control - Typeface applies easily on text - Corner radius and Padding applies -- Androidx support +- Androidx supported - More to find out yourself ## Installation @@ -23,7 +23,7 @@ dependencies { implementation 'com.github.kishandonga:custom-snackbar:1.0' } -//required support lib modules +//required lib modules implementation 'androidx.appcompat:appcompat:1.1.0-alpha02' implementation 'com.google.android.material:material:1.1.0-alpha03' ``` @@ -96,35 +96,56 @@ CustomSnackbar(this).show { ```java ``` +#### With Drawable + +Kotlin +```kotlin +CustomSnackbar(this).show { + drawableRes(R.drawable.ic_gradient) + ... +} +``` + +```java +``` + ## API Documents |Function |Parameter |Description | |:-------------------:|:-------------------:|:-------------------:| |actionTextColor | Integer Color Value | Change the action button text color, default value is colorAccent -|actionTextColorRes | Color Resource | Change the action button text color, default value is colorAccent +|actionTextColorRes | Color Resource | Refer actionTextColor function description |textColor | Integer Color Value | Change the message text color, default value is white color -|textColorRes | Color Resource | Change the message text color, default value is white color +|textColorRes | Color Resource | Refer textColor function description |backgroundColor | Integer Color Value | Change the background color of the snackbar, default value is same as snackbar background also you can't make it transparent -|backgroundColorRes | Color Resource | Change the background color of the snackbar, default value is same as snackbar background also you can't make it transparent +|backgroundColorRes | Color Resource | Refer backgroundColor function description |cornerRadius | Float Value | Apply corner radius all the side (Left, Top, Right, Bottom), default value is 0 -|cornerRadiusRes | Dimension Resource | Apply corner radius all the side (Left, Top, Right, Bottom), default value is 0 +|cornerRadiusRes | Dimension Resource | Refer cornerRadius function description |border | Width as Integer and Integer Color Value | Apply border width and color around the snackbar, default value of the width is 0 and color is transparent -|borderRes | Width as Dimension Resource and Color Resource | Apply border width and color around the snackbar, default value of the width is 0 and color is transparent -|customView | View or Layout Resource | set the your customized view as snackbar, default value is null view +|borderRes | Width as Dimension Resource and Color Resource | Refer border function description +|customView | View or Layout Resource | set the your customized view as snackbar, default value is null also when you apply custom view then other api are not in use as shown in the example code |message | String | set the message as string and default value is empty string -|messageRes | String Resource | set the message as string and default value is empty string +|messageRes | String Resource | Refer message function description |duration | Integer Value | set the time duration default value is Snackbar.LENGTH_SHORT |padding | Integer Value | apply the padding at the (Left, Right, Bottom) side, default value is 0 -|paddingRes | Dimension Resource | apply the padding at the (Left, Right, Bottom) side, default value is 0 +|paddingRes | Dimension Resource | Refer padding function description |textTypeface | Typeface | Change the message text Typeface, default value is Typeface.NORMAL |actionTypeface | Typeface | Change the action button text Typeface, default value is Typeface.NORMAL |withAction | String Resource or String and Snackbar as anonymous function | pass the first argument as action button name and default value is empty string, second argument as lamda function with snackbar reference |withCustomView | View as anonymous function | when custom view initialze then passing here for your further use +|drawable | GradientDrawable | set the gradient drawable as snackbar background, default value is null also when you apply drawable then background color, cornerRadius, border width and color api are not in use as shown in the example code +|drawableRes | Drawable Resource| Refer drawable function description |show | Void Or Koltin DSL | Show the snackbar view |dismiss | Void Or Unit | Dismiss the snackbar view |getView | Void and return View? | Same as withCustomView but it will return null value too +### About me + +Kishan Donga ([@ikishan92](https://twitter.com/ikishan92)) +I am mobility developer. + + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 971ee80..c88f9ef 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,6 +11,8 @@ android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning"> + + diff --git a/app/src/main/java/com/example/custom_snackbar/ui/DrawableAct.kt b/app/src/main/java/com/example/custom_snackbar/ui/DrawableAct.kt new file mode 100644 index 0000000..a4febb1 --- /dev/null +++ b/app/src/main/java/com/example/custom_snackbar/ui/DrawableAct.kt @@ -0,0 +1,107 @@ +package com.example.custom_snackbar.ui + +import android.content.DialogInterface +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.widget.SeekBar +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import com.example.custom_snackbar.R +import com.example.custom_snackbar.utils.themeConst +import com.google.android.material.snackbar.Snackbar +import com.kishandonga.csbx.CustomSnackbar +import com.skydoves.colorpickerview.ColorPickerDialog +import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener +import kotlinx.android.synthetic.main.activity_drawable.* + +class DrawableAct : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val themeConst = intent.getIntExtra(themeConst, 0) + setTheme(if (themeConst == 0) R.style.AppCompat_ActionBar else R.style.Material_ActionBar) + + setContentView(R.layout.activity_drawable) + title = "Custom Drawable" + + var textColor = Color.WHITE + var actionTxtColor = ContextCompat.getColor(this, R.color.colorAccent) + + btnActionTextColor.setOnClickListener { + ColorPickerDialog.Builder(this) + .setTitle("ColorPicker Dialog") + .setPositiveButton(getString(android.R.string.ok), ColorEnvelopeListener { envelope, _ -> + llActionTextColor.setBackgroundColor(envelope.color) + actionTxtColor = envelope.color + }) + .setNegativeButton(getString(android.R.string.cancel)) { di: DialogInterface, _: Int -> + di.cancel() + } + .show() + } + + btnTextColor.setOnClickListener { + ColorPickerDialog.Builder(this) + .setTitle("ColorPicker Dialog") + .setPositiveButton(getString(android.R.string.ok), ColorEnvelopeListener { envelope, _ -> + llTextColor.setBackgroundColor(envelope.color) + textColor = envelope.color + }) + .setNegativeButton(getString(android.R.string.cancel)) { di: DialogInterface, _: Int -> + di.cancel() + } + .show() + } + + sbPadding.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) { + tvPaddingIndicator.text = p0?.progress.toString() + } + + override fun onStartTrackingTouch(p0: SeekBar?) { + } + + override fun onStopTrackingTouch(p0: SeekBar?) { + } + }) + + var snackbar: CustomSnackbar? = null + + btnShow.setOnClickListener { + + val timeDuration: Int = when { + rbLengthIndefinite.isChecked -> Snackbar.LENGTH_INDEFINITE + rbLengthLong.isChecked -> Snackbar.LENGTH_LONG + else -> Snackbar.LENGTH_SHORT + } + + val typeface: Typeface = when { + rbBold.isChecked -> Typeface.defaultFromStyle(Typeface.BOLD) + rbBoldItalic.isChecked -> Typeface.defaultFromStyle(Typeface.BOLD_ITALIC) + rbItalic.isChecked -> Typeface.defaultFromStyle(Typeface.ITALIC) + else -> Typeface.defaultFromStyle(Typeface.NORMAL) + } + + snackbar = CustomSnackbar(this).show { + drawableRes(R.drawable.ic_gradient) + textColor(textColor) + actionTextColor(actionTxtColor) + textTypeface(typeface) + actionTypeface(typeface) + padding(sbPadding.progress) + duration(timeDuration) + message("Testing Message...") + if (rbWithAction.isChecked) { + withAction(android.R.string.ok) { + it.dismiss() + } + } + } + } + + btnHide.setOnClickListener { + snackbar?.dismiss() + } + } +} diff --git a/app/src/main/java/com/example/custom_snackbar/ui/MainActivity.kt b/app/src/main/java/com/example/custom_snackbar/ui/MainActivity.kt index 8139200..b706f6a 100644 --- a/app/src/main/java/com/example/custom_snackbar/ui/MainActivity.kt +++ b/app/src/main/java/com/example/custom_snackbar/ui/MainActivity.kt @@ -24,6 +24,10 @@ class MainActivity : AppCompatActivity() { btnCustomView.setOnClickListener { startNewActivity(CustomViewAct::class.java) } + + btnDrawableAct.setOnClickListener { + startNewActivity(DrawableAct::class.java) + } } private fun startNewActivity(cls: Class<*>) { diff --git a/app/src/main/res/drawable/ic_gradient.xml b/app/src/main/res/drawable/ic_gradient.xml new file mode 100644 index 0000000..f33253f --- /dev/null +++ b/app/src/main/res/drawable/ic_gradient.xml @@ -0,0 +1,14 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_drawable.xml b/app/src/main/res/layout/activity_drawable.xml new file mode 100644 index 0000000..52bd558 --- /dev/null +++ b/app/src/main/res/layout/activity_drawable.xml @@ -0,0 +1,251 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6b06928..a873324 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -49,4 +49,11 @@ android:layout_height="wrap_content" android:text="@string/lbl_with_custom_view"/> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d9c6cba..99d4b49 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,4 +30,5 @@ Hide Custom View With Custom View Action Button Text Color + With Drawable diff --git a/csbx/build.gradle b/csbx/build.gradle index 25fdf80..c8a7062 100644 --- a/csbx/build.gradle +++ b/csbx/build.gradle @@ -3,14 +3,14 @@ apply plugin: 'kotlin-android' apply plugin: 'com.github.dcendents.android-maven' group = 'com.github.kishandonga' -version = '1.0' +version = '1.1' android { compileSdkVersion 28 defaultConfig { minSdkVersion 19 targetSdkVersion 28 - versionCode 1 + versionCode 2 versionName version testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/csbx/src/main/java/com/kishandonga/csbx/CustomSnackbar.kt b/csbx/src/main/java/com/kishandonga/csbx/CustomSnackbar.kt index eb9923a..c93bf63 100644 --- a/csbx/src/main/java/com/kishandonga/csbx/CustomSnackbar.kt +++ b/csbx/src/main/java/com/kishandonga/csbx/CustomSnackbar.kt @@ -38,12 +38,24 @@ class CustomSnackbar(private val context: Context) { private var coordinateView: View? = null private var tfTextView: Typeface? = null private var tfActionBtn: Typeface? = null + private var customDrawable: GradientDrawable? = null private lateinit var snackbar: Snackbar constructor(context: Context, view: View) : this(context) { coordinateView = view } + fun drawableRes(@DrawableRes drawableId: Int) { + val drw = ContextCompat.getDrawable(context, drawableId) + if (drw is GradientDrawable) { + drawable(drw) + } + } + + fun drawable(drawable: GradientDrawable?) { + this.customDrawable = drawable + } + fun textTypeface(textTypeface: Typeface) { tfTextView = textTypeface } @@ -151,15 +163,18 @@ class CustomSnackbar(private val context: Context) { val snackbarLayout = snackbar.view as Snackbar.SnackbarLayout // Frame Layout val snackContentLayout = snackbarLayout.getChildAt(0) as SnackbarContentLayout // Linear Layout - if (backgroundColor != 0) { - drawable.setColor(backgroundColor) + if (customDrawable == null) { + if (backgroundColor != 0) { + drawable.setColor(backgroundColor) + } else { + drawable = snackbarLayout.background as GradientDrawable + } + drawable.cornerRadius = cornerRadius + drawable.setStroke(borderWidth, borderColor) } else { - drawable = snackbarLayout.background as GradientDrawable + drawable = customDrawable as GradientDrawable } - drawable.cornerRadius = cornerRadius - drawable.setStroke(borderWidth, borderColor) - val pLeft = snackbarLayout.paddingLeft val pRight = snackbarLayout.paddingRight