32

I have a FragmentActivity where I add a Fragment. Depending on some situation, I want to access Buttons from that fragment's layout and change click listeners of them.

What I do is:

View view = myFragment.getView();
Button myButton = (Button) view.findViewById(R.id.my_button);
myButton.setOnClickListener(new MyClickListener());

But the view variable is always null.

How can I get the view of the activity?

Drastamat
  • 343
  • 1
  • 3
  • 6

5 Answers5

34

If you want to access your component and set some data, I suggest you to make a method inside the fragment like this:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class DetailFragment extends Fragment {

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


    public void setSettings(){
        Button button = (Button) getView().findViewById(R.id.my_button);
        button.setOnClickListener(new MyClickListener());
    }
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Jarvis
  • 1,527
  • 12
  • 17
  • 4
    getView() might return null and it does so in many cases. If I call setSettings from Activity's OnCreate method the exception will be thrown – Varvara Kalinina Jan 13 '16 at 13:52
  • @VarvaraKalinina So `getView ` only works from inside a fragment and not from an activity? – John Aug 07 '19 at 06:57
  • Just tested it with an activity, there is no such method as `getView` in an activity ([doc link](https://developer.android.com/reference/android/app/Activity)), and you need to keep a member variable for the xml root view, not really ideal to keep a reference to the parent element. [doc link](https://developer.android.com/reference/android/app/Fragment) for fragment which shows `getView` method – John Aug 07 '19 at 07:21
  • @John In case of Activity you can use findViewById(android.R.id.content) – Varvara Kalinina Aug 07 '19 at 12:24
19

try this:

public class YourFragment extends Fragment {

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

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

        View view = getView();
        if(view != null) {
            view.findViewById(R.id.my_button).setOnClickListener(new MyClickListener());
        }
    }
}

this code must works fine. I hope, this will help you.

Volodymyr Yatsykiv
  • 3,181
  • 1
  • 24
  • 28
4

To set OnClickListeners in fragments, override onViewCreated in your fragment class.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.findViewById(R.id.yourId).setOnClickListener(this);
    //or
    getActivity().findViewById(R.id.yourId).setOnClickListener(this);
}

I was trying to set a timertask to automatically reload a webview in a fragment and this method was the only thing that worked for me. My project is on github.

Source: this answer

Community
  • 1
  • 1
JB0x2D1
  • 838
  • 8
  • 20
1

When you initialize your button inside a class that extends Fragment it's better to do it inside the onViewCreated() method. This will guarantee that the view already exists.

Mokus
  • 3,339
  • 1
  • 24
  • 30
Alex P
  • 131
  • 1
  • 6
0

Simply Use the getRootView() method to get the data of your fragment in Activity.

//below block of code write in activity.

EditText et = (EditText) v.getRootView().findViewById(R.id.name);
Log.d("Name", et.getText().toString());

name is the field of fragment.

Aman Gupta - ΔMΔN
  • 2,971
  • 2
  • 19
  • 39