AviranAbady / CookieBar2

Android library for displaying text messages, notifications and alerts at the top or bottom of the screen. A great alternative for toast and snackbar alerts.
Apache License 2.0
668 stars 74 forks source link

Cannot display from layout #27

Closed rashgaroth closed 3 years ago

rashgaroth commented 3 years ago

hey yo! this is my function for show the dialog custom view

  CookieBar.build(getActivity())
                .setCustomView(R.layout.dialog_settings)
                .setCustomViewInitializer(new CookieBar.CustomViewInitializer() {
                    @Override
                    public void initView(View view) {
                        Button logout = (Button) view.findViewById(R.id.logoutButton);

                        View.OnClickListener listener = new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Button button = (Button) v;
//                                Utils.showLongSnackBar(getActivity(), "berhasil");
                                button.setText("checked");
                            }
                        };

                        logout.setOnClickListener(listener);
                    }

                })
                .setAction("Tutup", new OnActionClickListener() {
                    @Override
                    public void onClick() {
                        CookieBar.dismiss(getActivity());
                    }
                })
                .setTitle("Pengaturan Akun")
                .setMessage("Pilih Aksi")
                .setEnableAutoDismiss(false)
                .setSwipeToDismiss(true)
                .setCookiePosition(CookieBar.BOTTOM)
                .show();
AviranAbady commented 3 years ago

What's the issue @rashgaroth?

rashgaroth commented 3 years ago

here is the issue @AviranAbady :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference

AviranAbady commented 3 years ago

@rashgaroth please provide a sample project

rashgaroth commented 3 years ago

@AviranAbady woops sorry mybad hehe

this is my ProfileFragment.java

public class ProfileFragments extends Fragment implements View.OnClickListener {

    private ImageView settings;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_profile_fragments, container, false);
        InitComponents(v);

        settings.setOnClickListener(this);

        return v;
    }

    void InitComponents(View v){
        settings = v.findViewById(R.id.btSetting);
    }

    @Override
    public void onClick(View v) {

        CookieBar.build(getActivity())
                .setCustomView(R.layout.dialog_settings)
                .setCustomViewInitializer(new CookieBar.CustomViewInitializer() {
                    @Override
                    public void initView(View view) {
                        Button logout = view.findViewById(R.id.logoutButton);

                        View.OnClickListener listener = new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Button button = (Button) v;
//                                Utils.showLongSnackBar(getActivity(), "berhasil");
                                button.setText("checked");
                            }
                        };

                        logout.setOnClickListener(listener);
                    }

                })
                .setEnableAutoDismiss(false)
                .setSwipeToDismiss(true)
                .setCookiePosition(CookieBar.BOTTOM)
                .show();
    }
}

and here is my dialog_settings.xml

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

    <Button
        android:id="@+id/logoutButton"
        android:layout_width="290dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_gravity="center"
        android:layout_marginStart="62dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="62dp"
        android:background="@drawable/rounded_outline_button"
        android:gravity="center"
        android:text="@string/logout_akun"
        android:textColor="@color/merah" />

</RelativeLayout>
AviranAbady commented 3 years ago

@rashgaroth you need to create all the other required views, you can keep them hidden if you dont neet them

morgankoh commented 3 years ago

I don't know why, but after figuring out for sometime.

Apparently, if you want to use customView. You'll need to set the rootView in your customView to id. How I figured out was that, the demo app that is provided has no issues with customView.

But, as soon I remove the rootView ID. It just freaks out.

android:id="@+id/cookie"