Alexiz Hernandez Alexiz Hernandez - 1 month ago 9
Java Question

Selecting a fragment to start with in ViewPager - Android

I am trying to make a Login screen where the Register, Login and Forgot Password screens are all fragments, placed like this:

Forgot Password | Login | Register

I found a simple video on YouTube showing me how to do this. The only issue I have is that when I start the Activity, it opens on the Forgot Password fragment since it is the first one, but I am wanting it to start in the middle, the Login fragment. Here is the code for the main activity. What should I change or add to this code or am I taking the wrong approach for what I am trying to achieve

Access.java

public class Access extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.access);

initializePaging();
}

private void initializePaging() {
List<Fragment> fragments = new Vector<>();
fragments.add(Fragment.instantiate(this, AccessForgotPassword.class.getName()));
fragments.add(Fragment.instantiate(this, AccessLogin.class.getName()));
fragments.add(Fragment.instantiate(this, AccessRegister.class.getName()));

PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);

ViewPager accessViewPager = (ViewPager) findViewById(R.id.accessViewPager);
accessViewPager.setAdapter(mPagerAdapter);
}

}


access.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/accessMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".access.Access">

<android.support.v4.view.ViewPager
android:id="@+id/accessViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

</LinearLayout>


AccessForgotPassword.java

public class AccessForgotPassword extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_forgot_password, container, false);
}

}


access_forgot_password.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/accessForgotPasswordMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000">
</LinearLayout>


AccessLogin.java

public class AccessLogin extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_login, container, false);
}

}


access_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/accessLoginMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
</LinearLayout>


AccessRegister.java

public class AccessRegister extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.infalte(R.layout.access_register, container, false);
}

}


access_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/accessRegisterMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0000FF">
</LinearLayout>

Answer

Set

accessViewPager.setCurrentItem(1);
Comments