title: How do I place a login screen inside a fragment?
body: <p>I am trying to build a an android application where there is a navigation drawer with 5 different options in it. 
  For the first option, I am trying to implement a login screen. However, I can't get the application to switch to the register screen or vice versa.</p>
  
  <p>Here are my codes:</p>
  
  <p>This is the Login Activity:</p>
  
  <pre><code>public class LoginActivity extends MainActivity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // setting default screen to login.xml
          setContentView(R.layout.login);
  
          TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
  
          // Listening to register new account link
          registerScreen.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // Switching to Register screen
                  Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                  startActivity(i);
              }
          });
      }
  }
  </code></pre>
  
  <p>This is the Register Activity:</p>
  
  <pre><code>public class RegisterActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // Set View to register.xml
          setContentView(R.layout.register);
  
          TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
  
          // Listening to Login Screen link
          loginScreen.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View arg0) {
                  // Closing registration screen
                  // Switching to Login Screen/closing register screen
                  finish();
              }
          });
      }
  }
  </code></pre>
  
  <p>This is the Login Fragment(where i want to implement the above 2 activities):</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.TextView;
  
  import com.a.myapplication.R;
  
  
  public class LoginFragment extends Fragment {
  
      public LoginFragment() {
          // Required empty public constructor
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
  
  
      }
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.login, container, false);
  
  
          // Inflate the layout for this fragment
  
  
          return rootView;
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
      }
  }
  </code></pre>
  
  <p>Here is the register.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="10dip"
      android:layout_below="@id/nav_header_container"&gt;
      &lt;!-- Full Name Label --&gt;
      &lt;TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#372c24"
          android:text="Full Name"/&gt;
      &lt;EditText android:id="@+id/reg_fullname"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:singleLine="true"
          android:layout_marginBottom="20dip"/&gt;
      &lt;!--  Email Label --&gt;
      &lt;TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#372c24"
          android:text="Email"/&gt;
      &lt;EditText android:id="@+id/reg_email"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:singleLine="true"
          android:layout_marginBottom="20dip"/&gt;
      &lt;!-- Password Label --&gt;
      &lt;TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#372c24"
          android:text="Password"/&gt;
      &lt;EditText android:id="@+id/reg_password"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:password="true"
          android:singleLine="true"
          android:layout_marginTop="5dip"/&gt;
      &lt;!-- Register Button --&gt;
      &lt;Button android:id="@+id/btnRegister"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:text="Register New Account"/&gt;
      &lt;!-- Link to Login Screen --&gt;
      &lt;TextView android:id="@+id/link_to_login"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="40dip"
          android:layout_marginBottom="40dip"
          android:text="Already has account! Login here"
          android:gravity="center"
          android:textSize="20dip"
          android:textColor="#025f7c"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is the login.xml:</p>
  
  <pre><code>&lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="10dip"
      android:layout_below="@id/toolbar"&gt;
      &lt;!--  Email Label --&gt;
      &lt;TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#372c24"
          android:text="Email"/&gt;
      &lt;EditText android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:layout_marginBottom="20dip"
          android:singleLine="true"/&gt;
      &lt;!--  Password Label --&gt;
      &lt;TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#372c24"
          android:text="Password"/&gt;
      &lt;EditText android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:singleLine="true"
          android:password="true"/&gt;
      &lt;!-- Login button --&gt;
      &lt;Button android:id="@+id/btnLogin"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:text="Login"/&gt;
      &lt;!-- Link to Registration Screen --&gt;
      &lt;TextView android:id="@+id/link_to_register"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="40dip"
          android:layout_marginBottom="40dip"
          android:text="Register here"
          android:gravity="center"
          android:textSize="20dip"
          android:textColor="#0b84aa"/&gt;
  
  &lt;/LinearLayout&gt;
      &lt;!-- Login Form Ends --&gt;
  </code></pre>
  
  <p>Here is my MainActivity.java:</p>
  
  <pre><code>    public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
  
      private static String TAG = MainActivity.class.getSimpleName();
  
      private Toolbar mToolbar;
      private FragmentDrawer drawerFragment;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mToolbar = (Toolbar) findViewById(R.id.toolbar);
  
          setSupportActionBar(mToolbar);
          getSupportActionBar().setDisplayShowHomeEnabled(true);
  
          drawerFragment = (FragmentDrawer)
                  getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
          drawerFragment.setUp(R.id.fragment_navigation_drawer,
                  (DrawerLayout) findViewById(R.id.drawer_layout),
                  mToolbar);
          drawerFragment.setDrawerListener(this);
  
  
          // display the first navigation drawer view on app launch
          displayView(0);
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          if(id == R.id.action_search){
              Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void onDrawerItemSelected(View view, int position) {
          displayView(position);
      }
  
      private void displayView(int position) {
          Fragment fragment = null;
          String title = getString(R.string.app_name);
          switch (position) {
              case 0:
                  fragment = new LoginFragment();
  /*
  I am trying to call this fragment, that includes the two activities
  */
                  title = getString(R.string.login);
                  break;
              case 1:
                  fragment = new CalendarFragment();
                  title = getString(R.string.title_calendar);
                  break;
              case 2:
                  fragment = new GalleryFragment();
                  title = getString(R.string.title_gallery);
                  break;
              case 3:
                  fragment = new BankFragment();
                  title = getString(R.string.title_bank);
                  break;
              case 4:
                  fragment = new SettingsFragment();
                  title = getString(R.string.title_settings);
              default:
                  break;
          }
  
          if (fragment != null) {
              FragmentManager fragmentManager = getSupportFragmentManager();
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
              fragmentTransaction.replace(R.id.container_body, fragment);
              fragmentTransaction.commit();
  
              // set the toolbar title
              getSupportActionBar().setTitle(title);
          }
      }
  }
  </code></pre>
  
  <p>There is an additional fragmentdrawer to draw the fragments on the screen.
  Any help Is highly Appreciated.</p>
  
  <p>I tried adding an onClick() event to handle the switch, but this didn't really work for me.</p>