Workshop India
» An activity is the equivalent of a Frame/Window in
  GUI toolkits. It takes up the entire drawable area
  of the screen (minus the status and title bars on
  top).
» Activities are meant to display the UI and get input
  from the user.
» Activities can be frozen when the focus is switched
  away from them (eg: incoming phone call).
» Services on the other hand keep running for the
  duration of the user’s ‘session’ on the device.
» Any screen state is called an activity.

» An Application always starts with a main activity

» An application can have multiple activities that the
  user moves in and out of.

» A stack of activities is maintained by the android
  system to handle back presses and returns.
» In res/layout we’ll need to make 2 xml files each
  defining the layout for the individual activity.
   ˃ main.xml
   ˃ second.xml


» Each activity needs an activity class associated
  with it, these are present in the /src folder
   ˃ mainActivity.java -> main.xml
   ˃ secondActivity.java -> second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/linearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I&apos;m screen 1 (main.xml)"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  <Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"                     Button that we’ll
    android:layout_height="wrap_content"                   use to move to the
    android:text="Click me to another screen" />           next activity
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/linearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I&apos;m screen 2 (second.xml)"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
package com.wingie;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class mainactivity extends Activity {
  Button button;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);
      addListenerOnButton();
    }
    public void addListenerOnButton() {
      final Context context = this;
      button = (Button) findViewById(R.id.button1);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(context, second.class);
            startActivity(intent);
          }
      });              Then we create an
    }                  intent to move to the
                    next activity,
}                   (Second.java)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.wingie"
     android:versionCode="1"
     android:versionName="1.0">
  <uses-sdk android:minSdkVersion="13"/>
  <application android:label="@string/app_name">
    <activity android:name=".main"
          android:label="@string/app_name">
      <intent-filter>
         <action android:name="android.intent.action.MAIN"/>
         <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <activity                      This describes the
                                   second activity in
         android:label="@string/app_name"
                                   the application
         android:name=".second" >
    </activity>
  </application>
</manifest>
The super.fn() makes sure that the
                                                       overridden method is also called
                                                       before any of our code runs..



public class ExampleActivity extends Activity {
@Override
  public void onCreate(Bundle savedInstanceState) {   The activity is being created.
    super.onCreate(savedInstanceState);
}

@Override
  protected void onStart() {                          The activity is about to
                                                      become visible.
    super.onStart();
}

@Override
  protected void onResume() {                         The activity has become
                                                      visible (it is now "resumed").
    super.onResume();
}
@Override
                                 Another activity is taking
    protected void onPause() {
                                 focus (this activity is about
      super.onPause();           to be "paused").
}

@Override
  protected void onStop() {
    super.onStop();              The activity is no longer
}                                visible (it is now "stopped")

@Override
 protected void onDestroy() {
   super.onDestroy();
                                 The activity is about to be
    }                            destroyed.
}
» A Fragment represents a behavior or a portion of
  user interface in an Activity.

» A fragment must always be embedded in an
  activity
   ˃ fragment's lifecycle is directly affected by the host activity's lifecycle.


» You can insert a fragment into your activity layout
   ˃ by declaring the fragment in the activity's layout file, as
     a <fragment> element
   ˃ or from your application code by adding it to an existing ViewGroup
     container.
» MainActivity.java
   ˃ Main Activity that will host the 2 fragments
   ˃ Layout : res/layout/Activity_main.xml


» Frag1.java
   ˃ Fragment 1 class
   ˃ Layout : res/layout/Frag1.xml



» Frag2.java
   ˃ Fragment 2 class.
   ˃ Layout: res/layout/frag2.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <fragment
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:name="com.example.fragtest.Frag1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="45"
  android:id="@+id/frag1">
</fragment>
<fragment

xmlns:android="http://schemas.android.com/apk/res/an
droid"
  android:name="com.example.fragtest.Frag2"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="55"
  android:id="@+id/frag2">
</fragment>

</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <TextView
    android:id="@+id/textview01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Frag ONE"
    tools:context=".MainActivity" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textview01"
    android:text="ClickHere"
    tools:context=".MainActivity" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <TextView
    android:id="@+id/textview02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Frag TWO"
    tools:context=".MainActivity" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textview02"
    android:text="Click---Here"
    tools:context=".MainActivity" />
</RelativeLayout>
package com.example.fragtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    }
}
package com.example.fragtest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Frag1 extends Fragment {

public Frag1() {
// TODO Auto-generated constructor stub
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
    View view = inflater.inflate(R.layout.frag1, container, false);
return view;
}

}
package com.example.fragtest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Frag1 extends Fragment {

public Frag1() {
// TODO Auto-generated constructor stub
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
    View view = inflater.inflate(R.layout.frag1, container, false);
return view;
}

}
» What are Fragments?

