skip to Main Content

Very basic Android Studio question:

How do I activate that Toast?

Normally, if I type in anything in Android Studio, I get a list of code I can Alt+Enter on / click on and it implements it. How do I implement that in the attached picture? so it looks like this:

I clicked the buttons that normally would implement stuff like Alt+Enter but that doesn’t seem to be working. How do I get that second image from that first image?

2

Answers


  1. The bracket is the scope of the on click listener which has the view so it always there. There is no difference between the two images you have aside from the brackets being expanded out. The it is of type View and always going to be view in the case. This is a lambda so there is nothing else you need to "implement", this is the callback for the button click. Notice how onClick gives you the view as a parameter? This is exactly the same thing just less verbose

    Example:

    binding.continueBtn.setOnClickListener{
        Log.d("MyActivity", "Button Clicked")
        it.getId() //ID of the view you have in your xml
    }
    

    is the same as

    binding.continueBtn.setOnClickListener{ view ->
        Log.d("MyActivity", "Button Clicked")
        view.getId() 
    }
    

    And all are the same as the more verbose way

    binding.continueBtn.setOnClickListener(object: View.OnClickListener()){
        public void onClick(View v) {
            Log.d("MyActivity", "Button Clicked")
        }
    }
    

    Doing it the last way, Android Studio will suggest that you do the other ways anyway

    Login or Signup to reply.
  2. Position your caret next to the first curly bracket {, then press Alt+Enter/Option+Enter, this will open up this menu, select the "Enable option ‘Implicit receivers and parameters’ for ‘Lambdas’ inlay hints".
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search