Friday, January 10, 2014

How to Make an Exit Button for the Android



XML

The 'main.xml' file contains the buttons and properties for your Android app. You must add the button in the XML file before you can use it in your app to close the program. The following code adds a button you can use to close the app:

android:layout_height='wrap_content'

android:layout_width='wrap_content'

android:text='@string/self_destruct'

android:onClick='selfDestruct' />

Java Code

After you create the XML code for the button, you can set up the code that handles the 'finish' function when the user clicks it. The following code closes the application when the button is clicked:Intent intent = new Intent(Intent.ACTION_MAIN);

finish();

Testing

You use the Eclipse software to create Android apps. Eclipse includes a debugger and emulator. These functions let you run the code in an Android development environment so you can test for any coding errors. You use the debugger to test the new exit button. Click 'Run' and tap the button to check for any coding issues when the app closes.

Considerations

If you want to offer a close button, prompt the user to save a file if a file is opened that must be saved before closing. The app programmer must include this functionality in the code or the Java application will close without saving the user's changes.

No comments:

Post a Comment