Tag Archives: FragmentManager

How to convert an Activity class to use with FragmentPagerAdapter

This article shows how you have to alter your Activity classes to make them usable as Fragments for a horizontal slider.

You can find a tutorial how to create a smooth horizontal slider here:
Simple tutorial for a “smooth horizontal view slider” with Android

If you already have Activity classes and you want to combine them into one horizontal slider (FragmentPagerAdapter) you have to make some alterations in order to make it work. Here is how:

Let’s say you have following Activity classes in your project and want to combine them with a FragmentPagerAdapter:

  • MainArticle
  • AuthorInformation
  • CommentSection

First of all you change extends Activity to extends Fragment in those classes.

Before:

public class MainArticle extends Activity {...}

After:

public class MainArticle extends Fragment {...}

As with any Activity class there is the mandatory onCreate(…) method. Change this method to public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {…}.

Before:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.outer_layout);
...
}

After:

private LinearLayout ll;
private FragmentActivity fa;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.outer_layout, container, false);
...

return ll;
}

The difference now is that we have to use the FragmentActivity object „fa“ and get the Activity from its parent class. Otherwise we won’t be able to access the Layout XML’s elements. You now also have to return a View object which should be your most outer element in the Layout XML file. In this example’s case it would be the LinearLayout called R.layout.outer_layout.

If you used an Intent object to pass variables to that view you will have to change the line of code from:

intent = getIntent();

to

fa = super.getActivity();
intent = fa.getIntent();

All the elements you are accessing from the XML Layout file can still be accessed. However, you have to alter the lines of code from

TextView myTextView = (TextView) findViewById(R.id.myTextView);

to

TextView myTextView = (TextView) ll.findViewById(R.id.myTextView);

Also if you have any Toast messages in that Activity you have to change the line of code to the following:

Toast.makeText(fa, message, Toast.LENGTH_LONG).show();

If you have any links which forward the user to another Activity you have to change the source parameter to „fa“. This will look like this:

Intent explicitIntent = new Intent(fa, AuthorInformation.class);
startActivity(explicitIntent);

Once you’ve done all that you can add those newly converted Fragment classes to the FragmentPagerAdapter class. If you have any questions feel free to use the comment section below.

Simple tutorial for a „smooth horizontal view slider“ with Android

Screenshot of a horizontal sliderSurely you have seen some Android apps which have this cool feature to scroll through different Views smoothly with a horizontal swipe. If you have some Views and you want them to be „slide-able“ you can do this quite easily actually.

Because this feature is not in Android 2.x you will need to use the compatibility pack from Android. It is a library with classes from Android 3. It is called „android-support-v13.jar“. You can download this JAR file from developer.android.com or just download the sample project from this site which also contains this JAR file.

Create a new project and add android-support-v13.jar to its build path. First we need a FragmentPagerAdapter. Create a class according to the code below:

MyPagerAdapter.java

public class MyPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragments;

/**
* @param fm
* @param fragments
*/
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}

/*
* (non-Javadoc)
*
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
}

Next you have to create a class which handles the different Fragments (i.e. the separate „Views“). This class is going to be your Activity class.

ViewPagerFragmentActivity.java

public class ViewPagerFragmentActivity extends FragmentActivity {

private PagerAdapter mPagerAdapter;

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

// initialize the pager
this.initialisePaging();
}

/**
* Initialize the fragments to be paged
*/
private void initialisePaging() {

List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment0.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);

ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
}
}

viewpager_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/awesomepager">

</android.support.v4.view.ViewPager>

Eclipse will mark some errors. This is because the Fragment classes have not created yet. But this is what we are going to do right now. Create as many classes as you wish to have in your slider. In my example we use the classes Fragment0, Fragment1 and Fragment2. If you want to use any other classes make sure to include them in the code in the ViewPagerFragmentActivity.java class.

Fragment0.java, Fragment1.java, Fragment2.java

public class Fragment0 extends Fragment {
/**
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return (LinearLayout) inflater.inflate(R.layout.fragment0_layout, container, false);
}
}

As for the XML Layouts you can design them however you like.

Lastly add the classes ViewPagerFragmentActivity, Fragment0, Fragment1 and Fragment2 to the manifest file.

That’s all. The app should work already allowing you to slide through the Fragment classes smoothly with a horizontal swipe.

If you’re lazy you can as well just download the whole demo project here:
HorizontalSmoothSliderExample

If you already have a FragmentPagerAdapter and want to use your already existing Activity classes als Fragments you can use my other tutorial here:
How to convert an Activity class to use with FragmentPagerAdapter