Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Click in Coordinator Layout not working

I have place a coordinater layout inside a relative layout. The coordinator layout consists of two button. I designed the usual onclick function but the button does not seem to be clicked.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayoutMain"
    android:layout_margin="0dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.shaby.payshare.WorkPageOneFragment">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/add1"
            android:layout_gravity="bottom|right"
            android:text="ADDBene"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/add2"
            android:layout_gravity="bottom|left"
            android:text="AddItem"/>
    </android.support.design.widget.CoordinatorLayout>



</RelativeLayout>

Java File part as

buttonAddB= (Button)view.findViewById(R.id.add1);


        buttonAddB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show();


            }
        });

The question is 1) Am I doing the correct thing? 2) If yes then why is the button not working? Help.

like image 688
Shubham Agarwal Bhewanewala Avatar asked Nov 09 '22 01:11

Shubham Agarwal Bhewanewala


1 Answers

Two ideas:

1) Check to see what view is in buttonAddB= (Button)view.findViewById(R.id.add1) and that it refers to what you think it refers to;

2) You don't say what happens when things fail, so I am guessing it's a NPE. Try changing Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show() to Toast.makeText(v.getContext(), "Hi", Toast.LENGTH_LONG).show() to see if that works. getContext() may be returning null.

If these don't fix it, post a little more information about the failure.

like image 185
Cheticamp Avatar answered Nov 15 '22 08:11

Cheticamp