I am currently creating a drawing app with Kotlin, and whenever I change the thickness of my paint (Using a seekBar), it changes the thickness of the paint I've already drawn. This is not what I intended to happen. As you might have already guessed, I only want to change the thickness of paint that the user is about to draw.
I really hope someone could help, as I have tried lots of things, and nothing has succeed, as of yet... Hope someone could turn that record around.
Here is the code for my Main Activity (It is called Collab) Just for your notice, the 'colourPicker' button, is just leading to a activity with many more buttons, of different colours. There are actually no dialogs or sharedPreferences used, I was just fiddling around with those functions.
Here is the code, itself:
package com.example.savedtrial
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Window
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.savedtrial.PaintView.Companion.colorList
import com.example.savedtrial.PaintView.Companion.currentBrush
import com.example.savedtrial.PaintView.Companion.pathList
class Collab : AppCompatActivity() {
companion object{
var path = Path()
var paintBrush = Paint()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_collab)
val actionBar = supportActionBar
actionBar!!.title = "Whiteboard"
actionBar.setDisplayHomeAsUpEnabled(true)
//saveData()
val redBtn = findViewById<ImageButton>(R.id.redColour)
val blackBtn = findViewById<ImageButton>(R.id.blackColour)
val eraser = findViewById<ImageView>(R.id.eraser)
val colourPicker = findViewById<ImageView>(R.id.colourPicker)
val clearBoard = findViewById<ImageView>(R.id.clearBoard)
val sharedPref = getSharedPreferences("myPref", Context.MODE_PRIVATE)
val editor = sharedPref.edit()
redBtn.setOnClickListener{
Toast.makeText(this, "Red Colour Chosen", Toast.LENGTH_SHORT).show()
paintBrush.color = Color.RED
currentColor(paintBrush.color)
}
eraser.setOnClickListener{
Toast.makeText(this, "Eraser Chosen", Toast.LENGTH_SHORT).show()
paintBrush.color = Color.WHITE
currentColor(paintBrush.color)
}
val thicknessSeekbar = findViewById<SeekBar>(R.id.thicknessSeekbar)
thicknessSeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
paintBrush.strokeWidth = progress.toFloat()
//saveData()
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
//saveData()
}
})
colourPicker.setOnClickListener{
//loadData()
val intent = Intent(this, ColourPicker::class.java)
startActivity(intent)
//loadData()
}
blackBtn.setOnClickListener{
Toast.makeText(this, "Black Colour Chosen", Toast.LENGTH_SHORT).show()
paintBrush.color = Color.BLACK
currentColor(paintBrush.color)
}
clearBoard.setOnClickListener {
Toast.makeText(this, "Board Cleared", Toast.LENGTH_SHORT).show()
pathList.clear()
colorList.clear()
path.reset()
}
}
private fun showCustomDialogBox() {
val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.pick_a_colour_attempt_popup)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
private fun currentColor(color: Int){
currentBrush = color
path = Path()
}
private fun saveData(){
val strokePref = paintBrush.strokeWidth.toFloat()
//redBtn.Button = strokePref
//blackBtn = strokePref
val sharedPreferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.apply {
putFloat("STROKE_PREF", strokePref)
}.apply()
}
private fun loadData() {
val sharedPreferences = getSharedPreferences("sharedPrefs", MODE_PRIVATE)
val savedFloat = sharedPreferences.getFloat("STROKE_PREF", 8f)
paintBrush.strokeWidth = savedFloat
}
}
Here is the activity of the MainActivity, just so you can better understand what I was talking about in the code above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/white"
tools:context=".Collab">
<LinearLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/toolbar_background"
android:gravity="center"
tools:ignore="MissingConstraints">
<ImageButton
android:id="@+id/blackColour"
android:layout_width="50dp"
android:layout_margin="10dp"
android:layout_height="50dp"
android:background="@drawable/black_background"
android:contentDescription="Black"/>
<ImageButton
android:id="@+id/redColour"
android:layout_width="50dp"
android:layout_margin="10dp"
android:layout_height="50dp"
android:background="@drawable/red_background"
android:contentDescription="Red"/>
<ImageView
android:id="@+id/colourPicker"
android:layout_width="50dp"
android:layout_margin="10dp"
android:layout_height="50dp"
android:src="@drawable/img_2"/>
<ImageView
android:id="@+id/eraser"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
app:srcCompat="@drawable/eraser_useless" />
<ImageView
android:id="@+id/clearBoard"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/img_5"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
tools:ignore="UnknownId">
<include layout="@layout/paint_view"/>
</RelativeLayout>
<SeekBar
android:id="@+id/thicknessSeekbar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/toolbar"
android:layout_marginTop="20dp"
android:backgroundTint="@color/darkRED"
android:background="@drawable/toolbar_background"
android:layout_marginRight="10dp"
android:layout_marginLeft="50dp"/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/draw"
android:layout_marginTop="16dp"
android:layout_marginLeft="5dp"
android:layout_below="@+id/toolbar"/>
</RelativeLayout>
This is the code for the canvas, the layout is just a blank white screen:
package com.example.savedtrial
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.motion.widget.TransitionBuilder.validate
import com.example.savedtrial.Collab.Companion.paintBrush
import com.example.savedtrial.Collab.Companion.path
class PaintView: View {
var params : ViewGroup.LayoutParams? = null
companion object{
var pathList = ArrayList<Path>()
var colorList = ArrayList<Int>()
var currentBrush = Color.BLACK
}
constructor(context: Context) : this(context, null){
init()
}
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0){
init()
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
private fun init(){
paintBrush.isAntiAlias = true
paintBrush.color = currentBrush
paintBrush.style = Paint.Style.STROKE
paintBrush.strokeJoin = Paint.Join.ROUND
if (paintBrush.strokeWidth == 8f) {
true
} else {
true
}
params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
var x = event!!.x
var y = event.y
when(event.action){
MotionEvent.ACTION_DOWN -> {
path.moveTo(x,y)
return true
}
MotionEvent.ACTION_MOVE ->{
path.lineTo(x,y)
pathList.add(path)
colorList.add(currentBrush)
}
else -> return false
}
postInvalidate()
return false
}
override fun onDraw(canvas: Canvas) {
for(i in pathList.indices){
paintBrush.color = colorList[i]
canvas.drawPath(pathList[i], paintBrush)
drawableStateChanged()
invalidate()
}
}
}
Anyways, really hope someone can help my situation here. Good luck, and thanks for taking the time to read until the end.
Nevermind guys. I used ChatGPT, and by doing so, I realised it would've been way to complicated for someone to try and solve via Stack Overflow. But thanks to anyone who was considering, or trying to help!