title: How to switch between fragments from button click in android?
body: <p>I am creating an Android app. It contains two activities. One for login and the other for registration. Both of them are in separate tabs and I can switch between them by swiping.  </p>
  
  <p>But there is a button on bottom of both tabs, in case of login is necessary. I need to move to registration tab when the button is clicked, and also another button to return to login tab.  </p>
  
  <p>How can I implement it using onclick listener event? </p>
  
  <pre><code>public static class PlaceholderFragment extends Fragment {
  
      /**
       * The fragment argument representing the section number for this
       * fragment.
       */
      private static final String ARG_SECTION_NUMBER = "section_number";
  
      public PlaceholderFragment() {
      }
      private static final String lTAG = RegisterActivity.class.getSimpleName();
      private Button btnLogin;
      private Button btnLinkToRegister;
      private EditText linputEmail;
      private EditText linputPassword;
      private ProgressDialog lpDialog;
      private SessionManager lsession;
      private SQLiteHandler ldb;
      private static final String rTAG = RegisterActivity.class.getSimpleName();
      private Button btnRegister;
      private Button btnLinkToLogin;
      private EditText rinputFullName;
      private EditText rinputEmail;
      private EditText rinputPassword;
      private ProgressDialog rpDialog;
      private SessionManager rsession;
      private SQLiteHandler rdb;
       public static Context context;
  
      /**
       * Returns a new instance of this fragment for the given section
       * number.
       */
      public static PlaceholderFragment newInstance(int sectionNumber) {
          PlaceholderFragment fragment = new PlaceholderFragment();
          Bundle args = new Bundle();
          args.putInt(ARG_SECTION_NUMBER, sectionNumber);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          if(getArguments().getInt(ARG_SECTION_NUMBER)==1) {
  
              View rootView = inflater.inflate(R.layout.activity_login, container, false);
              linputEmail = (EditText) rootView.findViewById(R.id.email);
              linputPassword = (EditText) rootView.findViewById(R.id.password);
              btnLogin = (Button) rootView.findViewById(R.id.btnLogin);
              btnLinkToRegister = (Button) rootView.findViewById(R.id.btnLinkToRegisterScreen);
  
              // Progress dialog
              lpDialog = new ProgressDialog(getActivity());
              lpDialog.setCancelable(false);
  
              // SQLite database handler
               context=getContext();
              ldb = new SQLiteHandler(context);
  
              // Session manager
              lsession = new SessionManager(context);
  
              // Check if user is already logged in or not
              if (lsession.isLoggedIn()) {
                  // User is already logged in. Take him to main activity
                  Intent intent = new Intent(context, MainActivity.class);
                  startActivity(intent);
                  getActivity().finish();
              }
  
              // Login button Click Event
              btnLogin.setOnClickListener(new View.OnClickListener() {
  
                  public void onClick(View view) {
                      String email = linputEmail.getText().toString().trim();
                      String password = linputPassword.getText().toString().trim();
  
  
                      // Check for empty data in the form
                      if (!email.isEmpty() &amp;&amp; !password.isEmpty()) {
                          // login user
                          checkLogin(email, password);
                      } else {
                          // Prompt user to enter credentials
                          Toast.makeText(context,
                                  "Please enter the credentials!", Toast.LENGTH_LONG)
                                  .show();
                      }
                  }
  
              });
  
              // Link to Register Screen
              btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  
                  public void onClick(View view) {
  
                      Intent i = new Intent(context,
                              RegisterActivity.class);
                      startActivity(i);
                      getActivity().finish();
                  }
              });
              return  rootView;
          }
  
          else if (getArguments().getInt(ARG_SECTION_NUMBER)==2){
  
              View rootView = inflater.inflate(R.layout.activity_register, container, false);
              rinputFullName = (EditText) rootView.findViewById(R.id.name);
              rinputEmail = (EditText) rootView.findViewById(R.id.email);
              rinputPassword = (EditText) rootView.findViewById(R.id.password);
              btnRegister = (Button) rootView.findViewById(R.id.btnRegister);
              btnLinkToLogin = (Button) rootView.findViewById(R.id.btnLinkToLoginScreen);
  
              // Progress dialog
              rpDialog = new ProgressDialog(getActivity());
              rpDialog.setCancelable(false);
  
              // Session manager
              context=getContext();
              rsession = new SessionManager(context);
  
              // SQLite database handler
              rdb = new SQLiteHandler(context);
  
              // Check if user is already logged in or not
              if (rsession.isLoggedIn()) {
                  // User is already logged in. Take him to main activity
                  Intent intent = new Intent(context,
                          MainActivity.class);
                  startActivity(intent);
                  getActivity().finish();
              }
  
              // Register Button Click event
              btnRegister.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View view) {
                      String name = rinputFullName.getText().toString().trim();
                      String email = rinputEmail.getText().toString().trim();
                      String password = rinputPassword.getText().toString().trim();
  
              if (!name.isEmpty() &amp;&amp; !email.isEmpty() &amp;&amp; !password.isEmpty()) {
                          if(isEmailValid(email)) {
                              registerUser(name, email, password);
                          }else{
                              Toast.makeText(context,
                                      "Please enter valid email!", Toast.LENGTH_LONG)
                                      .show();
                          }
                      } else {
                          Toast.makeText(context,
                                  "Please enter your details!", Toast.LENGTH_LONG)
                                  .show();
                      }
                  }
              });
  
              // Link to Login Screen
              btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
  
                  public void onClick(View view) {
                      Intent i = new Intent(context,
                              LoginActivity.class);
                      startActivity(i);
                      getActivity().finish();
                  }
              });
  
              return  rootView;
  
          }
          return null;
      }
      ``
  /**
   * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
   * one of the sections/tabs/pages.
   */
  public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          // getItem is called to instantiate the fragment for the given page.
          // Return a PlaceholderFragment (defined as a static inner class below).
          return PlaceholderFragment.newInstance(position + 1);
      }
  
      @Override
      public int getCount() {
          // Show 3 total pages.
          return 2;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          switch (position) {
              case 0:
                  return "LOGIN";
              case 1:
                  return "REGISTER";
  
          }
          return null;
      }
  }
  </code></pre>