ride_on_the_NOP_sled
ride_on_the_NOP_sled

Reputation: 135

Resource ID of Button Defined Using Include Directive in Layout XML

I came across an app, whose layout puzzles me.

Assume two layout XML files, such that one includes the other:

activity_button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include layout="@layout/button"/>

</LinearLayout>%       

button.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/button" 
    android:onClick="sendMessage">    
</Button>

What is the resource ID of the button?

I thought one always had to specify ID via android:id attribute, so I am confused why this layout works. I also checked the R.java ID entry and it seems to be empty.

Upvotes: 0

Views: 113

Answers (2)

Michael A
Michael A

Reputation: 5840

android:id

Is not mandatory field for a view in the layout xml file. the only two mandatory fields are the width and height of the view.

Think of situations when you want a layout to simply show some title without any business need to change it through the activity life time. for this need you don't need to obtain this view id, and don't need the overhead of creating id for this view

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191743

What is the resource ID of the button?

Doesn't have one

I thought one always had to specify ID via android:id attribute

Why did you think this? You often leave off this attribute for the root views of most layouts. (See LinearLayout of your question)

You do not need an android:id if you never expect to find the view using an ID. Since the onClick is defined in the XML, there's really no purpose in finding the button.

Upvotes: 1

Related Questions