ViewFlipper/ViewSwitcher 2nd View Empty

414 Views Asked by At

I am a newbie to Android programming and now I want to create a ViewFlipper or ViewSwitcher for 2 views which will switch view between 1 and 2 columns. The problem is that the 1st view is ok, but when I use showNext() or showPrevious(), the 2nd view shows as blank, and I have been completely out of mind thinking what is wrong. I have tried ViewFlipper and ViewSwitcher with the same results.

layout_devicelistmainswitcher.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:contentDescription="Grid/List View"
    android:id="@+id/viewbutton"
    android:background="@drawable/viewicon"
    android:layout_gravity="right" />

<ViewFlipper
        android:id="@+id/vflipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <GridView
                android:numColumns="1"
                android:gravity="center"
                android:columnWidth="100dp"
                android:stretchMode="columnWidth"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/grid"
                android:scrollbars="none" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="200dp">

            <GridView
                android:numColumns="2"
                android:gravity="center"
                android:columnWidth="100dp"
                android:stretchMode="columnWidth"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/grid_l"
                android:scrollbars="none" />
        </LinearLayout>

</ViewFlipper>

mainactivity.java

public class  activity_devicelist extends AppCompatActivity {

GridView grid;
Boolean viewtype = true;
ViewFlipper switcher;

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

    Button button = (Button) findViewById(R.id.viewbutton);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity_devicelist.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            switcher = (ViewFlipper) findViewById(R.id.vflipper);
            switcher.showPrevious();
        }
    });
}

}

1

There are 1 best solutions below

3
T_V On

Take your findViewById out like below,then It should work

switcher = (ViewFlipper) findViewById(R.id.vflipper);

 button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(activity_devicelist.this, "Button Clicked", Toast.LENGTH_SHORT).show();

        switcher.showPrevious();
    }
});