Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johncodeos committed May 12, 2023
0 parents commit 18f5e44
Show file tree
Hide file tree
Showing 49 changed files with 813 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
46 changes: 46 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.example.colorpickerexample'
compileSdk 33

defaultConfig {
applicationId "com.example.colorpickerexample"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.appcompat:appcompat:1.6.1'

implementation 'com.google.android.material:material:1.8.0'

implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.colorpickerexample

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.colorpickerexample", appContext.packageName)
}
}
26 changes: 26 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ColorPickerExample"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
43 changes: 43 additions & 0 deletions app/src/main/java/com/example/colorpickerexample/ColorAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.colorpickerexample

import android.content.Context
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView

class ColorAdapter(
private val context: Context,
private val colors: List<Int>,
private val onItemClick: (Int) -> Unit
) : RecyclerView.Adapter<ColorAdapter.ColorViewHolder>() {

inner class ColorViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val colorView: View = itemView.findViewById(R.id.colorView)

fun bind(color: Int) {
val backgroundDrawable = ContextCompat.getDrawable(context, R.drawable.square_rounded_corners)?.mutate()
backgroundDrawable?.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
colorView.background = backgroundDrawable
itemView.setOnClickListener { onItemClick(color) }
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ColorViewHolder {
val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.color_item, parent, false)
return ColorViewHolder(view)
}

override fun onBindViewHolder(holder: ColorViewHolder, position: Int) {
val color = colors[position]
holder.bind(color)
}

override fun getItemCount(): Int {
return colors.size
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.colorpickerexample

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class GridSpacingItemDecoration(
private val spanCount: Int,
private val spacing: Int,
private val includeEdge: Boolean
) : RecyclerView.ItemDecoration() {

override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view) // item position
val column = position % spanCount // item column

val leftSpacing = spacing - column * spacing / spanCount
val rightSpacing = (column + 1) * spacing / spanCount

if (includeEdge) {
outRect.left = leftSpacing
outRect.right = rightSpacing

if (position < spanCount) { // top edge
outRect.top = spacing
}
outRect.bottom = spacing // item bottom
} else {
outRect.left = column * spacing / spanCount
outRect.right = spacing - (column + 1) * spacing / spanCount
if (position >= spanCount) {
outRect.top = spacing // item top
}
}
}
}
84 changes: 84 additions & 0 deletions app/src/main/java/com/example/colorpickerexample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.example.colorpickerexample

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.graphics.ColorUtils
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private lateinit var colorPickerDialog: AlertDialog
private lateinit var content: ConstraintLayout

private val colors = listOf(
Color.CYAN, Color.rgb(179, 157, 219), Color.MAGENTA, Color.rgb(245, 245, 220), Color.YELLOW,
Color.rgb(169, 169, 169), Color.GREEN, Color.rgb(244, 164, 96), Color.BLUE, Color.RED,
Color.rgb(255, 228, 181), Color.rgb(72, 61, 139), Color.rgb(205, 92, 92), Color.rgb(255, 165, 0), Color.rgb(102, 205, 170)
)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

content = findViewById(R.id.content)
val colorPickerButton: Button = findViewById(R.id.color_picker_button)
colorPickerButton.setOnClickListener { showColorPickerDialog() }
}

private fun showColorPickerDialog() {
val recyclerView = createColorPickerRecyclerView()
colorPickerDialog = createColorPickerDialog(recyclerView)
colorPickerDialog.show()
}

private fun createColorPickerRecyclerView(): RecyclerView {
val numColumns = 5 // Desired number of columns
val padding = dpToPx(15) // Convert 15 dp to pixels
val spacing = dpToPx(15) // Set the spacing between items in dp

return RecyclerView(this).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
layoutManager = GridLayoutManager(this@MainActivity, numColumns)
setPadding(padding, dpToPx(20), padding, padding) // Convert padding to pixels
adapter = ColorAdapter(this@MainActivity, colors) { selectedColor ->
applySelectedColor(selectedColor)
colorPickerDialog.dismiss()
}
addItemDecoration(GridSpacingItemDecoration(numColumns, spacing, true))
}
}

private fun createColorPickerDialog(recyclerView: RecyclerView): AlertDialog {
return AlertDialog.Builder(this, R.style.CustomAlertDialogTheme)
.setTitle("Choose a color")
.setView(recyclerView)
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
.create()
}

private fun applySelectedColor(selectedColor: Int) {
// Change Background Color
content.setBackgroundColor(ColorUtils.blendARGB(selectedColor, Color.BLACK, 0.3f)) // Make it darker
// Change Status Bar Background Color
window.statusBarColor = ColorUtils.blendARGB(selectedColor, Color.BLACK, 0.1f) // Make it darker
// Change the App Bar Background Color
supportActionBar?.setBackgroundDrawable(ColorDrawable(selectedColor))
}

private fun dpToPx(dp: Int): Int {
return (dp * resources.displayMetrics.density).toInt()
}

}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/square_rounded_corners.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="12dp" />
</shape>
Loading

0 comments on commit 18f5e44

Please sign in to comment.