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




Sunday, 3 April 2011

creating a button

How to create a button in android


  • Add a button to the main layout
  • Add the button to the activity
  • Label the button green
  • Add a click listener to the activity

Step 1

  1. open flash light activity
  2. click on R. @ the set  content view and press f3 (setContentView(R.layout.main);
go to the main view and right under label red put your code : (<Button
    android:id="@+id/green_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 android:text="@string/green"  )

(The “+” means that it’s the first time , I’m defining this id , so give me a new unique number identifier in the R-file.  Subsequently the + won’t be added if I need to put another id.)

(string name of green need to be created in string.xml to avoid error after typing the above code. (  <string name="green">Green</string>  )

3. Add a quick listener by going to myRedFlashLightActivity and type this code (Button greenButton = (Button)findViewById(R.id.green_button);
        greenButton.setOnClickListener(new View.OnClickListener() { ).

4. Run on emulator.


The outcome :