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.

About Dominik
Currently working as an Android/iOS developer for Cineman, Switzerland. Owner of http://www.ukon.ch and https://play.google.com/store/apps/details?id=com.cheatdatabase

Comments

5 Responses to “How to convert an Activity class to use with FragmentPagerAdapter”
  1. codeskraps sagt:

    How do you set up the onclicklisteners for your view. Once you get the view like you suggested by texview = (textview) ll.findbyid(R.id.textview) before is was able to set the the listener by textview.setonclicklistener(this) or set a new View.onclicklistener but now when I run the app I get a nullpointerexception

    any help?

  2. Tejas Jadhav sagt:

    Ok, it all works just too fine! But Thread performance just seems to be worst, I mean lot of lags. Is there any way out?

  3. parag sagt:

    i have one issue that in my project class
    JCompassActivity extends SensorActivity
    how can i use in fragmentpageradapter ?

Trackbacks

Check out what others are saying about this post...
  1. […] 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 Posted in: This And That   Tags: Android, android-support-v13, android-support-v4, Fragment, […]



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.