forked from amanss00/ForNewbies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolormyviews.kt
45 lines (37 loc) · 1.76 KB
/
colormyviews.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.example.colormyviews
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setListeners()
}
private fun setListeners() {
val clickableViews: List<View> = listOf(
box_one_text, box_two_text, box_three_text,
box_four_text, box_five_text, constraint_layout,
red_button, yellow_button, green_button)
for (item in clickableViews) {
item.setOnClickListener { makeColored(it) }
}
}
private fun makeColored(view: View) {
when (view.id) {
//Boxes using Color class for background
R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
//Boxes using Android Color Resources for background
R.id.box_three_text -> view.setBackgroundResource(android.R.color.holo_green_light)
R.id.box_four_text -> view.setBackgroundResource(android.R.color.holo_green_dark)
R.id.box_five_text -> view.setBackgroundResource(android.R.color.holo_green_light)
//Boxes using custom colors for background
R.id.red_button -> box_three_text.setBackgroundResource(R.color.my_red)
R.id.green_button -> box_four_text.setBackgroundResource(R.color.my_green)
R.id.yellow_button -> box_five_text.setBackgroundResource(R.color.my_yellow)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}