XML : Android replace fragment shows nothing

Currently I have created android apps with viewpager that changes view between 3 fragments. The first fragment (which I'm having problem on) has a sign up button that will navigate user to the signup fragment. I have set the listener to replace the current fragment to signup fragment but it just shows nothing.

the activity_home.xml that contains viewpager

      <android.support.v4.view.ViewPager          android:id="@+id/container"          android:layout_width="match_parent"          android:layout_height="match_parent"          app:layout_behavior="@string/appbar_scrolling_view_behavior"          android:background="#EE9A00"  >        <android.support.v4.view.PagerTitleStrip          android:id="@+id/pager_title_strip"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:layout_gravity="top"          android:background="#EE9A00"          android:textColor="#fff"          android:paddingTop="4dp"          android:paddingBottom="4dp" />          </android.support.v4.view.ViewPager>        <android.support.design.widget.AppBarLayout          android:id="@+id/appbar"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:paddingTop="@dimen/appbar_padding_top"          android:theme="@style/AppTheme.AppBarOverlay">            <android.support.v7.widget.Toolbar              android:id="@+id/toolbar"              android:layout_width="match_parent"              android:layout_height="?attr/actionBarSize"              android:background="#EE9A00"              app:layout_scrollFlags="scroll|enterAlways"              app:popupTheme="@style/AppTheme.PopupOverlay">            </android.support.v7.widget.Toolbar>        </android.support.design.widget.AppBarLayout>          <android.support.design.widget.FloatingActionButton          android:id="@+id/fab"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="end|bottom"          android:layout_margin="@dimen/fab_margin"          android:src="@android:drawable/ic_dialog_email" />    </android.support.design.widget.CoordinatorLayout>    

activity_home's java code

  package com.example.jamesytl.hostel;    import android.support.design.widget.FloatingActionButton;  import android.support.design.widget.Snackbar;  import android.support.v7.app.AppCompatActivity;  import android.support.v7.widget.Toolbar;    import android.support.v4.app.Fragment;  import android.support.v4.app.FragmentManager;  import android.support.v4.app.FragmentPagerAdapter;  import android.support.v4.view.ViewPager;  import android.os.Bundle;  import android.view.LayoutInflater;  import android.view.Menu;  import android.view.MenuItem;  import android.view.View;  import android.view.ViewGroup;    import android.widget.TextView;    import com.parse.Parse;    public class homeActivity extends AppCompatActivity {        /**       * The {@link android.support.v4.view.PagerAdapter} that will provide       * fragments for each of the sections. We use a       * {@link FragmentPagerAdapter} derivative, which will keep every       * loaded fragment in memory. If this becomes too memory intensive, it       * may be best to switch to a       * {@link android.support.v4.app.FragmentStatePagerAdapter}.       */      private SectionsPagerAdapter mSectionsPagerAdapter;        /**       * The {@link ViewPager} that will host the section contents.       */      private ViewPager mViewPager;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_home);            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);          setSupportActionBar(toolbar);          // Parse Connection          Parse.initialize(new Parse.Configuration.Builder(getApplicationContext()).applicationId("hostelid").server("http://ytlhostel.azurewebsites.net").build()          );          // Create the adapter that will return a fragment for each of the three          // primary sections of the activity.          mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());            // Set up the ViewPager with the sections adapter.          mViewPager = (ViewPager) findViewById(R.id.container);          mViewPager.setAdapter(mSectionsPagerAdapter);                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);          fab.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View view) {                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                          .setAction("Action", null).show();                }          });        }          @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_home, 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;          }            return super.onOptionsItemSelected(item);      }        /**       * A placeholder fragment containing a simple view.       */      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() {          }            /**           * 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) {              View rootView = inflater.inflate(R.layout.fragment_home, container, false);              return rootView;          }      }        /**       * 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) {              switch(position){                  case 0:                      login fm =   new login();                      return fm;                  case 1: return new fragmentHome();                  case 2: return new hostelcardview();              }              return null;          }            @Override          public int getCount() {              // Show 3 total pages.              return 3;          }            @Override          public CharSequence getPageTitle(int position) {              switch (position) {                  case 0:                      return "Login";                  case 1:                      return "View By Area";                  case 2:                      return "Search Hostel";              }              return null;          }      }  }    

