Reputation: 9548
How do I define an ID to a layout?
I am trying to add an ID to a linearlayout and setting an onclick listener:
XML:
<LinearLayout
android:id="@+id/?????????"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onClick="btnHandler" >
</LinearLayout>
Class:
public class MainActivity extends Activity {
//....
public void btnHandler(View v){
switch(v)
{
case R.id.????? :
}
}
}
Upvotes: 8
Views: 29722
Reputation: 176
This could work..
XML
<LinearLayout
android:id="@+id/mylayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
JAVA
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout myLayout = findViewById(R.id.mylayout);
myLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Click over Layout", Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 0
Reputation: 832
<LinearLayout
android:id="@+id/lineaLayoutId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onClick="btnHandler" >
</LinearLayout>
public class MainActivity extends Activity {
//....
public void btnHandler(View v){
switch(v)
{
case R.id.lineaLayoutId :
break;
}
}
}
Upvotes: 1
Reputation: 133560
Since you have this
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onClick="btnHandler" >
</LinearLayout>
You can do as below
public void btnHandler(View v)
{
switch(v.getId()) // use v.getId()
{
case R.id.linearlayout :
break; // also don't forget the break;
}
}
Edit:
If you have button then you can do as below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearlayout"
android:onClick="clickEvent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="button"
android:onClick="clickEvent"
/>
</LinearLayout>
Then in your activity
public void clickEvent(View v)
{
switch(v.getId())
{
case R.id.linearlayout :
Log.i("......"," linear layout clicked");
break;
case R.id.bt1 :
Log.i("......"," button clicked");
break;
}
}
Upvotes: 8
Reputation: 1107
Instead of
switch(v)
use
switch(v.getId())
and set your id from the xml
android:id="@+id/idValue"
Upvotes: 5
Reputation: 4168
In onCreate() of your Activity:
findViewById(R.id.?????????).setOnClickListener(this);
And have your Activity implement View.OnClickListener.
@Override
public void onClick(View view) {
if(view.getId() == R.id.?????????) {
//your code for on click
}
}
Upvotes: 0
Reputation: 17922
The easiest way would be to just add it in your layout xml:
<LinearLayout
android:id="@+id/myId" <!-- the part where the id is being created -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onClick="btnHandler" >
</LinearLayout>
It can then be referenced from your code via your.package.R.id.myId
.
Upvotes: 2