Android Listener with Silver

I am trying to do some Android programming with Silver, and one item I cannot figure out is how to create an anonymous local class to handle a multi-method listener. I know that the single method version works with a shortcut so only the lambda is needed, like so:

button.setOnClickListener { view in
	self.alert(message: "This is a message to show in the Alert - via Extension")
}

But for example a SeekBar.OnSeekBarChangeListener ? How would that be coded? In Kotlin we can do this:

val listener = object: SeekBar.OnSeekBarChangeListener {
    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Bool) {
    }
    override fun onStartTrackingTouch(seekBar: SeekBar) {
    }
    override fun onStopTrackingTouch(seekBar: SeekBar) { {
    }
}

Can we do something similar in Silver / Swift?

Thank you,
Tyler

Note that the syntax given above is wrong. I was able to implement it as an extension to a class, like so:

extension MainActivity: SeekBar.OnSeekBarChangeListener {
	func onProgressChanged(_ seekBar: SeekBar!, _ progress: Int32, _ fromUser: Bool) {
		print("onProgressChanged")
	}
	func onStartTrackingTouch(_ seekBar: SeekBar!) {
		print("onStartTrackingTouch")
	}
	func onStopTrackingTouch(_ seekBar: SeekBar!) {
		print("onStopTrackingTouch")
	}
}

But I would still like to know if it is possible to implement this as a local anonymous class.

Also note the need for ‘Int32’ where I think the default Swift ‘Int’ ought to work (the Swift ‘Bool’ works fine).

To answer my own question, after some research: Swift - and thus Silver I expect - does not support anonymous inline classes like the example in Kotlin. Instead, a regular inner class can used just as well. For example, this works for me:

		class SeekBarListener: SeekBar.OnSeekBarChangeListener {
			func onProgressChanged(_ seekBar: SeekBar!, _ progress: Integer, _ fromUser: Bool) {
				print("onProgressChanged")
			}
			func onStartTrackingTouch(_ seekBar: SeekBar!) {
				print("onStartTrackingTouch")
			}
			func onStopTrackingTouch(_ seekBar: SeekBar!) {
				print("onStopTrackingTouch")
			}
		}

Then we can hook it up like so:

		if let seekBar = findViewById(R.id.seekBar) as? SeekBar {
			seekBar.setOnSeekBarChangeListener(SeekBarListener())
		}

That is certainly more than adequate for my needs. Hope this helps others too.

It does, although the syntax takes some getting used to and could be improved upon. i’m away from my main computer right now, but i’ll try to get you an example from one of my own projects, tomorrow.

—marc

ok, this is from my app:

			loginButton.registerCallback(callbackManager, class FacebookCallback<LoginResult> {
				func onSuccess(loginResult: LoginResult!) {
					log("onSuccess, {0}", loginResult)
					// App code
				}

				func onCancel() {
					log("onCancel")
					// App code
				}

				func onError(exception: FacebookException!) {
					log("onException, {0}", exception)
					// App code
				}
			})

So you’d want something like

if let seekBar = findViewById(R.id.seekBar) as? SeekBar {
		seekBar.OnSeekBarChangeListener = class SeekBar.OnSeekBarChangeListener {
			func onProgressChanged(_ seekBar: SeekBar!, _ progress: Integer, _ fromUser: Bool) {
				print("onProgressChanged")
			}
			func onStartTrackingTouch(_ seekBar: SeekBar!) {
				print("onStartTrackingTouch")
			}
			func onStopTrackingTouch(_ seekBar: SeekBar!) {
				print("onStopTrackingTouch")
			}
		}
	}

Thank you very much for the example. Makes perfect sense. I should have tried that variation.

I appreciate the reply. Very much. Now to find a way to get rid of all the '_'s. :slight_smile:

1 Like