Login XML (first fragment that contain the sign up button)

  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:weightSum="1">        <ImageView          android:layout_width="150dp"          android:layout_height="150dp"          android:id="@+id/imageView8"          android:src="@mipmap/hostel_logo"          android:scaleType="fitCenter"          android:layout_alignParentTop="true"          android:layout_centerHorizontal="true"          android:layout_marginTop="41dp" />        <TextView          android:layout_width="wrap_content"          android:textStyle="bold"          android:textColor="#EE9A00"          android:textSize="20sp"          android:layout_height="wrap_content"          android:id="@+id/editText"          android:text="Hostel Application"          android:layout_below="@+id/imageView8"          android:layout_centerHorizontal="true"          android:fontFamily=""/>        <EditText          android:layout_width="300dp"          android:layout_height="wrap_content"          android:textColor="#ffffff"          android:textStyle="bold"          android:textColorHint="#ffffff"          android:background="#5d5d5d"          android:padding="10dp"          android:hint="EMAIL"          android:textSize="20sp"          android:id="@+id/edtuserid"          android:layout_centerVertical="true"          android:layout_centerHorizontal="true" />        <EditText          android:layout_width="300dp"          android:layout_height="wrap_content"          android:textSize="20sp"          android:hint="PASSWORD"          android:textColor="#ffffff"          android:textColorHint="#ffffff"          android:textStyle="bold"          android:background="#5d5d5d"          android:padding="10dp"          android:inputType="textPassword"          android:id="@+id/edtpass"          android:layout_below="@+id/edtuserid"          android:layout_centerHorizontal="true" />      <ImageButton          android:layout_width="100dp"          android:layout_height="100dp"          android:id="@+id/loginbtn"          android:background="#0000"          android:scaleType="fitCenter"          android:src="@drawable/login"          android:layout_alignTop="@+id/signupbtn"          android:layout_toLeftOf="@+id/signupbtn"          android:layout_toStartOf="@+id/signupbtn"          android:clickable="true" />        <ProgressBar          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="center"          android:id="@+id/pbbar" />        <ImageButton          android:layout_width="100dp"          android:layout_height="100dp"          android:id="@+id/signupbtn"          android:background="#0000"          android:scaleType="fitCenter"          android:src="@mipmap/signup"          android:layout_below="@+id/edtpass"          android:layout_alignRight="@+id/editText"          android:layout_alignEnd="@+id/editText"          android:clickable="true" />        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/edtforgotpw"          android:textColor="#0000FF"          android:text="Forgot password? Click here"          android:layout_below="@+id/loginbtn"          android:layout_centerHorizontal="true"          android:clickable="true" />    </RelativeLayout>    

Login code

  package com.example.jamesytl.hostel;    import android.content.Intent;  import android.support.v4.app.Fragment;  import android.os.Bundle;  import android.support.v4.app.FragmentManager;  import android.support.v4.app.FragmentTransaction;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;  import android.widget.ImageButton;  import android.widget.TextView;    /**   * Created by jamesytl on 7/4/2016.   */  public class login extends Fragment {        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          View v = inflater.inflate(R.layout.login, container, false);          btnsignup_onclick(v);          return v;      }          public void btnsignup_onclick(View v) {            ImageButton ib = (ImageButton) v.findViewById(R.id.signupbtn);          ib.setOnClickListener(new View.OnClickListener() {                @Override              public void onClick(View arg0) {                    Fragment newfrag; newfrag = new signup();                  FragmentManager manager = getFragmentManager();                  FragmentTransaction transaction = manager.beginTransaction();                  transaction.replace(R.id.container, newfrag);                  transaction.addToBackStack(null);                  transaction.commit();                }            });        }  }    

Sign up XML (User click on sign up image button will navigate to this page)

  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent" >        <EditText          android:layout_width="300dp"          android:layout_height="wrap_content"          android:textColor="#ffffff"          android:textStyle="bold"          android:textColorHint="#ffffff"          android:background="#5d5d5d"          android:padding="10dp"          android:hint="USERNAME"          android:textSize="20sp"          android:id="@+id/edtuserid"          android:layout_above="@+id/edtpass"          android:layout_centerHorizontal="true" />        <EditText          android:layout_width="300dp"          android:layout_height="wrap_content"          android:textSize="20sp"          android:hint="PASSWORD"          android:textColor="#ffffff"          android:textColorHint="#ffffff"          android:textStyle="bold"          android:background="#5d5d5d"          android:padding="10dp"          android:inputType="textPassword"          android:id="@+id/edtpass"          android:layout_alignParentBottom="true"          android:layout_centerHorizontal="true"          android:layout_marginBottom="73dp" />        <EditText          android:layout_width="300dp"          android:layout_height="wrap_content"          android:textColor="#ffffff"          android:textStyle="bold"          android:textColorHint="#ffffff"          android:background="#5d5d5d"          android:padding="10dp"          android:hint="EMAIL"          android:textSize="20sp"          android:id="@+id/editname"          android:layout_above="@+id/edtuserid"          android:layout_centerHorizontal="true" />        <ImageView          android:layout_width="250dp"          android:layout_height="200dp"          android:id="@+id/imageView9"          android:src="@mipmap/cameraicon"          android:layout_above="@+id/textView"          android:layout_centerHorizontal="true" />        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Enter your information and photo"          android:textColor="#ffffff"          android:id="@+id/textView"          android:layout_centerVertical="true"          android:layout_centerHorizontal="true" /></RelativeLayout>    

Sign up code

  package com.example.jamesytl.hostel;    import android.content.Intent;  import android.support.v4.app.Fragment;  import android.os.Bundle;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;  import android.widget.ImageButton;  import android.widget.TextView;    /**   * Created by jamesytl on 7/4/2016.   */  public class signup extends Fragment {        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          View v = inflater.inflate(R.layout.signup, container, false);            return v;        }      }    

Have I used the replace fragment wrongly or must I add a fragment in the activity_home.xml in order to make this work?

No comments:

Post a Comment