Home / Tutorials / How to make custom listener using interface

How to make custom listener using interface

We are familiar with android listeners… Listeners are used to listen user’s interaction..

As like when a user click a button we use “onClikListener” to listen the button click and do things which are depends on the button click..

Some times we need to capture other components as like visibility change of a view .. We will now see a demo tutorial how to listen visibility change of a linear layout using interface term..

Follow those steps to achieve that

=> create a class which extend LinearLayout class and add all of the constructor of the super class

=> create an inner  interface named OnVisibilityChangedListener on that class ..Create a instance of that interface and define a setVisibilityListener method

=> override “onVisibilityChanged” method and pass the visibility change through the instance of the interface

here is the complete code

 

Now in xml layout file instead of Linearlayout use the custom layout in which view you want to listen visibility chnage.. Here is a example code… I want to hear adview visibility listener

 

Now In the activity file we need to connect the layout file with our customlayout instance..

now if we want to capture visibilty of that view just need to call that listener

Now it will automatically listen the visibility change of that specific view

In the same way you can create any custom listener for your code purpose..

About Rana

Rana
Love to do fun with programming and build something new.. :)

Check Also

android-push-notification-tutorial

How to add push notification in android application from android studio – Android developer (part – 1 Connect with firebase )

Assalamualaikum, Here is Rana to tech you how to add push notification in android app …

Leave a Reply

Your email address will not be published. Required fields are marked *