Wednesday, March 26, 2014
How to Create Interactive Widgets for the Android
1. Open the Java Eclipse editor and the project in which you want to display the widget. Double-click the XML file in the 'resource' directory and the Java source code file in the left navigation panel.
2. Create the button for the interactive widget. The button displays results after the user taps it on an Android device. Add the following XML code to the XML file:
android:layout_height='wrap_content'
android:layout_width='wrap_content'
android:text='@string/sayhello'
android:onClick='helloFunction' />The button's name is set as 'sayhello.' When the user taps the button, the 'helloFunction' function triggers. After you create the interactive button, you must create the Java function that runs the code to display feedback to the user.
3. Switch to the Java source code file you opened previously. In this file, you create the widget's function. For instance, in this example, the text 'Hello, World' displays on the screen:public void helloFunction(View view) {
TextView tv = new TextView(this);
tv.setText('Hello, World');
setContentView(tv);
}
4. Save the changes and click the Eclipse 'Run' button in the main menu to review your button functionality.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment