Search code examples
androidandroid-fragmentsonclicklistenerandroid-tablayout

Implementing onClickListener in fragments


I am updating an older Android application from using the no longer supported ViewPager and deprecated ActionBar to supported classes. I am having a difficult time implementing OnClickListeners in my fragments and creating views. Previously public class myFragment would extend Fragment and implement OnClickListener.

Below is what I have so far.

public class myFragment extends Fragment {
Button btnOne;

@Override
public void onCreate(Bundle savedDataEntryInstanceState) {
    super.onCreate(savedDataEntryInstanceState);
}

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

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    fetchButtonClicks();
}

private void fetchButtonClicks(){
    View view = inflater.inflate(R.layout.my_fragment, container, false);

   btnOne = (Button)view.findViewById(R.id.btnonexml);
   btnOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "BtnOne click event occurred.", Toast.LENGTH_LONG).show();
        }
    });
}

Problem is in implementing the view in fetchButtonClicks(). Two questions: one, what am I missing here w/regards to view creation, and two, is there a better approach to structuring this? I have a dozen or so buttons in this fragment. Thanks in advance.

Update: Below code resolved my problem allowing OnClickListeners in Fragments. This is the structure I am going with based on responses. Seems to expand nicely to my many buttons.

public class dataEdit extends Fragment {
Button btnUpdateRecord, btnDeleteRecord, btnResetForm, btnFirstRecord, btnPreviousRecord,btnNextRecord, btnLastRecord, btnHelp, btnEditComment;
private Button btnSaveRecord = null; //Make above buttons private and equal null???
private View view = null;
private View v = null;


@Override
public void onCreate(Bundle savedDataEntryInstanceState) {
    super.onCreate(savedDataEntryInstanceState);
    setRetainInstance(true);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.data_edit, container, false);
    btnSaveRecord = (Button) view.findViewById(R.id.btnSaveRecordxml);
    btnUpdateRecord = (Button)view.findViewById(R.id.btnUpdateRecordxml);
    btnDeleteRecord = (Button)view.findViewById(R.id.btnDeleteRecordxml);
    btnResetForm = (Button)view.findViewById(R.id.btnResetFormxml);
    btnFirstRecord = (Button)view.findViewById(R.id.btnFirstRecordxml);
    btnPreviousRecord = (Button)view.findViewById(R.id.btnPreviousRecordxml);
    btnNextRecord = (Button)view.findViewById(R.id.btnNextRecordxml);
    btnLastRecord = (Button)view.findViewById(R.id.btnLastRecordxml);
    btnHelp = (Button)view.findViewById(R.id.btnDataEditHelpFilexml);
    btnEditComment = (Button)view.findViewById(R.id.btnEditCommentxml);

    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    fetchButtonClicks();
} 

private void fetchButtonClicks(){
    btnSaveRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveRecord(v);
        }
    });
    btnUpdateRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateRecord(v);
        }
    });
    btnDeleteRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteRecord(v);
        }
    });
    btnResetForm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetForm();
        }
    });
    btnFirstRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            firstRecord(v);
        }
    });
    btnPreviousRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            previousRecord(v);
        }
    });
    btnNextRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nextRecord(v);
        }
    });
    btnLastRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lastRecord(v);
        }
    });
    btnHelp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showHelpDialog();
        }
    });
    btnEditComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showCommentEditDialog();
        }
    });
} 

It's a work in progress at this point: additional suggestions welcome. Thanks.


Solution

  • You don't need to Inflate The view two time just Pass the View into your fetchButtonClicks

    private void fetchButtonClicks(View view){
    
       btnOne = (Button)view.findViewById(R.id.btnonexml);
       btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "BtnOne click event occurred.", Toast.LENGTH_LONG).show();
            }
        });
    }
    

    and on your onViewCreated() method just call it like this :

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fetchButtonClicks(view);
    }