In Android Button btn = (Button) findViewById(R.id.myBtn);

what is btn here? or any other keyword who is using below syntax Keyword x = new findViewById(R.id.something);

1 answer

  • answered 2018-07-11 03:56 Psypher

    btn stores the data of type Button(Button extends View) for a view that is identified by an identifier R.id.myBTn in the xml that is passed in the onCreate method. In another words its a variable of type Button which points to the view of type Button.

    Eg: In a layout file:

    <Button
        android:id="@+id/myBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
    

    In the java code file:

    Button btn = (Button) findViewById(R.id.myBtn);
    

    Using the instance of the button you can perform actions on the button like invoking some code when the button is clicked.

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do something when button click
        }
    });
    

    You would need to start going over some reading material. Better to start from here