Reputation: 71
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.
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.
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.
I'm not sure if it's necessary, but here is my code: From my starting screen
frament_bluetooth_scanner.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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"/>
<View
android:id="@+id/view"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#ccc"/>
</LinearLayout>
<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">
<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"
/>
</LinearLayout>
button_present
both activates the Bluetooth test and displays the pass/fail results. It was existing and is working as expected.
go_to_log
is a button I added and I want it to click to take me to the next view:
view_list.xml:
<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">
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="@string/clear_log"
android:id="@+id/button_clear_log" />
<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"/>
</FrameLayout>
All of this code was originally in the fragment_bluetooth_scanner.xml
file and everything worked as expected. The only problem is that the TextView
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.
The main .java file is quite long so I'll post the relevant portions of it.
From ScannerFragment.java
:
@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);
}
}
});
This is where all the existing actions take place. The main thing that deters me from creating a new .java class is that mReadingLog
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.
I've gone through tutorials in the past such as this one, but I can't find anything on how to open a new xml
file while associating in with the same .java
class. Is this possible?
Upvotes: 1
Views: 1302
Reputation: 19298
I'm not sure i understood. But If you want to switch between different views by the press of a button or ... you can use ViewFilpper.
Add it as a View in your activity layout.
<ViewFlipper
android:id="@+id/simpleViewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Add View’s Here Which You Want to Flip -- >
<include layout="First"/>
<include layout="Second"/>
<include layout="third"/>
<!-- ... -->
</ ViewFlipper >
Upvotes: 1
Reputation: 3064
... OK, so I actually read it this time... lol... you can create function in a single class. Make that static and then you can call those functions anywhere.
This is what I do
public class MethodContants {
public static void showLog(String tag, String logMessage, boolean error) {
if (error) {
Log.e(tag, logMessage);
} else {
Log.i(tag, logMessage);
}
}
public static String showStringResource(int resource) {
return getApplicationContext().getResources().getString(resource);
}
}
So in any of my classes, I can call like so:
MethodConstants.showLog(TAG, "message", true);
Upvotes: 1