Skip to content

Commit

Permalink
Make TextView type changeable in real time.
Browse files Browse the repository at this point in the history
  • Loading branch information
slaviboy committed Apr 13, 2020
1 parent 6d08d5c commit abd4cd8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.slaviboy.colorpickerkotlinexample
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.slaviboy.colorpicker.Updater
Expand All @@ -14,6 +15,11 @@ import com.slaviboy.colorpickerkotlinexample.pickers.RectangularHSL
import com.slaviboy.colorpickerkotlinexample.pickers.RectangularHSV

class MainActivity : AppCompatActivity() {

lateinit var textViewChangeType: EditText
lateinit var labelViewChangeType: TextView
lateinit var updater: Updater

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -32,31 +38,52 @@ class MainActivity : AppCompatActivity() {
val colorConverter = ColorConverter(160, 73, 184, 50)

// create updater object, that will update all color window and text views
val updater = Updater(colorConverter)
updater = Updater(colorConverter)

// attach updater to all color pickers
rectangularHSV.attach(updater)
rectangularHSL.attach(updater)
circularHSV.attach(updater)

textViewChangeType = circularHSV.findViewById(R.id.rgba_value)
labelViewChangeType = circularHSV.findViewById(R.id.rgba_label)

// attach listener to the updater
updater.setOnUpdateListener(object : OnUpdateListener {
override fun onTextViewUpdate(textView: TextView) {
// triggered when user changes text view value
}

override fun onColorWindowUpdate(colorWindow: Base) {
// triggered when user changes color window value
}
})
}

/**
* Hide the system UI
*/
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemUI(this)
}
}

/**
* Change the text view type, by changing the tag and updating
* the it in the updater class.
*/
fun onButtonClick(view: View) {
if (::textViewChangeType.isInitialized) {
val tagString = (view as TextView).text.toString()
textViewChangeType.tag = tagString
labelViewChangeType.text = tagString
val type = Updater.getType(textViewChangeType.tag.toString())
updater.updateTextViewTag(textViewChangeType, type)
}
}

/**
* Enables regular immersive mode.
* For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
Expand All @@ -66,13 +93,13 @@ class MainActivity : AppCompatActivity() {

val decorView = activity.window.decorView
decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_IMMERSIVE // Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
(View.SYSTEM_UI_FLAG_IMMERSIVE // Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}

/**
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,34 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/picker2" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/rgb_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="onButtonClick"
android:text="RGB"
app:layout_constraintLeft_toLeftOf="@+id/picker3"
app:layout_constraintTop_toBottomOf="@+id/picker3" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/hsv_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="onButtonClick"
android:text="HSV"
app:layout_constraintLeft_toRightOf="@+id/rgb_button"
app:layout_constraintTop_toBottomOf="@+id/picker3" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/hsl_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="onButtonClick"
android:text="HSL"
app:layout_constraintLeft_toRightOf="@+id/hsv_button"
app:layout_constraintTop_toBottomOf="@+id/picker3" />

</androidx.constraintlayout.widget.ConstraintLayout>
10 changes: 9 additions & 1 deletion colorpicker/src/main/java/com/slaviboy/colorpicker/Updater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ class Updater(
}
}

fun updateTextViewTag(textView: TextView, type: Int) {

if (textViews.containsKey(textView)) {
textViews[textView] = type
updateTextView(textView)
}
}

/**
* Attach text view with preset type: hex
* @param textViewHEX - text view with hexadecimal type
Expand Down Expand Up @@ -1116,7 +1124,7 @@ class Updater(
*/
fun getType(strTag: String): Int {
if (strTag.isNotEmpty()) {
when (strTag) {
when (strTag.toLowerCase()) {
"rgb" -> return TYPE_RGB
"rgba" -> return TYPE_RGBA
"rgba_r" -> return TYPE_RGBA_R
Expand Down

0 comments on commit abd4cd8

Please sign in to comment.