0

I am implementing an Android layout that acts as an instruction manual of sorts. So basically I have several pages that the user can flip through. Let's say I implemented it like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/page_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- Put stuff for page 1 -->
    </LinearLayout>
    <LinearLayout
        android:id="@+id/page_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="INVISIBLE">
        <!-- Put stuff for page 2 -->
    </LinearLayout>
    <LinearLayout
        android:id="@+id/page_3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="INVISIBLE">
        <!-- Put stuff for page 3 -->
    </LinearLayout>

    <!-- ... -->

    <LinearLayout
        android:id="@+id/page_N"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="INVISIBLE">
        <!-- Put stuff for page N -->
    </LinearLayout>
</FrameLayout>

Basically how I would simulate the "page flipping effect" (perhaps with animation) is to make the current page invisible and make the new page visible. However, this implementation has some problems:

  1. Since I'm basically loading all the pages at the same time, the Activity holding this layout will use up a lot of heap, especially if there's background images involved.
  2. The above problem is going to get worse as I expand the manual.
  3. I've considered splitting the pages into separate activities, but that seems a bit excessive and too troublesome.

I was thinking if there's a way to dynamically load a page from an XML file during run time. As in, would it be possible to inflate a particular layout whenever the user demands it? Or if anyone has a better implementation that I haven't though of, please tell me!

3 Answers 3

2

You can simply Use ViewFlipper for that purpose, and add views to flipper dynamically as user demand for that page.

Sign up to request clarification or add additional context in comments.

Comments

1

Use a view flipper, for example

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ViewFlipper
    android:id="@+id/viewflipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/page_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- Put stuff for page 1 -->
    </LinearLayout>
    <LinearLayout
        android:id="@+id/page_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="INVISIBLE">
        <!-- Put stuff for page 2 -->
    </LinearLayout>
    <LinearLayout
        android:id="@+id/page_3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="INVISIBLE">
        <!-- Put stuff for page 3 -->
    </LinearLayout>

    <!-- ... -->

    <LinearLayout
        android:id="@+id/page_N"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="INVISIBLE">
        <!-- Put stuff for page N -->
    </LinearLayout>
  </ViewFlipper>
</FrameLayout>

Now to flip between the different views, you would (in your java file)

ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflipper);
flipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.exit_slide_right_left));
flipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.enter_slide_right_left));
flipper.setDisplayedChild(n);

which will slide as if you're flipping forward, the animation file looks like:

(enter_slide_right_left.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
        <translate android:fromXDelta="0%" android:toXDelta="-100%"
          android:fromYDelta="0%" android:toYDelta="0%"
         android:duration="700"/>
       </set>

and (exit_slide_right_left.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
      <translate
       android:fromXDelta="100%" android:toXDelta="0%"
       android:fromYDelta="0%" android:toYDelta="0%"
       android:duration="700" />
    </set>

4 Comments

This does not solve my memory problem; your solution is merely a different implementation of what I have already. I need to be able to load pages from a different resource on demand so that I don't load everything at once and use a ton of heap. Or am I missing something here?
no, its okay Android is very good at dealing with this, however you could just use one layout total and just change the text and images onClick() if you would like to do this
Does ViewFlipper offer bidirectional flipping, i.e. am I able to flip pages forward AND backwards? From what I can see, ViewFlipper is only unidirectional.
no, it can go forward and backward, you would just need to change the animations to do the opposite of what they're doing now, then do .setDisplayedChild(n-1)
0

Maybe have each page as a separate resource? That way, you can just put all the pages in an array, and retrieve individual pages by index.

1 Comment

Is it possible if you can give an example of how to load pages from a separate resource?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.