Skip to content
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ apply plugin: 'kotlin-kapt'
apply from: "../ktlint.gradle"

android {
compileSdkVersion 31
compileSdkVersion 33

defaultConfig {
applicationId "com.github.dhaval2404.colorpicker.sample"
minSdkVersion 21
targetSdkVersion 31
versionCode 8
versionName "2.3"
versionCode 9
versionName "2.31"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down Expand Up @@ -41,10 +41,10 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.browser:browser:1.4.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0'

Expand Down
11 changes: 3 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.5.0'
ext.kotlin_version = '1.7.21'
repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

//jcenter plugins
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}

allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
// Avoid Kotlin docs error
tasks.withType(Javadoc) {
Expand Down
18 changes: 9 additions & 9 deletions colorpicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 31
versionCode 8
versionName "2.3"
versionCode 9
versionName "2.31"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down Expand Up @@ -40,9 +40,9 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'com.google.android.material:material:1.6.0'
implementation 'com.google.android.flexbox:flexbox:3.0.0'

testImplementation 'junit:junit:4.13.2'
Expand All @@ -63,7 +63,7 @@ ext {
siteUrl = 'https://github.com/Dhaval2404/ColorPicker/'
gitUrl = 'https://github.com/Dhaval2404/ColorPicker.git'

libraryVersion = '2.3'
libraryVersion = '2.31'
//If you are uploading new library try : gradlew install
//If you are updating existing library then execute: gradlew bintrayUpload
//In both the case don't forgot to put bintray credentials in local.properties file.
Expand All @@ -77,6 +77,6 @@ ext {
allLicenses = ["Apache-2.0"]
}

// Place it at the end of the file
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
//// Place it at the end of the file
//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.github.dhaval2404.colorpicker.adapter.RecentColorAdapter
import com.github.dhaval2404.colorpicker.adapter.ColorPickerAdapter
import com.github.dhaval2404.colorpicker.listener.ColorListener
import com.github.dhaval2404.colorpicker.listener.DismissListener
import com.github.dhaval2404.colorpicker.model.ColorShape
import com.github.dhaval2404.colorpicker.util.ColorUtil
import com.github.dhaval2404.colorpicker.util.SharedPref
import com.github.dhaval2404.colorpicker.util.setButtonTextColor
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.material.card.MaterialCardView
import androidx.cardview.widget.CardView

/**
* Color Picker Dialog, Pick Color from Color Dial
Expand All @@ -34,7 +34,9 @@ class ColorPickerDialog private constructor(
val colorListener: ColorListener?,
val dismissListener: DismissListener?,
val defaultColor: String?,
var colorShape: ColorShape
var colorShape: ColorShape,
val layout: Int,
val colors: List<String>,
) {

class Builder(val context: Context) {
Expand All @@ -45,6 +47,8 @@ class ColorPickerDialog private constructor(
private var dismissListener: DismissListener? = null
private var defaultColor: String? = null
private var colorShape: ColorShape = ColorShape.CIRCLE
private var colors: List<String> = emptyList()
private var layout: Int = R.layout.dialog_color_picker

/**
* Set Dialog Title
Expand Down Expand Up @@ -136,6 +140,43 @@ class ColorPickerDialog private constructor(
return this
}

/**
* Set Custom Layout,
*
* @param layout Layout resource id
*/
fun setLayout(layout: Int): Builder {
this.layout = layout
return this
}


/**
* Provide PreDefined Colors,
*
* If colors is not empty, those colors will be shown instead of recent colors
* If colors is empty, recent colors will be shown
*
* @param colors List<String> List of Hex Colors
*/
fun setColors(colors: List<String>): ColorPickerDialog.Builder {
this.colors = colors
return this
}

/**
* Provide PreDefined Colors,
*
* If colors is not empty, those colors will be shown instead of recent colors
* If colors is empty, recent colors will be shown
*
* @param colors Array<String> List of Hex Colors
*/
fun setColors(colors: Array<String>): ColorPickerDialog.Builder {
this.colors = colors.toList()
return this
}

/**
* Set Color Listener
*
Expand Down Expand Up @@ -201,7 +242,9 @@ class ColorPickerDialog private constructor(
colorListener = colorListener,
dismissListener = dismissListener,
defaultColor = defaultColor,
colorShape = colorShape
colorShape = colorShape,
colors = colors,
layout = layout
)
}

Expand All @@ -225,12 +268,12 @@ class ColorPickerDialog private constructor(

// Setup Custom View
val inflater: LayoutInflater = LayoutInflater.from(context)
val dialogView = inflater.inflate(R.layout.dialog_color_picker, null) as View
val dialogView = inflater.inflate(layout, null) as View
dialog.setView(dialogView)

val colorPicker = dialogView.findViewById<ColorPickerView>(R.id.colorPicker)
val colorView = dialogView.findViewById<MaterialCardView>(R.id.colorView)
val recentColorsRV = dialogView.findViewById<RecyclerView>(R.id.recentColorsRV)
val colorPicker = dialogView.findViewById<ColorPickerView?>(R.id.colorPicker)
val colorView = dialogView.findViewById<CardView?>(R.id.colorView)
val recentColorsRV = dialogView.findViewById<RecyclerView?>(R.id.recentColorsRV)

val initialColor = if (!defaultColor.isNullOrBlank()) {
Color.parseColor(defaultColor)
Expand All @@ -239,16 +282,19 @@ class ColorPickerDialog private constructor(
}
colorView.setCardBackgroundColor(initialColor)

colorPicker.setColor(initialColor)
colorPicker.setColorListener { color, _ ->
colorPicker?.setColor(initialColor)
colorPicker?.setColorListener { color, _ ->
colorView.setCardBackgroundColor(color)
}

val sharedPref = SharedPref(context)
val colorList = sharedPref.getRecentColors()
var colorList = colors
if(colors.isEmpty()) {
colorList = sharedPref.getRecentColors()
}

// Setup Color Listing Adapter
val adapter = RecentColorAdapter(colorList)
val adapter = ColorPickerAdapter(colorList)
adapter.setColorShape(colorShape)
adapter.setColorListener(object : ColorListener {
override fun onColorSelected(color: Int, colorHex: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.dhaval2404.colorpicker.listener.ColorListener
import com.github.dhaval2404.colorpicker.model.ColorShape
import com.github.dhaval2404.colorpicker.util.SharedPref
import kotlinx.android.synthetic.main.adapter_material_color_picker.view.*
import androidx.cardview.widget.CardView

/**
* Adapter for Recent Color
Expand All @@ -16,8 +16,8 @@ import kotlinx.android.synthetic.main.adapter_material_color_picker.view.*
* @version 1.0
* @since 23 Dec 2019
*/
class RecentColorAdapter(private val colors: List<String>) :
RecyclerView.Adapter<RecentColorAdapter.MaterialColorViewHolder>() {
class ColorPickerAdapter(private val colors: List<String>) :
RecyclerView.Adapter<ColorPickerAdapter.ColorViewHolder>() {

private var colorShape = ColorShape.CIRCLE
private var colorListener: ColorListener? = null
Expand Down Expand Up @@ -45,22 +45,22 @@ class RecentColorAdapter(private val colors: List<String>) :
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MaterialColorViewHolder {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ColorViewHolder {
val rootView = ColorViewBinding.inflateAdapterItemView(parent)
return MaterialColorViewHolder(rootView)
return ColorViewHolder(rootView)
}

override fun onBindViewHolder(holder: MaterialColorViewHolder, position: Int) {
override fun onBindViewHolder(holder: ColorViewHolder, position: Int) {
holder.bind(position)
}

inner class MaterialColorViewHolder(private val rootView: View) :
inner class ColorViewHolder(private val rootView: View) :
RecyclerView.ViewHolder(rootView) {

private val colorView = rootView.colorView
private val cardView = rootView as CardView

init {
rootView.setOnClickListener {
cardView.setOnClickListener {
val index = it.tag as Int
if (index < colors.size) {
val color = getItem(index)
Expand All @@ -72,9 +72,9 @@ class RecentColorAdapter(private val colors: List<String>) :
fun bind(position: Int) {
val color = getItem(position)

rootView.tag = position
ColorViewBinding.setBackgroundColor(colorView, color)
ColorViewBinding.setCardRadius(colorView, colorShape)
cardView.tag = position
ColorViewBinding.setBackgroundColor(cardView, color)
ColorViewBinding.setCardRadius(cardView, colorShape)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.github.dhaval2404.colorpicker.R
import com.github.dhaval2404.colorpicker.model.ColorShape

/**
* Common method for {@see RecentColorAdapter and MaterialColorPickerAdapter}
* Common method for {@see ColorPickerAdapter and MaterialColorPickerAdapter}
*
* @author Dhaval Patel
* @version 1.0
Expand Down
6 changes: 6 additions & 0 deletions colorpicker/src/main/res/drawable/color_rounded_rectangle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#E6E6E6" />
<corners android:radius="8dp" />
</shape>
14 changes: 14 additions & 0 deletions colorpicker/src/main/res/layout/adapter_color_picker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/colorView"
android:layout_width="@dimen/color_card_size"
android:layout_height="@dimen/color_card_size"
card_view:cardBackgroundColor="@color/green_300"
android:foreground="@drawable/color_rounded_rectangle"
card_view:cardCornerRadius="10dp"
android:layout_margin="@dimen/color_card_margin"
android:elevation="4dp"
android:visibility="visible"
/>
19 changes: 10 additions & 9 deletions colorpicker/src/main/res/layout/dialog_color_picker.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">

<com.github.dhaval2404.colorpicker.ColorPickerView
Expand All @@ -16,18 +17,18 @@
android:layout_marginEnd="24dp"
android:minWidth="210dp" />

<com.google.android.material.card.MaterialCardView
<androidx.cardview.widget.CardView
android:id="@+id/colorView"
android:layout_width="@dimen/color_card_size"
android:layout_height="@dimen/color_card_size"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:cardBackgroundColor="@color/grey_500"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="true"
app:strokeColor="@color/white"
app:strokeWidth="2dp" />
card_view:cardBackgroundColor="@color/grey_500"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="4dp"
card_view:cardPreventCornerOverlap="true"
card_view:strokeColor="@color/white"
card_view:strokeWidth="2dp" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/recentColorTitleTxt"
Expand All @@ -49,7 +50,7 @@
tools:itemCount="10"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
tools:listitem="@layout/adapter_material_color_picker"
tools:listitem="@layout/adapter_color_picker"
android:layout_gravity="center_horizontal"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />

Expand Down
4 changes: 4 additions & 0 deletions colorpicker/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MaterialTheme" parent="ThemeOverlay.MaterialComponents.Dark"/>
</resources>
1 change: 1 addition & 0 deletions colorpicker/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogStyle.Button" parent="Widget.AppCompat.Button.Borderless.Colored" />
<style name="MaterialTheme" parent="Theme.MaterialComponents.Light"/>
</resources>
Loading