Search code examples
androidandroid-fragmentsonclicklistener

Unable to use onClickListener inside fragment


I am trying to fire an OnclickListener inside the fragment to perform some task in the parent activity. But for some reason, the code inside the listener method has no effect. Neither toast nor Log is displaying. I am not quite sure how to attach the listener to the button so when implementing the View.OnclickListener interface so maybe I missed something there. Please help.

Parent Activity:

package app2.tarun.com.fragments2;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AdminPanel extends AppCompatActivity implements 
FragmentChangePassword.OnButtonClickedListener{

private String password, c_password;
EditText et_password, et_c_password;
DataBaseHelper db;
Button btn_change_password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.admin_panel);
    btn_change_password = findViewById (R.id.btn_changePassword);
    et_password =  findViewById (R.id.admin_input_password);
    et_c_password =  findViewById (R.id.admin_input_reEnterPassword);
    db=  new DataBaseHelper (this);

}


public void changeFragment(View view) {
    Fragment fragment;

    if (view == findViewById (R.id.register)) {
        fragment = new FragmentRegisterUser ();
        FragmentManager fm = getSupportFragmentManager ();
        FragmentTransaction ft = fm.beginTransaction ();
        ft.replace (R.id.fragment, fragment);
        ft.commit ();
    }

    if (view == findViewById (R.id.change_password)) {

        Log.e("mmmmmmmmmm","ssssssssss");
        Toast.makeText (this, "Hello", Toast.LENGTH_LONG).show ();

        fragment = new FragmentChangePassword ();
        FragmentManager fm = getSupportFragmentManager ();
        FragmentTransaction ft = fm.beginTransaction ();
        ft.replace (R.id.fragment, fragment);
        ft.commit ();
    }


}



@Override
public void onButtonClicked() {
    Log.e("mmmmmmmmmm","ssssssssss");
    Toast.makeText (this, "vdsfds", Toast.LENGTH_LONG).show ();
    password = et_password.getText ().toString ();
    c_password = et_c_password.getText ().toString ();
    if (password.equals (c_password)) {
        db.changePassword (password);
        finish ();
    } else
        Toast.makeText (this, "vdsfds", Toast.LENGTH_LONG).show ();
}

}

Fragment Activity:

package app2.tarun.com.fragments2;


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

public class FragmentChangePassword extends Fragment implements View.OnClickListener {



OnButtonClickedListener mCallback;
Button btn_change_password;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_fragment_change_password, container, false);

}

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

    mCallback = (OnButtonClickedListener) getActivity ();
    btn_change_password = getActivity ().findViewById (R.id.btn_changePassword);


}

@Override
public void onClick(View view) {
    Log.e("mmmmmmmmmm","ssssssssss");
    if(view.getId ()==R.id.btn_changePassword) {
        mCallback.onButtonClicked ();
    }
}


public interface OnButtonClickedListener {
    void onButtonClicked();
}

}


Solution

  • I am not quite sure how to attach the listener to the button so when implementing the View.OnclickListener interface so maybe I missed something there

    When you're implementing the View.OnclickListener on the Fragment, you also need to set the listener to the View.

    You need to do something like this:

    btn_change_password.setOnClickListener(this);
    

    this is referring the the Fragment which is already implementing the View.OnclickListener

    In case of your Activity, I suspect that you're using android:onClick in the XML for the Activity. So, when you have something like this in your XML:

    <Button
        ...
        android:onClick="changeFragment">
    
    </Button>
    

    Your click will work because you have the following method in your Activity:

    public void changeFragment(View view) {
      ...
    }