0

Please, give me a hint how to solve this situation:

I have two custom views in my Activity and I'd like to call an Activity method after clicking a button in one of custom views (Y).
I can get the X view from parent Acitivity by findViewById() and call it's public method. But how can I let the parent Acitivy know that the button was pressed?

Do I have to pass Activity reference to view Y to call an Activity method from OnClickListener?

example Thank you!

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70

1 Answers1

2
button = (Button)customviewY.findViewById(R.id.btnClick);

button.setOnClickListener(new OnClick..(){
    onClick(View v)
    {
        textView= (TextView)customviewY.findViewById(R.id.txtView);
        textView.setText("");
    }
});
MAC
  • 15,799
  • 8
  • 54
  • 95
  • Thanks. And what if these changes are more complex? For example saving data to SharedPreferences, executing AsyncTask and so.. I already have method doing all of this in the parent Activity and I thought I could call it somehow. Do I have to duplicate the method so that it could be called from the OnClickListener? – Marcel Bro Apr 20 '12 at 15:20
  • its depend upon either duplicate code or show or hide views if ur app is not complex... or better to use TabView – MAC Apr 20 '12 at 15:23
  • I realized that a really _need_ to call an Activity method (and execute an AsyncTask) so I'v changed the question a bit. The only solution that crossed my mind is passing an Activity referenc to the view with button (however the view is added through XML so I don't call it's constructor anywhere). Should I pass the Activity via some `set` method? – Marcel Bro Apr 22 '12 at 11:06