27

I have a custom view with 2 linear layouts: the first is the view's header and the second is the the details view.

In the custom view, the OnClickListener of the header Linearlayout is already defined: when it fires, it collapses/expandes the second linearlayout.

What I want to do is to add more functionalities to my header's OnClickListener event (i.e.: collapse/expand the second layout and show a Toast).

I can't modify the source code of the custom view. I tried to set a new OnClickListener but it hides the initial event (collapse/expand).

How should I implement this?

The source code of My Custom View:

public class ExpandoLayout extends ViewGroup
{
    /* some declarations */
    private Linearlayout header;
    private linearlayout footer;

    /* some code */
    @override
    protected void onFinishInflate() {
    header= new LinearLayout(context);
    header.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleExpand();
            }
        });
    }
}

What I want to do is to add some code to the already defined OnClickListener event in my activity. Something like that:

public class myActivity extends Activity {
private Linearlayout myCustomView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rsdetail);
    myCustomView= (MyCustomView) findViewById(R.id.expanded);

    myCustomView.getChildAt(0).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(v instanceof LinearLayout)
            {
                v.performClick();

                Toast.makeText(getActivity(), "ExpandoOnClickListener", 2000).show();
            }
        }
    });
}
1
  • 1
    Please provide the code....... Jun 5, 2012 at 9:13

4 Answers 4

88

You can programmatically raise click event on a View to call it's OnClickListener as below:

view.performClick();

Now if you call this on your second layout under first layout's OnClickListener then i hope it should do the magic

6
  • 1
    When I do this. It gives me a 06-05 10:29:59.470: E/AndroidRuntime(7518): java.lang.StackOverflowError
    – Zakaria
    Jun 5, 2012 at 9:30
  • can you post your full logcat exception
    – waqaslam
    Jun 5, 2012 at 9:41
  • are you calling myCustomView.performClick()? If, so you need to call as myCustomView.getChildAt(0).performClick()
    – waqaslam
    Jun 5, 2012 at 9:45
  • 06-05 10:58:10.440: E/AndroidRuntime(8044): FATAL EXCEPTION: main 06-05 10:58:10.440: E/AndroidRuntime(8044): java.lang.StackOverflowError 06-05 10:58:10.440: E/AndroidRuntime(8044): at java.util.HashMap.containsKey(HashMap.java:330) 06-05 10:58:10.440: E/AndroidRuntime(8044): at android.provider.Settings$NameValueCache.getString(Settings.java:599) 06-05 10:58:10.440: E/AndroidRuntime(8044): at android.provider.Settings$System.getString(Settings.java:731) 06-05 10:58:10.440: E/AndroidRuntime(8044): at android.provider.Settings$System.getInt(Settings.java:780)06-05
    – Zakaria
    Jun 5, 2012 at 10:00
  • here it is : expandedCustomer= (ExpandoLayout) viewr.findViewById(R.id.expandedClient); expandedCustomer.getChildAt(0).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(v instanceof LinearLayout) { expandedCustomer.getChildAt(0).performClick(); Toast.makeText(getActivity(), "ExpandoOnClickListener", 2000).show(); } } });
    – Zakaria
    Jun 5, 2012 at 10:58
10

Simple solution would be to get original OnClickListener and then fire it in new one:

final OnClickListener preDefinedListener = myCustomView.getChildAt(0).getOnClickListner();

myCustomView.getChildAt(0).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(v instanceof LinearLayout)
        {
            preDefinedListener.onClick(v); // calls default (defined by MyCustomView)

            Toast.makeText(getActivity(), "ExpandoOnClickListener", 2000).show();
        }
    }
});

Sadly, View does not have getOnClickListner(), but I guess you can use reflection to get it. It is stored in the field mOnClickListener (source).

This is how you can get OnClickListener defined for your layout:

OnClickListener tmpOnClickListener = null;
try {
    Class<View> cls = (Class<View>) Class.forName("android.view.View");
    Field fld = cls.getDeclaredField("mOnClickListener");
    fld.setAccessible(true); // because it is protected

    tmpOnClickListener = (OnClickListener) fld.get(myCustomView.getChildAt(0));

    fld.setAccessible(false); // restore it's original property
} catch (SecurityException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} 

final OnClickListener preDefinedListener = tmpOnClickListener;

if (preDefinedListener != null) {
    myCustomView.getChildAt(0).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View paramView) {
            preDefinedListener.onClick(paramView);

            Toast.makeText(getActivity(), "ExpandoOnClickListener", Toast.LENGTH_LONG).show();
        }
});

I didn't really bother to handle all exception correctly, but it's enough to get the idea. It might look messy, but it's actually just 5 lines of new code to solve your problem.

2
  • Thanks a lot Vladimir :) it works for me fine. I just want to ask you a question: is Reflection expansive in terms of memory and/or processor??
    – Zakaria
    Jun 5, 2012 at 11:10
  • 2
    no problem =) well, getting fields and invoking methods via reflection is more expensive than doing it "normally", but in this case the difference is so minor it won't affect anything. There is a caveat, however - since the field is accessed by name, if its name changes (which probably won't happen) this code will stop working - it won't crash, but NoSuchFieldException will be thrown, and preDefinedListener will end up null, which leaves listener set for layout intact.
    – Vladimir
    Jun 5, 2012 at 11:27
9

Since SDK 15 you can just call method:

view.callOnClick()
0

If you cannot modify the code of CustomView then you have the below option.

  • Extend the CustomView.
  • Override the onClick() method and bind this as a Listener to the first layout.
  • Inside onClick() first call super.onClick() then add your additional functionality below.

This way you are keeping the existing code and adding additional functionality as well.

1
  • 1
    the OnClickListener event is defined as Anonymous Listener. How can I override it??
    – Zakaria
    Jun 5, 2012 at 9:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.