title: How can I switch between multiple views using a single java class?
body: <p>I inherited an Android Studio project with basically no experience with Java programming.  I've been going through and teaching myself using free online tutorials and The Big Nerd Ranch guide book and I know how to switch to different .xml views using associated .java classes, but I want to open a new .xml view associated with the same .java class.</p>
  
  <p>Basically I just want to modify the app I have which reads information from a Bluetooth device and displays measurements in a TextView along with a "Pass/Fail" display to having a pass/fail display and a "View Log" button that when clicked, takes you to a new .xml that contains the TextView with all the results.  </p>
  
  <p>I'm trying to avoid using a different .java class because I want all of the items to have the same functions, just on different .xml views, and I'm worried that moving everything I already have into a new .java class will just create more problems than I already have.</p>
  
  <p>I'm not sure if it's necessary, but here is my code:
  From my starting screen</p>
  
  <pre><code>frament_bluetooth_scanner.xml:
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;Button
          android:id="@+id/go_to_log"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="16dp"
          android:text="@string/view_log"
          android:textSize="20dp"/&gt;
  
      &lt;View
          android:id="@+id/view"
          android:layout_width="fill_parent"
          android:layout_height="2dp"
          android:background="#ccc"/&gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
      android:orientation="horizontal"
      android:paddingTop="@dimen/margin_small"
      android:paddingLeft="4dp"
      android:paddingRight="4dp"
      android:paddingBottom="4dp"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center_horizontal"&gt;
  
  
      &lt;Button
          android:id="@+id/button_present"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#ffc4ab"
          android:clickable="false"
          android:padding="16dp"
          android:text="NO TAGGANT DETECTED"
          android:textSize="26dp"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><code>button_present</code> both activates the Bluetooth test and displays the pass/fail results.  It was existing and is working as expected.</p>
  
  <p><code>go_to_log</code> is a button I added and I want it to click to take me to the next  view:</p>
  
  <pre><code>view_list.xml:
  &lt;FrameLayout
      android:orientation="horizontal"
      android:paddingLeft="4dp"
      android:paddingRight="4dp"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:visibility="visible"
      android:layout_gravity="center_horizontal"&gt;
  
      &lt;Button
          android:layout_width="fill_parent"
          android:layout_height="40dp"
          android:text="@string/clear_log"
          android:id="@+id/button_clear_log" /&gt;
  
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:paddingTop="40dp"
          android:textAppearance="?android:attr/textAppearanceSmall"
          android:id="@+id/scanLogView"
          android:maxLines = "4096"
          android:text="@string/app_name"
          android:scrollbars = "vertical"
          android:layout_gravity="center_horizontal"/&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>All of this code was originally in the <code>fragment_bluetooth_scanner.xml</code> file and everything worked as expected.  The only problem is that the <code>TextView</code> was hard to read because it displayed information for all the tests until it was cleared, and all the information didn't fit on the screen for even a single test.  I want to change it to be more user friendly.</p>
  
  <p>The main .java file is quite long so I'll post the relevant portions of it.
  From <code>ScannerFragment.java</code>:</p>
  
  <pre><code> @Override
      public void onViewCreated(View view, @Nullable Bundle 
  savedInstanceState) {
      SP = PreferenceManager.getDefaultSharedPreferences(getContext());
  
      mPowerOffButton = (Button) view.findViewById(R.id.button_poweroff);
      mDisconnectButton = (Button) view.findViewById(R.id.button_disconnect);
      mConnectButton = (Button) view.findViewById(R.id.button_connect);
      mPresence = (Button) view.findViewById(R.id.button_present);
  
      mBattery = (ProgressBar) view.findViewById(R.id.progressBar);
  
      mBlinkConnect = (Button) view.findViewById(R.id.button_connectionblink);
      mBlinkData = (Button) view.findViewById(R.id.button_communication);
      mClearLog = (Button) view.findViewById(R.id.button_clear_log);
  
      mDeviceName = (Button) view.findViewById(R.id.button_devicename);
      mDeviceSN = (Button) view.findViewById(R.id.button_devicesn);
      mBatteryPerc = (TextView) view.findViewById(R.id.label_batterypct);
  
      mReadingLog  = (TextView) view.findViewById(R.id.scanLogView);
      mReadingLog.setMovementMethod(new ScrollingMovementMethod());
      mReadingLog.setText(readFromFile());
  
      mViewLog = (Button)view.findViewById(R.id.go_to_log);
  
  
      telephonyManager = (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
  
      initLocationService(this.getContext());
  
      final ActionBar actionBar = getActivity().getActionBar();
      if (null == actionBar) {
          return;
      }
      final Drawable D = getResources().getDrawable(R.drawable.stardust2);
      actionBar.setBackgroundDrawable(D);
      actionBar.setTitle("");
  
  
  
  }
  
  private void setupScanner() {
      Log.d(TAG, "setupScanner()");
  
      // Initialize the send button with a listener that for click events
      mPowerOffButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              if(isIris) {
                  publishMessage(Constants.COMMAND_POWEROFF_IRIS);
              } else {
                  publishMessage(Constants.COMMAND_POWEROFF);
              }
              mScannerService.stop();
          }
      });
  
      // Initialize the send button with a listener that for click events
      mDisconnectButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              // Send a message using content of the edit text widget
              publishMessage(Constants.COMMAND_DISCONNECT);
              mScannerService.stop();
          }
      });
  
      // Initialize the send button with a listener that for click events
      mConnectButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              // Send a message using content of the edit text widget
              publishMessage(Constants.COMMAND_ON_CONNECT);
          }
      });
  
      mClearLog.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              // Send a message using content of the edit text widget
              mReadingLog.setText("");
              if(SP.getBoolean("writeToFile", true)) {
                  writeToFile("", "", false);
              }
          }
      });
  </code></pre>
  
  <p>This is where all the existing actions take place.  The main thing that deters me from creating a new .java class is that <code>mReadingLog</code> is used all throughout this class, and I don't understand reading/writing text files very well at this point, so I want to avoid altering anything that has to do with that process.</p>
  
  <p>I've gone through tutorials in the past such as <a href="https://developer.android.com/training/basics/firstapp/starting-activity.html" rel="nofollow noreferrer">this one</a>, but I can't find anything on how to open a new <code>xml</code> file while associating in with the same <code>.java</code> class.  Is this possible?</p>