0

I have a custom widget thats extends Linear layout. It just block of text with litle.

Here is xml of widget:

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:background="@drawable/actionbar_background"
        android:layout_height="35dp"
        android:paddingLeft="15dp"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/title"
            android:layout_height="35dp"
            android:textColor="@color/white"
            android:layout_width="match_parent"
            android:textSize="18dp"
            android:clickable="true"
            android:gravity="center_vertical" />
    </LinearLayout>

    <TextView
        android:id="@+id/ingreds"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Some text"
        android:paddingLeft="15dp" />
</LinearLayout>

If I creating onClickListener for that custom widget somewhere in Activity - it reacts only if I click on last TextView in that layout. But I need to set onClickListener for horizontal LinearLayout which contains "title" textView, or directly on title TextView. Important: I want to set these listeners OUTSIDE the custom widget class.

I think that I need to override setOnclickListener method in my custom class or something like that, but I'm not sure how it can be done.

Here is the class of custom widget:

public class MyTextBlock extends LinearLayout {
    private TextView title;
    private TextView ingreds;

    public MyTextBlock(Context context) {
        this(context, null);
    }

    public MyTextBlock(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.my_text_block, this, true);

        title = (TextView) findViewById(R.id.title);
        ingreds = (TextView) findViewById(R.id.ingreds);
    }

    public void setText(String text) {
        ingreds.setText(text);
    }

    public void setTitle(String titleText) {
        title.setText(titleText);
    }
}

Thanks for answering.

reVerse
  • 35,075
  • 22
  • 89
  • 84
udenfox
  • 1,594
  • 1
  • 15
  • 25
  • So I solve that problem by creating custom event listener. http://stackoverflow.com/questions/8292712/android-custom-event-listener/8293106#8293106 – udenfox Sep 04 '14 at 13:24

2 Answers2

0

Just implement the OnClickListener to this view class and set textview.setOnClickLister(this);

now handle the click event under the onClick(View v) method.

jitain sharma
  • 584
  • 5
  • 19
  • Can You provide me example code, please? I have never created a customized widgets earlier, so I not fully understood You. Thanks. – udenfox Sep 03 '14 at 16:37
  • Okay, i did it. But this is not what I wanted to do. I can handle clicking on textview INSIDE the view class, but I need to handle it OUTSIDE. For example: I creating new MyTextBlock object in MainActivity. So I need to handle clicking on the textView (which is inside custom View class) from MainActivity. – udenfox Sep 04 '14 at 13:06
  • Okay, than just pass the onClickListener from your main activity class while initializing the custom view and set it to the textview, now the clicks over the textview will be catch under the onClick(View v) method of your main activity class. – jitain sharma Sep 04 '14 at 15:40
  • 1
    Thank You for solution. Thats work. But I did it in other way, by creating a custom listener for widget. This was necessary because the use of the widget changed a little. But Thanks anyway! – udenfox Sep 05 '14 at 16:03
0
MyTextBlock textviewCustom = ....
textviewCustom.setonclickListner(MainClass.this); 


public class MyTextBlock extends LinearLayout {
private TextView title;
private TextView ingreds;

public MyTextBlock(Context context) {
    this(context, null);
}


public MyTextBlock(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.my_text_block, this, true);

    title = (TextView) findViewById(R.id.title);
    ingreds = (TextView) findViewById(R.id.ingreds);
}


public void setonclickListner(OnclickListner listner)
{
 title.setonclicklistner(listner);
 ingreds.setonclicklistner(listner);
}

public void setText(String text) {
    ingreds.setText(text);
}

public void setTitle(String titleText) {
    title.setText(titleText);
}
}
  • That not seems to work. OnClick still handle clicking on ingreds TextView, not on title (in SetOnclickListener method I removed "ingreds.setonclicklistner(listner);" line) – udenfox Sep 03 '14 at 16:38