Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a multi-step registration form using viewpager and multiple fragments?

I want to create a multi-step registration form in my android app. I wish to use the viewpager with multiple fragments and each fragment being a step in a registration.

At the end, I want to submit all the data submitted in each fragment using an activity to a mysql database. Can I get a small sample code for this?

This is what I have tried so far. I am able to implement a viewpager with multiple fragments. I have created an interface in the StepOne.java Fragment so as to communicate to an activity. But I am not able to retrieve data from these fragments. It throws a null-pointer exception on line 52.

Here is my code: SliderActivity.java:

public class SliderActivity extends AppCompatActivity  {
    ViewPager viewPager;
    EditText edName,edPassword, edConfPass;
    TextView tvShowAll;
    ViewPagerAdapter mPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slider);

        viewPager = findViewById(R.id.view_pager);
        mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void selectIndex(int newIndex) {
        viewPager.setCurrentItem(newIndex);
    }

    @Override
    public void onBackPressed() {
        int currentPosition = viewPager.getCurrentItem();
        if (currentPosition != 0) {
            viewPager.setCurrentItem(viewPager.getCurrentItem()-1);
        } else {
            super.onBackPressed();
        }
    }

    public class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch (position)
            {
                case 0:
                    return new StepOne(); 
                case 1:
                    return new StepTwo(); 
                case 2:
                    return new StepThree();
            }
            return null; 
        }

        @Override
        public int getCount() {
            return 3; //three fragments
        }
    }
}

StepOne.java:

public class StepOne extends Fragment {
    Button buttonInFragment1;
    EditText edName;

    public interface ActivityFragmentCallback {
        void onSetName(String name);
    }
    ActivityFragmentCallback listener;
    public StepOne() {

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

        View rootView = inflater.inflate(R.layout.fragment_step_one, container, false);

        edName = rootView.findViewById(R.id.edName);


        buttonInFragment1 = rootView.findViewById(R.id.button_one);
        if(buttonInFragment1 != null) {
            buttonInFragment1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(),edName.getText().toString(),Toast.LENGTH_SHORT).show();
                    if(edName != null) {
                        listener.onSetName(edName.getText().toString());
                    }

                    switch (view.getId()){
                        case R.id.button_one:

                            ((SliderActivity)getActivity()).selectIndex(1);
                            break;
                    }
                }
            });
        }
        return rootView;
    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        try {
            listener = (ActivityFragmentCallback)context;
        }catch (ClassCastException c){
            c.printStackTrace();
        }
    }
}

MessageActivity.java: Here is where I need all the data submitted from all the fragments

public class MessageActivity extends AppCompatActivity implements StepOne.ActivityFragmentCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
    }
    @Override
    public void onSetName(String name){
//        tvShowAll = findViewById(R.id.tvShowAll);
//        tvShowAll.setText(name);
        Toast.makeText(getApplicationContext(),name, Toast.LENGTH_LONG).show();
    }
}
like image 614
DIVYA RATHOD Avatar asked Jul 22 '19 05:07

DIVYA RATHOD


Video Answer


1 Answers

Below is some reference link for step by step registration; Hope you have help

https://github.com/stepstone-tech/android-material-stepper

https://github.com/baoyachi/StepView?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=3774

like image 182
Haresh Ramani Avatar answered Oct 17 '22 11:10

Haresh Ramani