• Question About an Android Dev Code

    CatheriBurbank Member

    I am recently studying Android development and on one of the books i’m reading, it has a code that looks like this:

    A piece of code from the XML file looks like this

    android:onclick="buttonClicked"
    

    And on the java source file, the method looks like this

    public void buttonClicked(View view){
            //code goes here
    }
    

    The method looks like it is being passed/accepting an object of type View with the name view. But when the button is clicked, i don’t see anything passed by the XML command to the java method. Can someone explain to me what the (View view) actually does? Sorry if I sound noob lol but I don’t want to move past the chapter I’m in without understanding the whole thing

  • ShikhaTan Member

    Here buttonClickedis internally mapped with the Java code.
    When you click on button your buttonClicked(View view) method will be called.

    You can register listener to your button like this as well in your code:

    findViewById(R.id.your_btn).setOnClickListener(this);
    

    Then your class can implement onClickListener interface.
    You need to overrid onClick(View v) for this.

    Now when you click on button your overided
    onClick(View v) method will be called.

  • Abhey Member
    http://developer.android.com/reference/android/widget/Button.html
    
    http://developer.android.com/reference/android/view/View.OnClickListener.html
    

    The View parameter on buttonClicked event passes the view (a Button is a View) that was clicked.

    I suggest that instead of reading a book, which only give us a general view about Android programming, just read the documentation which give us deeper understanding about the features presented in Android API.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.