» What are Fragment Transactions?

» Message passing between two fragments.
  ˃ With example source code.

04 activities - Android

  • 1.
  • 2.
    » An activityis the equivalent of a Frame/Window in GUI toolkits. It takes up the entire drawable area of the screen (minus the status and title bars on top). » Activities are meant to display the UI and get input from the user. » Activities can be frozen when the focus is switched away from them (eg: incoming phone call). » Services on the other hand keep running for the duration of the user’s ‘session’ on the device.
  • 3.
    » Any screenstate is called an activity. » An Application always starts with a main activity » An application can have multiple activities that the user moves in and out of. » A stack of activities is maintained by the android system to handle back presses and returns.
  • 4.
    » In res/layoutwe’ll need to make 2 xml files each defining the layout for the individual activity. ˃ main.xml ˃ second.xml » Each activity needs an activity class associated with it, these are present in the /src folder ˃ mainActivity.java -> main.xml ˃ secondActivity.java -> second.xml
  • 5.
    <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I&apos;m screen 1 (main.xml)" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" Button that we’ll android:layout_height="wrap_content" use to move to the android:text="Click me to another screen" /> next activity </LinearLayout>
  • 6.
    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I&apos;m screen 2 (second.xml)" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
  • 7.
    package com.wingie; import android.app.Activity; importandroid.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class mainactivity extends Activity { Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
  • 8.
    setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { final Context context = this; button = (Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(context, second.class); startActivity(intent); } }); Then we create an } intent to move to the next activity, } (Second.java)
  • 10.
    <?xml version="1.0" encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.wingie" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="13"/> <application android:label="@string/app_name"> <activity android:name=".main" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity This describes the second activity in android:label="@string/app_name" the application android:name=".second" > </activity> </application> </manifest>
  • 11.
    The super.fn() makessure that the overridden method is also called before any of our code runs.. public class ExampleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { The activity is being created. super.onCreate(savedInstanceState); } @Override protected void onStart() { The activity is about to become visible. super.onStart(); } @Override protected void onResume() { The activity has become visible (it is now "resumed"). super.onResume(); }
  • 12.
    @Override Another activity is taking protected void onPause() { focus (this activity is about super.onPause(); to be "paused"). } @Override protected void onStop() { super.onStop(); The activity is no longer } visible (it is now "stopped") @Override protected void onDestroy() { super.onDestroy(); The activity is about to be } destroyed. }
  • 14.
    » A Fragmentrepresents a behavior or a portion of user interface in an Activity. » A fragment must always be embedded in an activity ˃ fragment's lifecycle is directly affected by the host activity's lifecycle. » You can insert a fragment into your activity layout ˃ by declaring the fragment in the activity's layout file, as a <fragment> element ˃ or from your application code by adding it to an existing ViewGroup container.
  • 15.
    » MainActivity.java ˃ Main Activity that will host the 2 fragments ˃ Layout : res/layout/Activity_main.xml » Frag1.java ˃ Fragment 1 class ˃ Layout : res/layout/Frag1.xml » Frag2.java ˃ Fragment 2 class. ˃ Layout: res/layout/frag2.xml
  • 19.
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.example.fragtest.Frag1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="45" android:id="@+id/frag1"> </fragment>
  • 20.
    <fragment xmlns:android="http://schemas.android.com/apk/res/an droid" android:name="com.example.fragtest.Frag2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="55" android:id="@+id/frag2"> </fragment> </LinearLayout>
  • 21.
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Frag ONE" tools:context=".MainActivity" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textview01" android:text="ClickHere" tools:context=".MainActivity" /> </RelativeLayout>
  • 22.
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textview02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Frag TWO" tools:context=".MainActivity" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textview02" android:text="Click---Here" tools:context=".MainActivity" /> </RelativeLayout>
  • 23.
    package com.example.fragtest; import android.os.Bundle; importandroid.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
  • 24.
    package com.example.fragtest; import android.app.Fragment; importandroid.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Frag1 extends Fragment { public Frag1() { // TODO Auto-generated constructor stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.frag1, container, false); return view; } }
  • 25.
    package com.example.fragtest; import android.app.Fragment; importandroid.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Frag1 extends Fragment { public Frag1() { // TODO Auto-generated constructor stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.frag1, container, false); return view; } }
  • 26.
    » What areFragments? » What are Fragment Transactions? » Message passing between two fragments. ˃ With example source code.