Reputation: 139
I have a custom class which is extended from Relative Layout. In my xml there is linear layout and a button inside it. When i inflated this and try to use with onclick, it doesn't work. But if i don't use a button, click works. This is the code:
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inflated_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/en_ad_purplebutton">
<Button
android:id="@+id/general_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/en_button_positive"
android:textColor="@color/en_white"
android:textSize="20sp"
android:clickable="true"/>
</LinearLayout>
CustomClass:
@Override
public void onFinishInflate() {
init();
}
private void init(){
inf.inflate(R.layout.uc_button_positive, this);
b = (Button)findViewById(R.id.general_button);
}
This is the code i use this custom class
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/finansbank.enpara"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<mypakage.customClass
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view"
android:text="deneme"
android:onClick="go"
android:clickable="true"
android:layout_gravity="center"/>
</LinearLayout>
Upvotes: 2
Views: 1965
Reputation: 633
I also tried it and saw that and it's not work, And found that Need to do an event like this:
layout = (LinearLayout) inflater.inflate(R.layout.your layout,
container, false);
Button b=(Button)layout .findViewById(R.id.view);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
It works great!!
Upvotes: 3