Skip to content Skip to sidebar Skip to footer

Android - Pass "edit Text" Element Value Between Tabbed Fragments

I have an activity with multiple swipe tabbed fragments. Each fragment has some check-boxes, edit text and switch fields. What I want is to navigate through all the tabs, edit the

Solution 1:

You can store the values in your activity and use a callback to update and query them.

Try this:

On your activity:

publicclassIconTextTabsActivityextendsAppCompatActivityimplementsActivityCallback{
    privateString mBreedTextName = "";
    ...
    @OverridepublicvoidonEditTextChange(String text) {
        this.mBreedTextName = text;
    }

    @OverridepublicStringgetEditTextName() {
        return mBreedTextName;
    }
}

The ActivityCallback interface:

publicinterfaceActivityCallback {

    voidonEditTextChange(String text);

    String getEditTextName();
}

then on your fragment:

publicclassOneFragmentextendsFragment {
    ...
    private ActivityCallback callback;    

    @OverridepublicvoidonAttach(Context context) {
        super.onAttach(context);
        callback = (ActivityCallback) context;
    }

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

    // Inflate the layout for this fragmentViewv= inflater.inflate(R.layout.fragment_one, container, false);
    mBreedName = (EditText) v.findViewById(R.id.addName);
    Buttonb= v.findViewById(R.id.accept_button);

    b.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
                callback.onEditTextChange(mBreedName.getText().toString());
            }
        });
        return v;
    }

}

And the same on the second fragment:

publicclassTwoFragmentextendsFragment {

    ...
    privateActivityCallback callback;    
    privateString text;
    @OverridepublicvoidonAttach(Context context) {
        super.onAttach(context);
        callback = (ActivityCallback) context;
    }
    @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragmentView v = inflater.inflate(R.layout.fragment_two, container, false);
        text = callback.getEditTextName();
        return v;
    }
}

Post a Comment for "Android - Pass "edit Text" Element Value Between Tabbed Fragments"