Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle visibility changes for a custom android view/widget

Tags:

android

view

I have written a custom view in android. I need to do some processing when visibility of this view is changed. Is there some listener which is called when visibility of a view/widget is changed ?

Edit:

I know how to change the visibility, would like to know if there is a listener which is called when we setVisibility on the view!

like image 410
Jeevan Avatar asked May 08 '13 14:05

Jeevan


2 Answers

Do you want to do that processing within your custom view class? If so, then why not just override the setVisibility() method, call super(), and then do your custom processing?

like image 88
user1676075 Avatar answered Oct 23 '22 11:10

user1676075


I know how to change the visibility, would like to know if there is a listener which is called when we setVisibility on the view!

you have to subclass your view/widget and override setVisibility, and register a interface on which you will recive the notification. For instance:

public class MyView extends View {

  public interface MyListener {
    public void onSetVisibilityCalled();
  }

  public void registerListener(MyListener myListener) {
    this.mListener = myListener;
  }

 public void setVisibility (int visibility) {
   super.setVisibility(visibility);
   if (mListener != null)
     mListener.onSetVisibilityCalled();
 }

}
like image 18
Blackbelt Avatar answered Oct 23 '22 11:10

Blackbelt