Make sure for each layout you do the following:
LinearLayout ll1 = (LinearLayout )findViewById(R.id.ll1);
ll1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do stuff here
}
});
Question
I am working on a big UI to show the detail of the user form. I want that if the user clicks on the LinearLayout
containing the phone number TextView
. I want to make a call intent
. I have added the onClick()
attribute to all the LinearLayout
s. But only the first one works while others does not any explanations will be appreciated.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<ImageButton
android:id="@+id/imageButtonMSG1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f8f3c2"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/msg" />
<LinearLayout
android:layout_width="wrap_content"
android:id="@+id/ll1"
android:layout_height="match_parent"
android:clickable="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/imageButtonMSGMobile"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewMobile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Small Text"
android:textColor="#790202"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewCperson1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Cperson1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#E14343" />
</LinearLayout>
<TextView
android:id="@+id/textView201"
android:layout_width="0.1dp"
android:layout_height="40dp"
android:background="#8E6364"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp" >
<TextView
android:id="@+id/textView51"
android:layout_width="fill_parent"
android:layout_height="0.2dp"
android:background="#8E6364"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/ll2"
android:layout_alignParentLeft="true"
android:clickable="true"
android:layout_toLeftOf="@+id/imageButtonMSGHome"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewMobile2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Small Text"
android:duplicateParentState="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#790202" />
<TextView
android:id="@+id/textViewCperson2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Cperson2"
android:duplicateParentState="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#E14343" />
</LinearLayout>
<TextView
android:id="@+id/textView202"
android:layout_width="0.2dp"
android:layout_height="40dp"
android:background="#8E6364"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageButton
android:id="@+id/imageButtonMSG2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f8f3c2"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/msg" />
</RelativeLayout>
LinearLayout
with id ll1 works for onClick()
Event, while the ll2 and rest others does not work. I dont know why this happens.
Solution 2
Make sure for each layout you do the following:
LinearLayout ll1 = (LinearLayout )findViewById(R.id.ll1);
ll1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do stuff here
}
});
OTHER TIPS
It might be easier to simply declare the onClick
property in your XML - this will avoid you calling setOnClickListener
for each view. This can be done like so:
<LinearLayout
...
android:onClick="doSomething" >
Then, in your Java code you will need a public method named doSomething
that accepts a parameter with type View
:
public void doSomething(View view) {
// React to click here
}
Note, you do not have to do anything with the view
parameter if you don't want/need to.