How to use this library in Kotlin? - android

colorPicker.setColorSelectionListener(new SimpleColorSelectionListener() {
#Override
public void onColorSelected(int color) {
// Do whatever you want with the color
imageView.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
});
I am trying to use this color picker library which is in Java. I am not able to convert the code into Kotlin. How can I do so?

Will probably look something like
colorPicker.seColorSelectionListener(object : SimpleColorSelectionListener() {
override fun onColorSelected(color:Int) {
imageView.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
});

you can simply copy paste the code and Android studio will convert. For convenience you can use it like.
colorPicker.setColorSelectionListener(object : SimpleColorSelectionListener() {
override fun onColorSelected(color: Int) {
// Do whatever you want with the color
}
})

Related

How #sample is used in dokka API documentation in Android/Kotlin

I am generating the documentation for my public API/functions. It works well. But now I want to include the samples (Example code) also in the documentation. I know we can achieve it using #sample, but somehow I am not able to do it.
/**
#sample com.example.samples.Samples.doStuffSample
*/
fun doStuff(path: String, deviceID: String, newName: String,): Operation {
// Do stuff here
}
This is my Sample class in which, I have the sample codes.
class Samples {
fun doStuffSample() {
Manager.doStuff("path", "deviceID", "newName", object : Delegate {
override fun onSuccess() {
// Success
}
override fun onError() {
// Error
}
})
}
}
This is build.gradle file.
dokkaJavadoc.configure {
dokkaSourceSets {
named("main") {
skipDeprecated.set(true)
skipEmptyPackages.set(true)
includeNonPublic.set(false)
reportUndocumented.set(true)
perPackageOption {
skipEmptyPackages.set(true)
includeNonPublic.set(false)
matchingRegex.set("//package name")
suppress.set(false)
matchingRegex.set("//package name")
suppress.set(true)
}
}
}
}
In generated documentation, I can only see "com.example.samples.Samples.doStuffSample" below the "Samples" header.
How can I achieve this? Any help is appreciated.

com.jaygoo.widget.RangeSeekBar : Android Seekbar show progress value along the seekbar

I use as library "com.jaygoo.widget.RangeSeekBar" to get a Range Seek Bar.
Here's my following code XML :
<com.jaygoo.widget.RangeSeekBar
android:id="#+id/seekBarPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:rsb_min="1"
app:rsb_max="5000"
app:rsb_gravity="center"
app:rsb_indicator_background_color="#color/white"
app:rsb_indicator_show_mode="alwaysShow"
app:rsb_indicator_text_color="#color/darkGrey"
app:rsb_indicator_text_size="10sp"
app:rsb_mode="range"
app:rsb_progress_color="#color/honey"
app:rsb_thumb_drawable="#drawable/circle"/>
This RangeSeekBar used to specify the price range, I would like to know How can I add the "$" symbol at the indicator in my seekrangeBar as the follwonig picture :
I add the following kotlin code :
seekBarPrice.leftSeekBar.setIndicatorText("$1")
seekBarPrice.rightSeekBar.setIndicatorText("$1")
seekBarPrice.setRange(1F,5000F)
seekBarPrice.setOnRangeChangedListener(object: OnRangeChangedListener {
override fun onStartTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
}
override fun onRangeChanged(
view: RangeSeekBar?,
leftValue: Float,
rightValue: Float,
isFromUser: Boolean
) {
Log.d("tag", "Value: $leftValue")
seekBarPrice.leftSeekBar.setIndicatorText("$".plus(leftValue.toInt()))
seekBarPrice.rightSeekBar.setIndicatorText("$".plus(rightValue.toInt()))
}
override fun onStopTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
}
})
And my problem is solved
In your library to put % sign this way so you will change as per your requirement:
seekBarPrice.setIndicatorTextStringFormat("$%s%")
I hope it'll help you...!
I think that's the right way
rangePrice.setOnRangeChangedListener(new OnRangeChangedListener() {
#Override
public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser)
{
}
#Override
public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
}
#Override
public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
view.setIndicatorTextDecimalFormat("$%s%");
}
});

