I'm a newbie and i tried to learn the navigation drawer. I want to set a onClick listener on a button in Fragment but when i run the App, the button do nothing.
this is WeightFragment.java: `
public class WeightFragment extends Fragment {
private FragmentWeightBinding binding;
private EditText edtNumber;
private Button addBtn;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
WeightViewModel weightViewModel =
new ViewModelProvider(this).get(WeightViewModel.class);
binding = FragmentWeightBinding.inflate(inflater, container, false);
View root = binding.getRoot();
View view = inflater.inflate(R.layout.fragment_weight, container, false);
addBtn = view.findViewById(R.id.addBtn);
edtNumber = view.findViewById(R.id.edtNumber);
String weight = String.valueOf(edtNumber.getText());
SimpleDateFormat sdf = new SimpleDateFormat("'Date\n'dd-MM-yyyy HH:mm:ss");
String currentDateAndTime = sdf.format(new Date());
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),weight currentDateAndTime, Toast.LENGTH_SHORT).show();
}
});
final TextView textView = binding.txtDate;
weightViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
I've tried with:
public class WeightFragment extends Fragment implements View.OnClickListener
and implement the method but it doesn't work anyway.
CodePudding user response:
It's because you are setting the onClickListener
on a View that you throw away.
Here is the binding (pay attetion to the root
):
binding = FragmentWeightBinding.inflate(inflater, container, false);
View root = binding.getRoot();
but you are setting the listener on a button that is in:
View view = inflater.inflate(R.layout.fragment_weight, container, false);
addBtn = view.findViewById(R.id.addBtn);
addBtn.setOnClickListener(...);
and at the end you return root;
The view that has the onClick listener is there, you create it but in the end it's thrown away.
Just replace view
with binding
and it will work.
public View onCreateView(
@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
) {
WeightViewModel weightViewModel = new ViewModelProvider(this).get(WeightViewModel.class);
binding = FragmentWeightBinding.inflate(inflater, container, false);
View root = binding.getRoot();
addBtn = binding.addBtn;
edtNumber = binding.edtNumber;
String weight = String.valueOf(edtNumber.getText());
SimpleDateFormat sdf = new SimpleDateFormat("'Date\n'dd-MM-yyyy HH:mm:ss");
String currentDateAndTime = sdf.format(new Date());
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),weight currentDateAndTime, Toast.LENGTH_SHORT).show();
}
});
final TextView textView = binding.txtDate;
weightViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
return root;
}