-1

Why doesn't my button show toast message and writes to the log. The button is inside the fragment?

Code inside my MainActivity.java

 public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

Code inside my FragmentA.java:

    public class FragmentA extends Fragment implements   View.OnClickListener{

    Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = (Button)getActivity().findViewById(R.id.button);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getActivity(), "Do it", Toast.LENGTH_LONG).show();
        Log.w("myApp","Hi there");
    }
}

Code inside fragment_a.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFBB00"
    >


    <Button
        android:layout_margin="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        />

</LinearLayout>

Code inside activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00BBFF"
    >


    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.audiorecbas.audio_recorder_basic.FragmentA"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
Seba
  • 47
  • 2
  • 2
  • 8

4 Answers4

2
  @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = (Button)getActivity().findViewById(R.id.button);
        button.setOnClickListener(this);
    }
Anitha Manikandan
  • 1,160
  • 7
  • 19
1

In your Fragment Java class, you need to have the following to handle the Button's click event: Like this way

    public class FragmentA extends Fragment implements   View.OnClickListener
    {
           Button btn;

         @Override
         public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);

                btn = (Button) getActivity().findViewById(R.id.button);
                btn.setOnClickListener(this);       

            ......    
            }

For better information please check SO ANSWER

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You haven't linked the button's onClickListener after instantiating it.

button = (Button) getActivity.findViewById(R.id.button);
button.setOnClickListener(this); //add this
0

Remove this entire code

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    button = (Button)getActivity().findViewById(R.id.button);

}

then add

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);
    button= (Button) view.findViewById(R.id.button);
    button.setOnClickListener(this);
    return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            {
             //Do your stuff here
            }
         }
      }
Rishad Appat
  • 1,786
  • 1
  • 15
  • 30
  • why should I do it like that. It already works perfect in onActivityCreated() method ? – Seba Aug 06 '15 at 11:59