I'm trying to make a simple people counter app where the buttons will increment/decrement the amount of people. I'm trying to make a conditional that if the number of people (count) exceeds 15, make the text red. I tried to do text_name!!.setTextColor(Color.Red), but it doesn't seem to work with that. I attached the files in my project. I am making the app under MVP architecture.
Contract.kt
package com.example.peoplecounter
import android.graphics.Color
interface Contract {
interface View {
fun setCount(count: String)
fun setTotalCount(count: String)
fun changeTextColor()
fun hideButton()
fun viewButton()
}
interface Model {
fun getCount(): String
fun getFlag(): Boolean
// fun getZeroFlag() : Boolean
fun getTotalCount(): String
fun reset()
fun inc()
fun decr()
//fun Zero()
}
interface Presenter {
fun onAddCountButtonPressed()
fun onMinusCountButtonPressed()
fun onResetButtonPressed()
fun onOverCapacity()
fun isZero()
fun getCount() :Int
}
}
Model.kt
class Model : Contract.Model {
private var count = 0
private var totalCount = 0
private var overcapacity = false
//private var isZero = false
override fun getCount(): String = count.toString()
override fun getFlag(): Boolean = overcapacity
override fun getTotalCount(): String = totalCount.toString()
// override fun getZeroFlag() : Boolean = isZero
override fun reset() {
count = 0
totalCount = 0
}
override fun inc() {
count = 1
totalCount = 1
if(count > 15){
overcapacity = true;
}
}
override fun decr() {
count -= 1
if(count<0){
count = 0
}
}
}
Presenter.kt
package com.example.peoplecounter
import com.example.peoplecounter.Contract
class Presenter (private val model: Contract.Model, private val view: Contract.View): Contract.Presenter {
override fun onAddCountButtonPressed() {
model.inc()
view.setCount(model.getCount())
view.setTotalCount(model.getTotalCount())
}
override fun onMinusCountButtonPressed() {
model.decr()
view.setCount(model.getCount())
}
override fun onResetButtonPressed() {
model.reset()
view.setCount(model.getCount())
view.setTotalCount(model.getTotalCount())
}
override fun onOverCapacity(){
if(model.getFlag() == true){
view.changeTextColor()
}
}
override fun isZero(){
if(Integer.parseInt(model.getCount()) == 0){
view.hideButton();
}
else{
view.viewButton();
}
}
override fun getCount(): Int {
return Integer.parseInt(model.getCount())
}
}
MainActivity.kt
package com.example.peoplecounter
import android.content.Context
import android.content.SharedPreferences
import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), Contract.View {
private lateinit var presenter: Contract.Presenter
private var peoplecount: TextView? = null
private var totalcount: TextView? = null
private var plusbutton: Button? = null
private var minusbutton: Button? = null
private var resetbutton: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
peoplecount = findViewById(R.id.people_count_number)
totalcount = findViewById(R.id.total_number_text)
plusbutton = findViewById(R.id.increment_button)
minusbutton = findViewById(R.id.decrement_button)
resetbutton = findViewById(R.id.reset_button)
presenter = Presenter(Model(), this)
setOnClickListeners()
presenter.isZero()
presenter.onOverCapacity()
}
private fun setOnClickListeners() {
plusbutton!!.setOnClickListener {
presenter.onAddCountButtonPressed()
}
minusbutton!!.setOnClickListener {
presenter.onMinusCountButtonPressed()
}
resetbutton!!.setOnClickListener {
presenter.onResetButtonPressed()
}
}
override fun setCount(count: String) {
people_count_number!!.text = count
}
override fun setTotalCount(count: String) {
total_number_text!!.text = count
}
override fun changeTextColor() {
people_count!!.setTextColor(Color.CYAN)
people_count_number!!.setTextColor(Color.CYAN);
}
override fun hideButton() {
decrement_button?.visibility = View.GONE
}
override fun viewButton() {
decrement_button?.visibility = View.VISIBLE
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@ id/total_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Total: "
android:textSize="26sp" />
<TextView
android:id="@ id/total_number_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="26sp" />
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@ id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@android:color/black"
android:text="RESET"
android:textColor="@android:color/white" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@ id/people_count_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textSize="35sp"
app:layout_constraintRight_toLeftOf="@id/people_count"/>
<TextView
android:id="@ id/people_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" People"
android:textSize="35sp" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:panel="http://schemas.android.com/apk/res/it.kronplatz"
android:id="@ id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="@ id/ButtonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:baselineAligned="false" >
<Button
android:id="@ id/decrement_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:background="@android:color/holo_purple"
android:text="-"
android:textColor="@android:color/white" />
<Button
android:id="@ id/increment_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="@android:color/holo_purple"
android:text=" "
android:textColor="@android:color/white" />
</RelativeLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The methods we are focusing on are OnOverCapacity() in Presenter.kt, changeTextColor() in MainActivity.kt, and getFlag() in Model.kt
Thanks in advance!
CodePudding user response:
I think you can put onOverCapacity()
inside function onAddCountButtonPressed()
, if it exceeds 15, then proceed to call onOverCapacity()
, i.e.
override fun onAddCountButtonPressed() {
...
...
...
if (model.getCount() > 15) {
onOverCapacity()
}
}
and also, I think you need to call a function to restore the color if by any chance you reset the count, or minus the count and the count is not exceeds 15