title: Radio Group + Swipe Navigation + Button Android
body: <p>I have researched and tried various algorithms and ritual dances but to no avail.  I need a three page layout that will be implemented through android swipe.  I can get that set up just fine.  The issue I am having is on my first layout I have a radio group, edit text field and a button.  To test that the radio buttons are being selected properly I have the OnCheckedChangeListener edit the id of the radio button to the hint field of the edit text box. My first implementation here.</p>
  
  <pre><code>    import android.os.Bundle;
      import android.support.v4.app.Fragment;
      import android.support.v4.app.FragmentActivity;
      import android.support.v4.app.FragmentManager;
      import android.support.v4.app.FragmentPagerAdapter;
      import android.support.v4.view.ViewPager;
      import android.view.LayoutInflater;
      import android.view.Menu;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.EditText;
      import android.widget.RadioButton;
      import android.widget.RadioGroup;
  
      public class MainActivity extends FragmentActivity 
      {
  
  /**
   * The {@link android.support.v4.view.PagerAdapter} that will provide
   * fragments for each of the sections. We use a
   * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
   * will keep every loaded fragment in memory. If this becomes too memory
   * intensive, it may be best to switch to a
   * {@link android.support.v4.app.FragmentStatePagerAdapter}.
   */
  SectionsPagerAdapter mSectionsPagerAdapter;
  
  /**
   * The {@link ViewPager} that will host the section contents.
   */
  ViewPager mViewPager;   
  
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      // Create the adapter that will return a fragment for each of the three
      // primary sections of the app.
      mSectionsPagerAdapter = new SectionsPagerAdapter(
              getSupportFragmentManager());
  
      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.pager);
      mViewPager.setAdapter(mSectionsPagerAdapter);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) 
  {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  /**
   * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
   * one of the sections/tabs/pages.
   */
  public class SectionsPagerAdapter extends FragmentPagerAdapter 
  {
  
      public SectionsPagerAdapter(FragmentManager fm) 
      {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) 
      {
          // getItem is called to instantiate the fragment for the given        page.
          // Return a DummySectionFragment (defined as a static inner class
          // below) with the page number as its lone argument.
  
          Fragment fragment = new DummySectionFragment();
          Bundle args = new Bundle();
          args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
  
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public int getCount() 
      {
          // Show 3 total pages.
          return 3;
      }
  
      @Override
      public CharSequence getPageTitle(int position) 
      {
          switch (position) 
          {
              case 0:
                  return getString(R.string.title_section1);
              case 1:
                  return getString(R.string.title_section2);
              case 2:
                  return getString(R.string.title_section3);
          }
          return null;
      }
  }
  
  /**
   * A dummy fragment representing a section of the app, but that simply
   * displays dummy text.
   */
  public class DummySectionFragment extends Fragment 
  {
      /**
       * The fragment argument representing the section number for this
       * fragment.
       */
      RadioGroup group1;
      RadioButton radio1, radio2, radio3;
      EditText editText1;
  
      public static final String ARG_SECTION_NUMBER = "section_number";
  
      public DummySectionFragment() 
      {
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) 
      {
          // Create a new TextView and set its text to the fragment's section
          // number argument value.
  
          int position = getArguments().getInt(ARG_SECTION_NUMBER);
  
           int resId = 0;
              switch (position) 
              {
                  case 1:
                      resId = R.layout.layout1;
                      break;
                  case 2:
                      resId = R.layout.layout2;
                      break;
                  case 3:
                      resId = R.layout.layout3;
                      break;
              }
  
      View view = inflater.inflate(resId, container, false);
  
  
      if (position == 1)
      {
      group1 = (RadioGroup)
              findViewById(R.id.radioGroup1);
          radio1 = (RadioButton)
              group1.findViewById(group1.getCheckedRadioButtonId());
          radio2 = (RadioButton)
              group1.findViewById(group1.getCheckedRadioButtonId());
          radio3 = (RadioButton)
              group1.findViewById(group1.getCheckedRadioButtonId());
  
          editText1 = (EditText) findViewById(R.id.editText1);
  
  
          group1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
          {
  
              public void onCheckedChanged(RadioGroup group, int checkedId) 
              {
                  switch (checkedId)
                  {
                      case R.id.radio1:
                          editText1.setHint("Layout1 selected");
                          break;
  
                      case R.id.radio2:
                          editText1.setHint("Layout2 selected");
                          break;
  
                      case R.id.radio3:
                          editText1.setHint("Layout3 selected");
                          break;
                  }
  
              }
          });
      }
      return view;
      }
  
  
          }
          }
  </code></pre>
  
  <p>will implement the layouts just fine but fails when adding the last if statement (if (position == 1)</p>
  
  <p>However, my next implementation works but after I swipe to layout3, layout1 doesn't reinstantiate my argument as it did when it first loads up
  Here is the code snippet</p>
  
  <pre><code>    import android.content.Context;
      import android.os.Bundle;
      import android.os.Parcelable;
      import android.support.v4.app.FragmentActivity;
      import android.support.v4.view.PagerAdapter;
      import android.support.v4.view.ViewPager;
      import android.view.LayoutInflater;
      import android.view.Menu;
      import android.view.View;
      import android.widget.EditText;
      import android.widget.RadioButton;
      import android.widget.RadioGroup;
      import android.widget.Toast;
  
      public class MainActivity extends FragmentActivity {
  
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
     MyPagerAdapter adapter = new MyPagerAdapter();
      ViewPager myPager = (ViewPager) findViewById(R.id.pager);
     myPager.setAdapter(adapter);
      myPager.setCurrentItem(0);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  class MyPagerAdapter extends PagerAdapter {
  
      RadioGroup group1;
      RadioButton radio1, radio2, radio3;
      EditText editText1;
  
      public int getCount() {
          return 3;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          switch (position) {
          case 0:
              return getString(R.string.title_section1);
          case 1:
              return getString(R.string.title_section2);
          case 2:
              return getString(R.string.title_section3);
          }
          return null;
      }
  
      public Object instantiateItem(View collection, int position) {
  
          LayoutInflater inflater = (LayoutInflater) collection.getContext()
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         int resId = 0;
          switch (position) {
          case 0:
              resId = R.layout.layout1;
              break;
          case 1:
              resId = R.layout.layout2;
              break;
          case 2:
              resId = R.layout.layout3;
              break;
          }
  
          String a = Integer.toString(position);
          Toast.makeText(getBaseContext(), a, Toast.LENGTH_SHORT).show();
  
         if (position == 1){
                  group1 = (RadioGroup)
                      findViewById(R.id.radioGroup1);
              radio1 = (RadioButton)
                      group1.findViewById(group1.getCheckedRadioButtonId());
              radio2 = (RadioButton)
                      group1.findViewById(group1.getCheckedRadioButtonId());
              radio3 = (RadioButton)
                      group1.findViewById(group1.getCheckedRadioButtonId());
  
              editText1 = (EditText) findViewById(R.id.editText1);
  
              group1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
                {
  
                  public void onCheckedChanged(RadioGroup group, int checkedId) 
                  {
                      switch (checkedId)
                      {
                          case R.id.radio1:
                              editText1.setHint("radio1");
                              break;
  
                          case R.id.radio2:
                              editText1.setHint("radio2");
                              break;
  
                          case R.id.radio3:
                              editText1.setHint("radio3");
                              break;
                      }
  
                  } 
                });
          }
  
          View view = inflater.inflate(resId, null);
  
          ((ViewPager) collection).addView(view, 0);
  
          return view;
  
      }
  
  
      @Override
      public void destroyItem(View arg0, int arg1, Object arg2) 
      {
  
          ((ViewPager) arg0).removeView((View) arg2);
  
      }
  
  
      @Override
      public boolean isViewFromObject(View arg0, Object arg1) 
      {
          return arg0 == ((View) arg1);
  
      }
  
      @Override
      public Parcelable saveState() 
      {
          return null;
      }
      } 
          }
  </code></pre>
  
  <p>Here is my main layout and layout 1</p>
  
  <p>main.xml</p>
  
  <pre><code>    &lt;android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/pager"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity" &gt;
  
  &lt;!--
  This title strip will display the currently visible page title, as well as the page
  titles for adjacent pages.
  --&gt;
  
  &lt;android.support.v4.view.PagerTitleStrip
      android:id="@+id/pager_title_strip"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="top"
      android:background="#33b5e5"
      android:paddingBottom="4dp"
      android:paddingTop="4dp"
      android:textColor="#fff" /&gt;
  
  &lt;/android.support.v4.view.ViewPager&gt;
  </code></pre>
  
  <p>layout1.xml</p>
  
  <pre><code>    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/RelativeLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true" /&gt;
  
  &lt;RadioGroup
      android:id="@+id/radioGroup1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="57dp" &gt;
  
      &lt;RadioButton
          android:id="@+id/radio1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:checked="true"
          android:text="radio1" /&gt;
  
      &lt;RadioButton
          android:id="@+id/radio2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="radio2" /&gt;
  
      &lt;RadioButton
          android:id="@+id/radio3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="radio3" /&gt;
  &lt;/RadioGroup&gt;
  
  &lt;EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/radioGroup1"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="52dp"
      android:ems="10" &gt;
  
      &lt;requestFocus /&gt;
  &lt;/EditText&gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/editText1"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="40dp"
      android:text="Button" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>