askIT

Android Set new layout in fragment

Create a view variable inside your class.

public class CategoriesFragment extends Fragment {

    /*- Class Variables ---------------------------------------------------------------*/
    private View mainView;

Create On create view method.

    /*- On create view ------------------------------------------------------------- */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mainView = inflater.inflate(R.layout.fragment_categories, container, false);
        return mainView;
    }

Create set main view method.

    private void setMainView(int id){
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mainView = inflater.inflate(R.layout.id, null);
        ViewGroup rootView = (ViewGroup) getView();
        rootView.removeAllViews();
        rootView.addView(mainView);
    }

To change view simply use setMainView.
Example:

/* Change layout */
int newViewID = R.layout.fragment_categories_add_edit;

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.