Bobby
Bobby

Reputation: 537

OnClickListener Not Working in a Fragment (in onCreateView Method)

I have this fragment code below:

public class Device extends Fragment {

    EditText deviceName;
    EditText numDevice;
    Button submitButton;
    private API api;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.device_fragment_view, container, false);

        deviceName = (EditText) rootView.findViewById(R.id.device_name);
        numDevice = (EditText)
                rootView.findViewById(R.id.num_of_devices);
        submitButton = (Button) rootView.findViewById(R.id.submit_device);

        Retrofit retroFit = new Retrofit.Builder()
                .baseUrl(getString(R.string.base_url))
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        api = retroFit.create(API.class);

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.print("GOT INTO THE CLICK");
            }
        });

        return rootView;
    }
}

For some reason the click listener that I set on my submitButton doesn't seem to be working. I can't really seem to find what I did wrong in the code. Any thoughts?

Upvotes: 0

Views: 288

Answers (3)

magicleon94
magicleon94

Reputation: 5172

Using Log is a better idea (and should work better). As I said in the comments you can quickly try to change
System.out.print("GOT INTO THE CLICK");
to
Log.d("TAGOFTHEAPP","GOT INTO THE CLICK");

It's a good idea to learn about logging in Android, give this link a look:
https://developer.android.com/reference/android/util/Log.html

Upvotes: 2

W4R10CK
W4R10CK

Reputation: 5550

Try using the Toast to detect before creating API:

public class Device extends Fragment {

EditText deviceName;
EditText numDevice;
Button submitButton;
private API api;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.device_fragment_view, container, false);

    deviceName = (EditText) rootView.findViewById(R.id.device_name);
    numDevice = (EditText) rootView.findViewById(R.id.num_of_devices);
    submitButton = (Button) rootView.findViewById(R.id.submit_device);
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(),"GOT INTO THE CLICK",LENGTH_LONG).show();
        }
    });

    Retrofit retroFit = new Retrofit.Builder()
            .baseUrl(getString(R.string.base_url))
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    api = retroFit.create(API.class);

    return rootView;
   }
}

Upvotes: 1

Amey Shirke
Amey Shirke

Reputation: 714

Try to put :

Toast.makeText(getActivity().getApplicationContext(), "You clicked the button", Toast.LENGTH_SHORT).show();

instead of

System.out.print("GOT INTO THE CLICK");

As your code seems alright, you might be missing it in the log.

Upvotes: 3

Related Questions