Implement Callback by clicking on a button of a module in Android

0

I have a custom module of Android NativeBannerView intent that when clicking on a button delegate the assigned action of the project, I do not master the topic of interfaces, callbacks

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/smart_banner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:minHeight="@dimen/banner_height">

    <ImageView
        android:id="@+id/banner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:contentDescription="@string/banner_content"
        android:foreground="@drawable/ripple"
        android:src="@android:color/darker_gray" />

    <Button
        android:id="@+id/btn_close"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/ic_cancel_dark" />

</RelativeLayout>

Inside the module I detect the click:

Button btnCloseBanner = (Button) findViewById(R.id.btn_close);
if (btnCloseBanner != null) {
    btnCloseBanner.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           //delegar funcionalidad asignada desde el proyecto
        }
    });
}

In the project

myBanner = (NativeBannerView) findViewById(R.id.banner_view);

My idea would be to intercept the click of the button that is inside the module, and launch%% of the project%.

    
asked by Webserveis 14.06.2017 в 18:27
source

1 answer

1

As you mentioned you can create an interface to pass the data between different classes, fragments, views .. I leave you the link of the documentation below that describe it quite well. Declare an interface is quite simple, in the class you want to use, in your case "NativeBannerView" you declare an interface, for example ...

 public interface myListener {

 }

Within the interface we have to define the methods that it will have. For example, in your case you need a method for when you click on the button, then we declare a method that we will call when the button is clicked.

We add:

 public interface myListener {
     void onBotonPulsado();
 }

Now at the beginning of our class, we define a new variable of type myListener.

myListener listener;

We add a method to set the listener,

 public void setListener(myListener listener){
     this.listener = listener;
 }

Once the listener is added, we can call it from the part of the code that we want, in your case when the button is pressed.

Button btnCloseBanner = (Button) findViewById(R.id.btn_close);
if (btnCloseBanner != null) {
    btnCloseBanner.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           //delegar funcionalidad asignada desde el proyecto
           listener.onBotonPulsado();
        }
    });
}

Now we just have to add the listener in the main class, or where we have the "NativeBannerView", something like ...

myBanner = (NativeBannerView) findViewById(R.id.banner_view);
myBanner.setListener(new NativeBannerView.myListener(){
   public void onBotonPulsado(){
      // Y aqui puedes ya hacer lo que quieras
   }
});

Link to the documentation:

link

    
answered by 14.06.2017 / 19:58
source