I got an issue with SetOnClickListener in mine mobile kotlin app on AndroidStudio.
I created menu with this tutorial https://www.youtube.com/watch?v=sZWMPYIkNd8
Its ok! works fine on HAXM emulator but i can't manage to make mine button interactive..Tutorial guide tells me to initialize Button with SetOnClickListener
Android studio keeps asking me for more arguments and editors displays not-enough-information-to-infer-parameter-t-with-kotlin-and-android which makes me stuck.
I'm learning this language but resolving this problem is kinda out of mine range...what i need to know to properly implement OnClickListener?
Guy on youtube video doesnt put any extra phrase in brackets. so what i should do?
Make sure that you use braces {} instead of brackets after "view.setOnClickListener"
You can implement OnClickListeners a few different ways.
For a simple implementation you can do the following:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
button.setOnClickListener {
// do something when the user clicks the button
}
}
Or you can have your activity handle it like so:
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
<yourButton>.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v) {
(<yourButton>) -> {
// do something when the user clicks the button
}
else -> return
}
}
}
Here is a decent source if you want to read more.
https://antonioleiva.com/lambdas-kotlin-android/
Related
I'm new to Android development and I just started learning.
Here's my problem:
My onCreate function in activity_main works normally, it shows everything. Then he logs in and shows me the second activity and there also the onCreate function works but when I go to the third activity the onCreate function does not execute.
DashboardActivity.kt
class DashboardActivity : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
sendGet()
// sendPostRequest()
}
fun openTest(view: View){
setContentView(R.layout.activity_test);
}
}
TestActivity.kt
class TestActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
println("test")
}
}
Could someone guide me or explain what and how? I've been sitting on this for a while, searching the internet and nothing...
I searched the internet, tried solutions, tried to create more activities, turned on debug mode by adding breakpoint and it doesn't even enter this function
To start an Activity, you need to call the following:
startActivity(Intent(this, NextActivity::class.java))
Otherwise, onCreate(Bundle?) will not be called if you do not explicitly call the above.
So in your case, seems that you would like to get to TestActivity in openTest(View), you should have something like this:
fun openTest(view: View) {
startActivity(Intent(this, TestActivity::class.java))
// Calling setContentView() will not trigger another Activity
// setContentView(R.layout.activity_test);
}
I was trying to open some fragment however the app keep crashing
I am wondering if it was possible to fix the issue that I got
class HomePage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_page)
val addMed = findViewById<Button>(R.id.addMedButton)
val fragManager: FragmentManager = supportFragmentManager
fragManager.beginTransaction().add(R.id.frameLayout, addMed).commit()
}
}
You have to add a fragment, not a button.
Besides, if the button is found by id, it is already present in the activity.
I don't have enough skills to code. But I have to make this program. so please help me...
I want to make function to slide the screen sideways in Android but it isn't working what I made. how can I do? please tell me.
PS: I want to use 'View group' and this code is on the MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sv = SlidingView(this)
val v1 = View.inflate(this, R.layout.t1, null)
val v2 = View.inflate(this, R.layout.t2, null)
sv.addView(v1)
sv.addView(v2)
setContentView(sv)
setContentView(R.layout.activity_main)
}
}
Seems like you are overriding your SlidingView with activity_main, you are calling setContentView twice and the latest call overrides the first one, so your view gets replaced with what's in layout activity_main, if you do not have anything in activity_main, do remove setContentView(R.layout.activity_main)
or
if you want SlidingView to be a part of activity_main, then add it to activity_main xml instead.
I am trying to set OnClickListener on an Android button in Kotlin file. Unlike java file, where I can declare button variable at the class level and initialize it in onCreate method and assign ClickListener at the same time as:
Button inlineButton;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
inlineButton = findViewById(R.id.btn_inline);
inlineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new NotificationToast(MainActivity.this, "Inline Button");
}
});
}
when I try to follow similar pattern I get an error with the following message:
I can only set the event handler only when I create a copy of button with following code:
var inlineButton : Button? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
inlineButton = findViewById(R.id.btn_inline)
val inlineButtonCopy = inlineButton;
if (inlineButtonCopy != null) {
inlineButtonCopy.setOnClickListener({
NotificationToast(this,inlineButtonCopy.text.toString()).Show();
})
}
}
I was wondering if it is not possible to create only a single instance of Button on top level, initialize it in oncreate, and set event handler. If I can create only a single instance, I can use the event to change the property of the same button. Is it the default behavior or am I missing something.
Any clarification is highly appreciated.
In kotlin you don't need to describe the Button. You can directly call the id and set Listener. It is easier than java. Just don't get confused in your Id's
btn_inline.setOnClickListener({
NotificationToast(this,inlineButtonCopy.text.toString()).Show();
})
In Kotlin you don't need to define the Id's of the XML component.
you can directly access with the help of Id's are already defined in your XML.
let suppose you defined the Button Id in XML is btn_inline
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_inline.setOnClickListener({
NotificationToast(this,inlineButtonCopy.text.toString()).Show();
})
}
Also, make sure that something like this should be present there in the import section.
import kotlinx.android.synthetic.main.activity_main.*
Just check at your end.
Another option is to use Late-Initialized Property:
private lateinit var inlineButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
inlineButton = findViewById<Button>(R.id.btn_inline)
inlineButton.setOnClickListener {
NotificationToast(this,inlineButtonCopy.text.toString()).Show();
}
}
As for the title, I am a beginner of Android. When running github's sceneform-sample, I found that when the model appeared on the screen, a white circle would appear under the model. What methods can I use to remove the white circle at the bottom?
I have uploaded a picture so that you can understand my question more intuitively.
Just don't call node.select(), it should be fine.
I think you may have found an answer to your question in GitHub but for completeness here are some examples in Kotlin, setting the SelectionVisualiser, which allows you determine what is shown or not shown under a selected TransformableNode.
Example 1
Changing the SelectionVisualiser for the entire activity:
class StartViewMainActivity : AppCompatActivity() {
//included in the class variables
private lateinit var defaultSelectionVisualiser:SelectionVisualizer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//As part of OnCreate, set the selection visualiser
arFragment.transformationSystem.selectionVisualizer = CustomSelectionVisualiser()
}
private class CustomSelectionVisualiser(): SelectionVisualizer {
//Class providing a custom TransformableNode SelectionVisualiser
//This particular custom visualiser shows no image
override fun applySelectionVisual(node:BaseTransformableNode){}
override fun removeSelectionVisual(node:BaseTransformableNode){}
}
}
Example 2
Changing the SelectionVisualiser programatically during part of your program flow:
class StartViewMainActivity : AppCompatActivity() {
//included in the class variables
private lateinit var defaultSelectionVisualiser:SelectionVisualizer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//As part of OnCreate, get the default selection visualiser
defaultSelectionVisualiser = arFragment.transformationSystem.selectionVisualizer
}
fun toggelSelectionVisulaisers() {
//Switch between the default and the custom selectionVisualisers
if (arFragment.transformationSystem.selectionVisualizer == defaultSelectionVisualiser) {
arFragment.transformationSystem.selectionVisualizer = CustomSelectionVisualiser) else
} else {
arFragment.transformationSystem.selectionVisualizer = defaultSelectionVisualiser arFragment.transformationSystem.selectionVisualizer = CustomSelectionVisualiser) else
}
private class CustomSelectionVisualiser(): SelectionVisualizer {
//Class providing a custom TransformableNode SelectionVisualiser
//This particular custom visualiser shows no image
override fun applySelectionVisual(node:BaseTransformableNode){}
override fun removeSelectionVisual(node:BaseTransformableNode){}
}
}
Yes, you can,
there is two way one is don't select the model,
like don't call bellow method
node.select()
Second one is aplly transperent selection visualiser
First Create a Transparent Visualiser class like bellow
class BlanckSelectionVisualizer : SelectionVisualizer {
override fun applySelectionVisual(var1: BaseTransformableNode) {}
override fun removeSelectionVisual(var1: BaseTransformableNode) {}
}
Then after applying it on ArFragmnet like bellow
arFragment.transformationSystem.selectionVisualizer = BlanckSelectionVisualizer()
I suggest use second one