Home > database >  How To get value of SeekBar and use it on another class or method
How To get value of SeekBar and use it on another class or method

Time:01-20

I'm new in Kotlin and I want to use three Seekbar on my app for RGB control;
I want to set seekBarManger function to manage my seekbar on SeekBarManger class and use seekbar.progress value on other class but I cant get value of my seekbar.
the Toast is Show but I want to use the value of seekbar.progrees in other method and class. I want to get seekbar.progress of three seekbar for control RGB. I have Red seekbar , Green seekBar and Blue seekbar and I want that when I drag red seekbar and green and blue , I want to set Background for an image view that use value of seekbar . I want use this values for set background that Combination of three color . I'm write the pain function for get three values of my seekbar and set a color for my image view background; My SeekbarManger class is this : please help me..!!!! this is my seekbar class :


class SeekBarManager()  {


}

fun seekBarManage(context: Context, seekBar: SeekBar) {



   seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {


       override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) { }

       override fun onStartTrackingTouch(p0: SeekBar?) {}

       override fun onStopTrackingTouch(p0: SeekBar?) {


           val result = seekBar.progress

           Toast.makeText(context, "Progress is: $result%", Toast.LENGTH_SHORT).show()


       }


   })
fun paint(view: View, red: Int, green:Int, blue:Int){


   fun View.getLocationOnScreen(): Point
   {
       val location = IntArray(2)
       this.getLocationOnScreen(location)
       return Point(location[0],location[1])
   }
   val location = view.getLocationOnScreen()
   val absX = location.x
   val absY = location.y

   val bitmap = Bitmap.createBitmap(absX , absY , Bitmap.Config.RGB_565)
   val canvas = Canvas(bitmap)
   canvas.drawRGB(red , green , blue )
   view.background = BitmapDrawable(view.resources , bitmap)


}

and this is my MainActivity :

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


        val seekRed  = findViewById<SeekBar>(R.id.seekBar_red)
        val seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
        val seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)
        
        val seekEndRed = seekBarManage(this , seekRed)
        val seekEndGreen = seekBarManage(this , seekGreen)
        val seekEndBlue = seekBarManage(this , seekBlue)
     
       paint(color_output , seekEndRed , seekEndGreen , seekEndBlue)

the Toast is Show but I want to use the value of seekbar.progrees in other method and class

CodePudding user response:

the easiest way is to use a companion object in your Main class and just call a method and pass your seek bar object like below :

open class MainActivity : AppCompatActivity() {

    companion object {
        fun getSeekBarProgress(context: Context, seekBar: SeekBar) {
            val result = seekBar.progress
            Toast.makeText(context, "Progress is: $result%", Toast.LENGTH_SHORT).show()

    }
}

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


        val seekRed = findViewById<SeekBar>(R.id.seekBar_red)
        val seekGreen = findViewById<SeekBar>(R.id.seekBar_green)
        val seekBlue = findViewById<SeekBar>(R.id.seekBar_blue)

        val seekEndRed = seekBarManage(this, seekRed)
        val seekEndGreen = seekBarManage(this, seekGreen)
        val seekEndBlue = seekBarManage(this, seekBlue)
    }
}

and SeekBarManager class :

class SeekBarManager() {


}

    fun seekBarManage(context: Context, seekBar: SeekBar) {


    seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {


        override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}

        override fun onStartTrackingTouch(p0: SeekBar?) {}

        override fun onStopTrackingTouch(p0: SeekBar?) {

            MainActivity.getSeekBarProgress(context, seekBar)

        }


    })
    }
  •  Tags:  
  • Related