user3600801
user3600801

Reputation:

onClickListener is not getting fired

Im trying to fire onClick Event from a custom view but onClickListener is not getting fired

@Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    mRootView = inflater.inflate(R.layout.myorders_fragment, container, false);
    linear = (LinearLayout) mRootView.findViewById(R.id.linear);
    radioGroup = (RadioGroup) mRootView.findViewById(R.id.segment_text);
    pending = (RadioButton) mRootView.findViewById(R.id.button_one);
    completed = (RadioButton) mRootView.findViewById(R.id.button_two);

    //  radioGroup.setOnCheckedChangeListener(this);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (group == radioGroup) {
                if (checkedId == R.id.button_one) {

                    for (int i =0 ;i<=3;i++) {
                        mChildView = inflater.inflate(R.layout.pending_orders_fragment, linear);

                       mChildView.setClickable(true);
                        mChildView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(getActivity().getApplicationContext(),"sdf",Toast.LENGTH_SHORT).show();
                            }
                        });


                    }

                } else if (checkedId == R.id.button_two) {

                    Toast.makeText(getActivity().getApplicationContext(),"completed",Toast.LENGTH_SHORT).show();
                 //   mChildView = inflater.inflate(R.layout.pending_orders_fragment, linear);

                }
            }
        }
    });
    return mRootView;
}

child view has to be clickable and it should fire onClickListener. Why is that onClickListener is not getting fired? or should I try implementing onTouchListener?

Upvotes: 0

Views: 126

Answers (2)

Fabio Venturi Pastor
Fabio Venturi Pastor

Reputation: 2529

Try this and tell me if works please:

mChildView
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      public void onFocusChange(View v, boolean hasFocus) {
           if (hasFocus) {
                v.performClick();
             }
         }
  });

Upvotes: 0

Niza Siwale
Niza Siwale

Reputation: 2406

Your childViews do not have a parent, that's why OnClick is not callend. You are only inflating them, but not adding them to a parent. You should call addView on the LinearLayout with the children if they are not there. Otherwise, call getChildAt on the LinearLayout instead of inflating them if you already have them in the LinearLayout

Upvotes: 2

Related Questions