I'm working with Exoplayer RTMP player but I'm facing this problem.
This is the screenshot and I don't know how to replace findViewById.
I am new to kotlin and really don't know how to replace this. How do I do this?
In Activity :
mEditText = findViewById(R.id.edittext_id)
In fragment :
mButtonContinue = requireView().findViewById(R.id.button_id)
With Kotlin you can directly call the ID of your XML and be done with it, without using findViewById
In your case you can juste use simple_player.player = player
Kotlin doc
Related
I am new to Kotlin programming. I didn't have any problems in other layouts, but now I need to use relativeLayout. I will create hundreds of buttons and their positions will be relative to each other I have to do this programmatically. I was able to find java examples but I couldn't find many resources in Kotlin because addRule is not here. Can you give me a small example of this?
Thanks in advance.
Thanks for ansver. I solved the addRule problem. now i see only one button at start instead of 1000 button, i think i have problem assigning id. Here is my simple code. i just want programmatically add years at top line between 1000-2000. After that, I will add the beginning and end of an event in 1378 to a bottom line as button. so i can assign dates easily, i chose relativeLayout for this.
val buttonParam = LayoutParams( 100, 100 ) var counter:Int=1000 while (true) { var btnId:Int=counter val myButton = Button(this) myButton.id=btnId buttonParam.addRule(RelativeLayout.RIGHT_OF, btnId-1) myButton.text = btnId.toString() myButton.layoutParams = buttonParam binding.rl.addView(myButton) counter=counter +1 if(counter==2000) break }
Edit: İ solve my problem. the problem is i create 1 buttonParam. Now i am creating parameter for every buttons.
I try to do this:
cardview.cardBackgroundColor(Color)
but I have this message: The function 'invoke()' is not found.
Any idea?
Did you tried using
cardView.setCardBackgroundColor(Color.RED);
or create any color from a reference like below
val color = ContextCompat.getColor(this#SplashActivity, R.color.colorPrimary)
cardView.setCardBackgroundColor(color);
I found it, I forgot the set
cardview.setCardBackgroundColor(Color)
Everything works fine now, the background of my cardview change.
Logging Java in Intellij is easy with shortcuts such as 'logt', 'logd', 'loge'... and so on. But I moved to Kotlin, I noticed that those shortcuts doesn't work anymore. I don't know if it has something to do with my configuration, but if not, how can I fix this?
You should create separate templates to make them work correctly.
Here is the step-by-step guide:
Firstly, Copy and paste AndroidLog templates to Kotlin (Just select them and use CMD+C, CMD+V (or Ctrl+C, Ctrl+V)
Secondly, You have to adjust them manually:
1. logd (and others)
Select the logd item and press "Edit variables"
Change expression to: kotlinFunctionName()
Also, remove ; from the end of the template, as you don't need it in Kotlin.
Now your method name will be shown correctly
logt
This one is a bit trickier.
Solution 1 TAG = class name.
Template text :
private val TAG = "$className$"
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", kotlinClassName())
Solution 2 TAG = file name (can be used inside Companion)
Template text :
private const val TAG = "$className$
or:
companion object {
private const val TAG = "$className$"
}
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())
Edit 19.02.19
Also, it might be useful for someone.
In order to avoid creating the TAG variable, you can use the class name as a variable, for instance:
Log.d("BaseActivity", "onCreate: ")
Where BaseActivity is the class name.
The template will look now as:
android.util.Log.d("$CLASS_NAME$", "$METHOD_NAME$: $content$")
Where CLASS_NAME variable is:
groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())
These are provided in IntelliJ as a Live Template configuration for AndroidLog (found in Preferences -> Editor -> Live Templates), and are applicable specifically to Java code:
There isn't anything broken in your configuration, but if you want to make these Live Templates available for Kotlin you will need to add new Live Template for AndroidLog and make them applicable to Kotlin code.
https://www.jetbrains.com/help/idea/2017.1/creating-and-editing-live-templates.html
There's an open feature request to have them added as defaults here: https://youtrack.jetbrains.com/issue/KT-10464
Change the scope of the template in the applicable option.
In Android Studio 4.0 there's new AndroidLogKotlin block. You can implement #LeoDroidcoder's solution there.
I'm currently developing Android applications on Android
I was getting tired of writing SomeView view = (SomeView) findViewById(R.id.view_name); over and over again and decided to try and write a template for it.
I discovered Live Templates and decided to try and write on for this code snippet. I've managed to write the following:
= ($CLASS_NAME$) findViewById(R.id.$END$);
This will start the cursor at $CLASS_NAME$ and jump it to $END$ when I press Tab.
Which is nice but I'd like it to insert the first class name it finds, working backwards, and insert that into $CLASS_NAME$. Thus writing SomeView view and then calling the template would insert SomeView at $CLASS_NAME$.
I realise that the solution, if there is one, is probably to write a custom expression for the variable but I just don't know how to go about that.
I don' think you easily make a backward reference, but you can try someting like this:
$CLASS_NAME$ $END$ = ($CLASS_NAME$) findViewById(R.id.$END$);
or
$CLASS_NAME$ $VAR$ = ($CLASS_NAME$) findViewById(R.id.$END$);
I'm not sure if I'm going crazy. I've tested this on Kitchen sink also so it's not just me.
I've tried:
Ext.ComponentQuery.query('container[cls="blah"]');
Ext.ComponentQuery.query('container[cls~="blah"]');
But after it has a second class, it seems you can't get ahold of something by a class that it has.
Am I missing something or is this not possible?
If you go to http://dev.sencha.com/deploy/touch/e.../#demo/buttons
Ext.ComponentQuery.query('button')[0];
// returns element
Ext.ComponentQuery.query('button')[0].addCls('meep');
Ext.ComponentQuery.query('button[cls="meep"')[0];
// returns element
Ext.ComponentQuery.query('button')[0].addCls('blah');
Ext.ComponentQuery.query('button[cls="meep"')[0];
// returns undefined
Ext.ComponentQuery.query('button[cls~="meep"')[0];
// returns undefined
Because of what I expect and how the documentation words their DomQuery I think the above should work but is bugged.
I got around this by creating a new xtype and using that instead in ComponentQuery like so:
Ext.define('App.view.Deposit', {
extend: 'Ext.Container'
});
Ext.ComponentQuery('meep');
I guess I was trying to go about it like you would in jQuery, add a class and retrieve it using that, but with the Component stuff it's confusing.
I think this SHOULD have worked but it does not (tested in 2.0.1.1, 2.1.0b3):
Ext.ComponentQuery.query('button[cls*="meep"')[0];