Comments (16)
I found how can I to use certain this
lineChart {
setOnChartValueSelectedListener(this@MainActivity)
}
But my other issues still to be resolved.
from anko.
Hello, @xgrommx!
You can make use of ctx
and act
extension properties in any class that extends Activity
(explained in Instance shorthands part of manual) as well as using this@MainActivity
syntax.
As for replacing setData()
with data
property, Kotlin does not provide native properties for Java classes automatically. Properties for the Views
in Android SDK are contained in Koan, though you must write your own for the custom types.
For example,
var <T> Chart<T>.data: T
get() = getData()
set(value) = setData(value)
from anko.
@yanex I don't know when I should put this code =(
var <T> Chart<T>.data: T
set(value) = setData(value)
from anko.
Also I have yet another question. Now I use this construction:
var set1 = RadarDataSet(generateSourceY(2), "Set 1")
set1.setColor(Color.RED)
set1.setDrawFilled(true)
set1.setLineWidth(2f)
But I would like use it as in C#(I'm .net developer, but kotlin of interest to me)
RadarDataSet() {
source = generateSourceY(2)
label = "Set 1"
color = Color.RED
drawFilled = true
lineWidth = 2f
}
or
RadarDataSet(generateSourceY(2), "Set 1") {
color = Color.RED
drawFilled = true
lineWidth = 2f
}
Could you please explain how can I do it?
from anko.
I don't know when I should put this code =(
You could put it in any .kt
file in your project from where you could import it.
If you have lots of custom components, it would be easier to manage them if put in the separate file (CustomDslComponents.kt
or similar).
By the way, your property declaration doesn't contain get()
clause. As for now, val
property in Kotlin must contain a getter, and var
(mutable) property — a pair of getter and setter.
Could you please explain how can I do it?
The code is similar as for getData()
except it has different types:
var RadarDataSet.lineWidth: Float
get() = getLineWidth()
set(value) = setLineWidth(value)
And so on. Also you could use a Kotlin's with()
function:
with(RadarDataSet()) {
// `this` now is a RadarDataSet instance
color = Color.RED
lineWidth = 2f
}
You can read more about extension functions and properties here.
from anko.
But with
return Unit type instead of a new instance RadarDataSet =( and if I use without with
operator I got errors =>
var set = RadarDataSet(generateSourceY(2), "Set 1") {
color = Color.RED
}
from anko.
Well, you can either return this
from lambda:
val dataSet = with(RadarDataSet()) {
color = Color.RED
this
}
or replace with
function with the custom function that returns the argument instead of lambda calculation result:
inline fun <T> have(obj: T, init: T.() -> Unit): T {
obj.init()
return obj
}
val dataSet = have(RadarDataSet()) {
color: Color.RED
}
As an alternative, you could make a new function specially for the RadarDataSet() type that imitates constructor:
fun RadarDataSet(init: RadarDataSet.() -> Unit): RadarDataSet {
val obj = RadarDataSet()
obj.init()
return obj
}
val dataSet = RadarDataSet {
color: Color.RED
}
from anko.
Thanks! It's work now!)
public var RadarDataSet.color: Int
get() = getColor()
set(value) = setColor(value)
public var RadarDataSet.lineWidth: Float
get() = getLineWidth()
set(value) = setLineWidth(value)
public var RadarDataSet.drawFilled: Boolean
get() = isDrawFilledEnabled()
set(value) = setDrawFilled(value)
fun RadarDataSet(list: ArrayList<Entry>, label: String, init: RadarDataSet.() -> Unit): RadarDataSet {
val obj = RadarDataSet(list, label)
obj.init()
return obj;
}
var set = RadarDataSet(generateSourceY(2), "Set1") {
color = Color.RED
lineWidth = 2f
drawFilled = true
}
}
As I understood the same way for radarChart?
fun ViewManager.radarChart(init: RadarChart.() -> Unit = {}) = __dslAddView({ RadarChart(it)}, init, this)
I just need create similar function and getters, setters and I could be use it like a RadarDataSet?
from anko.
Yes, that's it!
from anko.
But that is not working in this case. RadarChart has the method getXLabels()
which return XLabels class.
public var XLabels.textSize: Float
get() = getTextSize()
set(value) = setTextSize(value)
And this code doesn't work now. No errors but and no results of work
getXLabels() {
textSize = 9f
}
How can I solve this case?
from anko.
Which getXLabels()
function do you use? If one defined in RadarChart.java
, the code you provided is unlikely to compile.
It would be better if you show some additional code.
from anko.
Sure, all files you can see in my repository. Now I use java way https://github.com/xgrommx/KoanMPAndroidChartExample/blob/master/app/src/main/kotlin/com/gromm/koanexample/MainActivity.kt#L152 but will be good if I can use a declarative style.
from anko.
Well, you could write this (the fastest way if you don't need the result):
with (getXLabels()) {
setTextSize(9f)
}
Or write an another extension function:
fun RadarChart.xLabels(init: XLabels.() -> Unit): XLabels {
val obj = getXLabels()
obj.init()
return obj
}
xLabels {
setTextSize(9f)
}
from anko.
Awesome! Thanks, as for me Kotlin looks very clearly unlike Scala and it's good. And for Android Scala has very big runtime library. My choise for Android it's Kotlin =) Could I to ask you in future if I have questions?
from anko.
Also I have one small question. This is correct syntax or I could you use smth instead of a ViewManager
fun ViewManager.radarChart(init: RadarChart.() -> Unit = {}) = __dslAddView({ RadarChart(it)}, init, this)
Now I cannot extract a declaration of my radarChart
in another function because this one required parent layout like a verticalLayout or etc.
from anko.
Sorry for the long response.
You could extract part of your DSL as an extension function.
verticalLayout {
frameLayout {
textView("Text")
}
}
And extracted version:
verticalLayout {
extractedFunction()
}
fun LinearLayout.framedText() {
frameLayout {
textView("Text")
}
}
from anko.
Related Issues (20)
- Alerts with no system bar, or: how to get dialog before show() ? HOT 1
- How to use linearLayout in verticalLayout? HOT 1
- hintTextColor can not work in anko? HOT 1
- Error: void org.jetbrains.anko.appcompat.v7._AlertDialogLayout.forceUniformWidth(int, int) is not being kept as e, but remapped to d
- Feature request: share file
- Android Q support
- Maybe Not update anymore for this project? HOT 1
- Maybe Not update anymore for this project? HOT 11
- Is this project discontinued? HOT 5
- Textview cannot accept new line from Resources strings HOT 2
- something issues of anko.include() dsl
- ***** BEWARE -- this project seems to be abandoned. ***** HOT 14
- Fix reference - Listeners not working HOT 1
- about Anko performance HOT 8
- Selector no cancel listener HOT 1
- textView.setMaxWidth() does not work
- Is SDK28 Available In A Repo Somewhere? HOT 1
- Style selector text? HOT 1
- Please archive this repository. HOT 3
- What if I don't want to add the view directly?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anko.