Hardware Reference
In-Depth Information
Part 3 describes the last two objects of the user interface: the buttons. These two buttons take up the reaming
space at the bottom right of the screen, with the send button located closer to the input box. Both buttons are set up
as the toggle button was in the blink example; the text of the button is linked to a string and the button is set up to
call a function when it is pressed. As compared to the toggle button, these buttons are momentary and do not hold a
changed state when released.
Listing 4-12. main.xml, Part 3 of 3
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="clearScreen"
android:text="@string/clear" >
</Button>
<Button
android:id="@+id/sendbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/clear"
android:onClick="SendData"
android:text="@string/send"
</Button>
</RelativeLayout>
The strings.xml file needs to be modified to contain the resources for the buttons and the EditText boxes;
Listing 4-13 shows the required changes. The new strings.xml file contains five tags: the app name, two hints,
and two button names. strings.xml is the only other XML file that needs to be modified for this example.
AndroidManifest.xml and accessory_filter.xml are needed, but require no modification.
Listing 4-13. New strings.xml File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mega ADK</string>
<string name="hint">Data from Arduino board will be displayed here</string>
<string name="sendhint">Type data to send</string>
<string name="send">Send</string>
<string name="clear">Clear</string>
</resources>
With the user interface finished, the code can be added to the main Java file to complete the functionality of the
serial monitor clone. Two different objects are used and therefore need to be imported. The following lines of code
need to replace the import android.widget.ToggleButton; line in the import section of CH4ExamplesActivity.java :
import android.widget.Button;
import android.widget.EditText;
 
Search WWH ::




Custom Search