0

Is this even possible? I created this view flipper and I am calling the view flipper from the onClick inside of the xml layout. I have created a view flipper before but for some reason it's not working.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="@android:color/transparent"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:cacheColorHint="@android:color/transparent" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/vf_sample1_main"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent"
            android:orientation="vertical" >

            <include layout="@layout/headers_main" />

            <include layout="@layout/searchbar" />

            <TextView
                android:id="@+id/sdfsd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:maxHeight="85dp"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                onClick="gotoSomething"
                android:singleLine="false" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/aquablue"
                android:textSize="15dp"
                android:textStyle="bold" />

            <include layout="@layout/listview_item_row_now_playing_info" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/aquablue"
                android:textSize="15dp"
                android:textStyle="bold" />

            <include layout="@layout/container_listview" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/vf_sdfsdfadf"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@null"
            android:cacheColorHint="@android:color/transparent"
            android:orientation="vertical" >

            <include layout="@layout/header_vf_asdfasdfadadafsd" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/vf_adsfasdfadsfasd"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@null"
            android:cacheColorHint="@android:color/transparent"
            android:orientation="vertical" >
        </LinearLayout>

    </ViewFlipper>

</LinearLayout>

code:

 public class BeatSpotPrototypeActivity extends BaseListActivity {
        private final String TAG = BeatSpotPrototypeActivity.class.getSimpleName();
        private ApplicationStateManager mAppStateMananger = ApplicationStateManager.GetInstance();
        private Context mCtx = null;
        private boolean mIsInitalized = false;
        private boolean mDataChanded= false;
        private ViewFlipper mViewFlipper= null;
        private DrawableManager mDrawManager = new DrawableManager();
        private enum ViewFlipperModes {
            MAIN (0),
            NOW_PLAYING (1),
            SONG_INFORMATION (2);

            private final int index;   

            ViewFlipperModes(int index) {
                this.index = index;
            }

            public int index() { 
                return index; 
            }
        }
        </code>
    <code>
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_beatspot_activity);

            if (!mIsInitalized)
                init();
            else
                mDataChanded = true;
            showLoading(false);

        }

        private void init() {
            mCtx                                            = this;
            mIsInitalized                                   = true;
            mViewFlipper                                    = (ViewFlipper)findViewById(R.id.viewFlipper);
            fadeDrawables();
        }


        public void gotoBeatspotMain(View v) {
            mViewFlipper.setDisplayedChild(ViewFlipperModes.MAIN.index());
        }

        public void gotoNowPlaying(View v) {
            Log.d(TAG, mViewFlipper.getCurrentView().toString());
            mViewFlipper.setDisplayedChild(ViewFlipperModes.NOW_PLAYING.index());
        }

        public void gotoSongDetails(View v) {
            mViewFlipper.setDisplayedChild(ViewFlipperModes.SONG_INFORMATION.index());
            Log.d(TAG, String.valueOf(ViewFlipperModes.SONG_INFORMATION.index()));
        }

    }
Shruti
  • 1
  • 13
  • 55
  • 95
James
  • 9,694
  • 5
  • 32
  • 38

2 Answers2

3

setDisplayedChild takes an index, not an id.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • True, but how come the mViewFlipper.showNext(); isn't working? – James May 23 '12 at 18:40
  • I changed the setDisplayChild(2) and still nothing. I also tried it showNext() alone and it still didn't work. – James May 23 '12 at 18:47
  • Yup he is correct. A simpler way to put it is that if you have a multiple views eg. more than 2 setDisplayedChild just goes to that view and since you gave out an R.id.view which is technically an integer code to find the appropriate view. It wont give out an error but it is still wrong. – sdfwer May 23 '12 at 18:47
  • Also remember that everything starts at 0. So 0 = 1, 1 = 2, and so on. – sdfwer May 23 '12 at 18:48
  • 1
    @sdfwer i tired it with 0,1, and 2. I also tired mViewFlipper.showNext() – James May 23 '12 at 19:00
1

before on create

ViewFlipper mViewFlipper = (ViewFlipper)findViewbyId(location);

on your onCreate() or onStart

Ensure that whatever your layout on setcontentView(R.layout.something) does contain the view flipper

Also try this for me. before this line mViewFlipper.setDisplayedChild(ViewFlipperModes.MAIN.index()); Type in:

int i = (ViewFlipperModes.MAIN.index()); place a debug point here
mViewFlipper.setDisplayedChild(i);

I also suggest using the debugger to try figure out if your flip views events are being called inside the debugger.

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

    <ViewFlipper 
            android:layout_marginTop="20dip"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ViewFlipper">
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView 
            android:id="@+id/page1"
            android:text="@string/loading_string"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="#000000"
            android:typeface="serif"/>  

        </LinearLayout>

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <TextView 
            android:id="@+id/page2"
            android:text="@string/loading_string"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="#000000"
            android:typeface="serif" />  
        </LinearLayout>

    </ViewFlipper>


</LinearLayout>
sdfwer
  • 1,042
  • 3
  • 10
  • 23
  • I debugged an the view is actually changing according to the debugger however, the display is not being updated. – James May 24 '12 at 17:15
  • Also, when I debug the mViewFlipper does contain my three layout. I am going to try to run it on the ui thread. – James May 24 '12 at 17:22
  • I already tired code similar to the code above and I am having the same issue. – James May 24 '12 at 17:37
  • Alright project clean it. Then ensure that the same layout you are using for your setcontentview has the same mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper); – sdfwer May 24 '12 at 17:44
  • Now how does a user switch views in your case? Since you haven't posted that code. If you didn't have either the gesture or the button click event for it. It wont switch other than just being declared. – sdfwer May 24 '12 at 17:44
  • the user switch views onclick its inside of the xml layout. – James May 24 '12 at 17:49
  • I cleaned my project. Also, when I debug my code i've checked the mViewFlipper object and it contains my views. Also, when i replace my code w/ mViewFlipper.showNext() it goes to the next view but it is not being displayed visually. – James May 24 '12 at 17:53
  • I found a solution however it defeats the purpose of the view flipper. I set the Layout to invisible, and I set the layout i want to view to visible – James May 24 '12 at 18:42
  • ViewFlipper is only meant on 1 layout containing derived class objects of that layout. EG. I will put an example on my answer. – sdfwer May 24 '12 at 19:22
  • If what you did was use multiple layouts and display them on the screen then yes the one that is on the front will overlap the one that is behind and viewflipper will not do anything since it doesn't hold those classes inside of it. – sdfwer May 24 '12 at 19:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11715/discussion-between-james-and-sdfwer) – James May 24 '12 at 20:00
  • For some reason the view flipper was out of scope. It needs to be init right after the onCreate or start. – James May 29 '12 at 16:11