2

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();
        }
    });
}

}

Zia Shams
  • 21
  • 4

1 Answers1

1

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();
    }
});
T_V
  • 17,440
  • 6
  • 36
  • 48
  • Tried that already before, but still the same. Tried again just now and also same. – Zia Shams Jul 07 '16 at 10:38
  • @ZiaShams check your gridview – T_V Jul 07 '16 at 10:47
  • Thank you Tarun, but both the GridViews are same copy paste except for the number of columns, so if first GridView is showing up in the switcher, why not the second one? Also I swapped 1st and 2nd GridViews, but same. – Zia Shams Jul 07 '16 at 10:54