Kotlin: pass a reference / pointer to a function

For Android development I am using Kotlin. There are various buttons (buttonA, buttonB) to call the same function. The only difference is that the same function is called with different parameters (REQUEST_A, REQUEST_B). Following Code is running fine:
fun standardizedFunction(requestCode: Int){
....}
buttonA.setOnClickListener { standardizedFunction(REQUEST_A) }
buttonB.setOnClickListener { standardizedFunction(REQUEST_B) }
Now the question: Is there a way to make it more elegant? like
fun standardizedFunction(Object: Pointer, requestCode: Int){
Object.setOnClickListener{
....
}
}
standardizedFunction(buttonA,REQUEST_A)
standardizedFunction(buttonB,REQUEST_B)
You can make it "nicer" by using an extension method to View to setup the listener:
fun View.standardizedFunction(requestCode: Int) = setOnClickListener {
...
}

Kotlin and Android Espresso tests: Adding extension function with receiver

I'm still working on improving my understanding of extension functions with receivers and need some help of you experts on a question I have regarding this.
I have an Android Espresso testcase where I check that I have selected the items of a recyclerview. This is the same code repeated many times. I was wondering if it would be possible using kotlins extension functions with receiver to simplify this.
My test code now:
#Test
public void shouldSelectAll() {
...
onView(withRecyclerView(R.id.multiselectview_recycler_view).atPosition(0))
.check(RecyclerViewMatcher.isSelected(true));
onView(withRecyclerView(R.id.multiselectview_recycler_view).atPosition(1))
.check(RecyclerViewMatcher.isSelected(true));
onView(withRecyclerView(R.id.multiselectview_recycler_view).atPosition(2))
.check(RecyclerViewMatcher.isSelected(true));
}
Is it some how possible to create a function atPositions(varag positions: Int) that would take an integer array and call the assertion on each of the positions in the array. Like this:
#Test
public void shouldSelectAll() {
...
onView(withRecyclerView(R.id.multiselectview_recycler_view).atPositions(0, 1, 2))
.check(RecyclerViewMatcher.isSelected(true));
}
Sure!
private fun Int.matchAsRecyclerView(): RecyclerViewMatcher = withRecyclerView(this)
private fun RecyclerViewMatcher.checkAtPositions(vararg indices: Int, assertionForIndex: (Int) -> ViewAssertion) {
for(index in indices) {
onView(this.atPosition(index)).let { viewMatcher ->
viewMatcher.check(assertionForIndex(index))
}
}
}
Which should work as
R.id.multiselectview_recycler_view.matchAsRecyclerView().checkAtPositions(0, 1, 2, assertionForIndex = {
index -> RecyclerViewMatcher.isSelected(true)
})

Remove "this" callback in kotlin

I'm a bit kotlin newbie and I'm trying to remove the callback instance inside the callback itself.
What I'm trying to achieve it's something similar to the following code.
private val myCallback = SomeInterfaceType {
if(it.something) {
someObject.removeListener(this#SomeInterfaceType)
}
}
Of course it doesn't compile or else I wouldn't be asking here. So I ask, how to remove the callback from inside the instance of the interface?
edit:
the error is "inferred type is X but Y was expected.
edit 2: I just realized I've asked the wrong question, it's similar to it but not exactly a Interface.
The object I'm using have the following constructor/interface
public open class Watcher<T> public constructor(call: (T) -> kotlin.Unit)
so in reality I'm trying to reference the Watcher from inside the call: (T) -> kotlin.Unit to remove the listener.
Is that possible?
You need to use a full object expression syntax to refer to be able to refer to the instance itself:
private val myCallback = object: SomeInterfaceType() {
override fun onSomeEvent() {
if (it.something) {
someObject.removeListener(this)
}
}
}
There's also a workaround: wrap the reference to myCallback into a lambda passed to a function that calls it (e.g. run { ... }):
private val myCallback: SomeInterfaceType = SomeInterfaceType {
if (it.something) {
someObject.removeListener(run { myCallback })
}
}

Categories

Resources