Monday, 4 April 2011

Green flash light activity

how to create a green flash  light activity 
This program creates a green flash light activities and a button. It switches between green flash light and red by clicking the button.

Steps

To create a green flash light activity you go to flash light activity à create activity à new class.

·        Type “green flash light activity” to name.
·        Type Android activity to super class.
Go to lay out and click create a new file then name it “green.xml”  Type in the code below :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:background="@color/green"
    >
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/green"
    />
    <Button
    android:id="@+id/red_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/red"
    />
</LinearLayout>


Green color should be define in color.xml. (  <color name="green">#FF00FF00</color>)

Go to green flashlight activity and override oncreate method{ sourceà overridemethodà oncreate(bundle). Then set the content view.

(setContentView(R.layout.green);)


Use intent in red flash activity. The purpose of this intent is to allow the activity to switch between red and green.

(public void onClick(View v) {
                  Intent intent = new Intent(MyRedFlaslightActivity.this,GreenFlashLightActivity.class);
                  startActivity(intent);
                                                                  )

Create the activity in the manifest cos android only recognize the activity designrd in the manifest. Design green flash light activity in the manifest.

(<activity android:name=".GreenFlashLightActivity" android:label="@string/app_name"/>)

To switch over to red light activity  by clicking a red button. You need to create a red button object in the  green light activity, but no need for intent because it’s just to end the program and not to switch within. (Button redButton = (Button)findViewById(R.id.red_button);
        redButton.setOnClickListener(new View.OnClickListener() {
                 
            @Override
                  public void onClick(View v) {
                  finish();
                                                                  )
Instead of intent  you use finish();

Then run




No comments:

Post a Comment