Showing posts with label Activity. Show all posts
Showing posts with label Activity. Show all posts

How to Close an Activity

public void finish () was added in API level 1
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

In this example, let's say you would like to close the activity when a button is pushed.
1. Add an OnClick Action to a Button

2. In the onClickmyButton method, add the below line. This will close the currently displayed activity when the user presses the button.

finish();
3. Compile and run!

Resources:
http://developer.android.com/reference/android/app/Activity.html#finish()
http://stackoverflow.com/questions/15393899/how-to-close-activity-and-go-back-to-previous-activity-in-android


android:background - Change the Background Color of the Layout of an Activity

How to Change the Background Color of the Layout of an Activity

1. Create an Android project, if you don't already have one.

3. Open the main.xml file and add the following highlighted line of code between the <LinearLayout> tags.This will make the background color

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#808080"  >
</LinearLayout>

4. Determine the Hex value for the color you would like and replace the android:background value with your color.

5. Compile and run!

Resources:

How to Create an Intent to Open a New Activity/Screen

1. Create a new Android project, if you don't already have one.

2. Create a new Activity called NewActivity.java in the src/com/YourApp/ directory.

3. Create a new XML file called newactivity.xml in the YourApp/res/layout directory.

        <activity
            android:name=".NewActivity"
            android:label="NewActivity" >
       </activity>

4. Add the new activity to the AndroidManifest.xml file.

5. In the MainActivity.java file, in the onCreate method, Create a New Intent object named myIntent.

         Intent myIntent = new Intent();

6. Add the below bold highlighted code to the onCreate method you would like to open the activity. The example below opens the NewActivity.java file from the MainActivity.java file.

myIntent = new Intent(this, NewActivity.class);

7. Compile and run!

8. The above will only create the Intent, to actually start the Activity, chose one of the below options.

Next Recommended Article: startActivity or startActivityForResult

  putExtra - Send Data to another Activity upon Opening

Resources:
http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click