androidandroid-layoutandroid-viewpagerandroid-view

How to access component from another view in android


I have created a custom dialog and inside it i create a view page which both have different layout file. Method to show the dialog is in MainActivity.

I inflate a layout to my adapter class where i populate the content. In this layout there is a button. My question is "How can i access the button from the layout that i inflate in my adapter class from my MainActivity. The reason I want to do this is because I want to dismiss the dialog when the button is clicked.

Thanks.

Here is the code of what i have done so far.

MainActivity:

public void ShowPromoOverlay(){

    promoOverlay = new Dialog(this);
    promoOverlay.requestWindowFeature(Window.FEATURE_NO_TITLE);
    promoOverlay.setContentView(R.layout.fragment_overlay);


    final List<Promotion> pageArr = new ArrayList<>();
    int maxcounter = 5;
    if (promotionList.size() < maxcounter) {
        maxcounter = promotionList.size();
    }
    for (int counter = 0; counter < maxcounter; counter++){
        pageArr.add(promotionList.get(counter));
    }

    TestPagerAdapter adapter = new TestPagerAdapter(this, pageArr);
    cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager 
    pager = promoOverlay.findViewById(R.id.overlayPager);
    pager.setPageMargin(50);
    pager.setAdapter(adapter);

    com.viewpagerindicator.CirclePageIndicator indicator = 
    promoOverlay.findViewById(R.id.overlay_page_indicator);
    indicator.setViewPager(pager);
    indicator.setCurrentItem(0);

    promoOverlay.show();

    View inlcudeLayout = findViewById(R.id.my_custom_dialog_layout);
    ImageView closeBtn = (ImageView) inlcudeLayout.findViewById(R.id.closeBtn);
    closeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            promoOverlya.dismiss();
        }
    });
}

fragment_overlay:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_over"
android:background="@color/bg_orange"
android:layout_width="match_parent"
android:layout_height="match_parent">

<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/overlayPager">

    <include layout="@layout/my_custom_dialog" />

</cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager>

<com.viewpagerindicator.CirclePageIndicator
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/overlay_page_indicator"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="70dp">

</com.viewpagerindicator.CirclePageIndicator>
</RelativeLayout>

my_custom_dialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_custom_dialog_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">

<android.support.v7.widget.CardView
    android:layout_width="310dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:layout_marginTop="40dp"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="15dp">

    <ImageView
        android:id="@+id/closeBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_clear_grey_600_24dp"
        android:layout_marginTop="7dp"
        android:layout_marginRight="7dp"
        android:elevation="5dp"
        android:layout_gravity="right"/>

</android.support.v7.widget.CardView>
</RelativeLayout>

TestPagerAdapter:

public class TestPagerAdapter extends PagerAdapter {

Context context;
LayoutInflater inflater;
List<Promotion> pageArr;

public TestPagerAdapter(Context context, List<Promotion> pageArr){
    this.context = context;
    this.pageArr = pageArr;

    inflater = ((Activity) context).getLayoutInflater();
}

public TestPagerAdapter(Context context){
    this.context = context;

    inflater = ((Activity) context).getLayoutInflater();

}
@Override
public int getCount(){
    return pageArr.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position){
   View view = inflater.inflate(R.layout.my_custom_dialog, container, 
false);
    TextView prTitle = view.findViewById(R.id.title_pr);
    TextView prDescription = view.findViewById(R.id.description_pr);
    TextView readMoreBtn = view.findViewById(R.id.readMore);

    view.setTag(position);
    ((ViewPager) container).addView(view);


    final Promotion promotion = pageArr.get(position);

    prTitle.setText(promotion.getTitle());
    prDescription.setText(promotion.getDescription());

    return view;

}

@Override
public boolean isViewFromObject(View view, Object o) {
    return view == ((View) o);
}

@Override
public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}
}

Solution

  • Nice question. You should have to use the interface. let's try this code.

    public interface OnButtonClickListner {
      public void OnButtonClick();
    }
    

    Now You need to pass this interface in the adapter. like this

    TestPagerAdapter adapter = new TestPagerAdapter(this, pageArr TestPagerAdapter adapter = new TestPagerAdapter(this,new OnButtonClickListner()
    {
    
        @Override
        public void OnButtonClick() {
        // dismiss dialog here
        }
    });
    

    Now, You need to change the constructor of Viewpager adapter. like below:

    public TestPagerAdapter(Context context, List<Promotion> pageArr,OnButtonClickListner listner){
        this.context = context;
        this.pageArr = pageArr;
        this.onButtonClickListner=listner;
        inflater = ((Activity) context).getLayoutInflater();
    }
    

    Now you just need to call this listener method from adapter like onButtonClickListner.OnButtonClick(); and the method of popup will call and you will easily dismiss the dialog. you may also pass the argument if you need to require to pass the position of view page.

    Click event of read more button like below :

    readMoreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onButtonClickListner.OnButtonClick();
            }
        });
    

    Hope this will help you.

    Happy coding.....