0

Hi Im posting this after not finding any possible answer here.

I have an invisible LinearLayout LL1 in a fragment that I turn visible when a recylerView data is empty, that LL1 has a button on it. The problem is simply that the button is not clickable ! I tried setting the listener in different ways but still not working. Here's the code below:

Here's the xml file:

 <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.main.MainActivity"
    android:layout_marginTop="3dp">

    <LinearLayout
        android:id="@+id/msg_emty_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="gone"
        android:orientation="vertical">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_empty_box" />
        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Aucune vente n'a été trouvée !"
            android:textSize="15dp"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/greydark"/>
        <Button
            android:id="@+id/btnAddSale"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Créer une vente"
            android:textColor="@color/whiteTextColor"
            android:background="@drawable/centre_button">

        </Button>

    </LinearLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_db"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_sale_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add_db"
        android:layout_margin="20dp"
        android:layout_gravity="bottom|end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccesGreen"
        android:src="@drawable/ic_edit"/>


</android.support.design.widget.CoordinatorLayout>

And here's the java:

   private void generateSaleList(ArrayList<Sale> saleArrayList, View view) {
    if(saleArrayList.isEmpty()){
        getActivity().setTitle("Ventes sur portable");
        msgLayout.setVisibility(View.VISIBLE);
        creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
        creatSaleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
            }
        });
        creatSaleBtn.setClickable(true);

    }else{
        msgLayout.setVisibility(View.GONE);
        recyclerView_db = view.findViewById(R.id.recycler_view_sale_list);
        adapter_db = new SaleAdapter((ArrayList<Sale>) Utils.sortArray(saleArrayList),this,1,getContext(),this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView_db.setLayoutManager(layoutManager);
        recyclerView_db.setAdapter(adapter_db);
        adapter_db.notifyDataSetChanged();
    }

}

Do you guys see what's causing this weird problem ?

noah_abou
  • 99
  • 12

4 Answers4

1

You can try with requestFocusFromTouch()

Call this to try to give focus to a specific view or to one of its descendants.

 msgLayout.setVisibility(View.VISIBLE);
 creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
 creatSaleBtn.requestFocusFromTouch();
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

SwipeRefreshLayout's visibility should be GONE after recyclerview is empty.
of coarse recyclcerview is empty but it is still visible so your click events are caught by SwipeRefreshLayout witch is defined on top of LL1.
EDIT
instead of recyclerview, set swipe_refresh_db visibility to gone cos I doubt recyclerview is a child of swipe_refresh_dd so if you set recyclerview's visibility to gone, swipe_refresh_db is still visible and gets click events.
give it a try

Reza
  • 845
  • 13
  • 18
  • The AddSaleButton and the Fab have different listeners so nope not that, and just to be sure I removed the FAB and still not working. – noah_abou Apr 26 '20 at 15:59
0

Hide your RecyclerView when there's no items (set visibility to gone). The problem is that it's capturing the clicks, since it's placed on top of the button.

0

as u said in comments you not using creatSaleBtn.setClickable(false); anywhere, then you have to hide the recyclerView using recyclerView_db.setVisibility(View.GONE);

why? because it by default fits the full-screen due too match_parent for width and height.

please add recyclerView_db.setVisibility(View.GONE); in the saleArrayList.isEmpty() check

so your new code will be :

if(saleArrayList.isEmpty()){
    recyclerView_db.setVisibility(View.GONE);
    getActivity().setTitle("Ventes sur portable");
    msgLayout.setVisibility(View.VISIBLE);
    creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
    creatSaleBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
        }
    });
    creatSaleBtn.setClickable(true);

}
Moustafa EL-Saghier
  • 1,721
  • 1
  • 13
  • 43