Mobile Programming Lecture 6 Fragments Permissions Broadcast Receivers

Mobile Programming Lecture 6 Fragments, Permissions, Broadcast. Receivers

Agenda • Dynamic UI • Layout. Inflater • Fragments • Permissions • Broadcast. Receiver

Building the UI Dynamically So far we've been creating UIs that are defined before runtime There are some cases where you will need to build the UI dynamically Consider this XML layout file

Building the UI Dynamically <Table. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="match_parent" android: layout_height="match_parent" > <Table. Row android: layout_width="wrap_content" android: layout_height="wrap_content" > <Text. View android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="Medium Text" android: text. Appearance="? android: attr/text. Appearance. Medium" /> </Table. Row> <Table. Row android: layout_width="wrap_content" android: layout_height="wrap_content" > </Table. Row> </Table. Layout>

Building the UI Dynamically Now let's look at Java code that can build a similar UI at runtime

Building the UI Dynamically <Table. Layout android: layout_width="match_parent" public void on. Create(Bundle state) { super. on. Create(saved. Instance. State); android: layout_height="match_parent" > <Table. Row Table. Layout layout = new Table. Layout(this); android: layout_width="wrap_content" android: layout_height="wrap_content" > Table. Row row = new Table. Row(this); <Text. View android: layout_width="wrap_content" Text. View tv = new Text. View(this); android: layout_height="wrap_content" tv. set. Text("Medium Text"); android: text="Medium Text"/> </Table. Row> row. add. View(tv); <Table. Row layout. add. View(row); android: layout_width="wrap_content" android: layout_height="wrap_content" > row = new Table. Row(this); </Table. Row> layout. add. View(row); </Table. Layout> set. Content. View(layout); }

Building the UI Dynamically <Table. Layout android: layout_width="match_parent" This argument asks for a Context, e. g. public void on. Create(Bundle state) { this or get. Application. Context() super. on. Create(saved. Instance. State); android: layout_height="match_parent" > <Table. Row Table. Layout layout = new Table. Layout(this); android: layout_width="wrap_content" android: layout_height="wrap_content" > Table. Row row = new Table. Row(this); <Text. View android: layout_width="wrap_content" Text. View tv = new Text. View(this); android: layout_height="wrap_content" tv. set. Text("Medium Text"); android: text="Medium Text"/> </Table. Row> row. add. View(tv); <Table. Row layout. add. View(row); android: layout_width="wrap_content" android: layout_height="wrap_content" > row = new Table. Row(this); </Table. Row> layout. add. View(row); </Table. Layout> set. Content. View(layout); }

Building the UI Dynamically <Table. Layout android: layout_width="match_parent" android: layout_height="match_parent" > "this" refers to the instance of whichever class the{ code is currently public void on. Create(Bundle state) in! super. on. Create(saved. Instance. State); <Table. Row Table. Layout layout = new Table. Layout(this); android: layout_width="wrap_content" android: layout_height="wrap_content" > Table. Row row = new Table. Row(this); <Text. View android: layout_width="wrap_content" Text. View tv = new Text. View(this); android: layout_height="wrap_content" tv. set. Text("Medium Text"); android: text="Medium Text"/> </Table. Row> row. add. View(tv); <Table. Row layout. add. View(row); android: layout_width="wrap_content" android: layout_height="wrap_content" > row = new Table. Row(this); </Table. Row> layout. add. View(row); </Table. Layout> set. Content. View(layout); }

Building the UI Dynamically <Table. Layout android: layout_width="match_parent" android: layout_height="match_parent" > In this case, it would refer to your Activity (sincestate) you know that public void on. Create(Bundle { on. Create() is a method of an Activity) super. on. Create(saved. Instance. State); <Table. Row Table. Layout layout = new Table. Layout(this); android: layout_width="wrap_content" android: layout_height="wrap_content" > Table. Row row = new Table. Row(this); <Text. View android: layout_width="wrap_content" Text. View tv = new Text. View(this); android: layout_height="wrap_content" tv. set. Text("Medium Text"); android: text="Medium Text"/> </Table. Row> row. add. View(tv); <Table. Row layout. add. View(row); android: layout_width="wrap_content" android: layout_height="wrap_content" > row = new Table. Row(this); </Table. Row> layout. add. View(row); </Table. Layout> set. Content. View(layout); }

Building the UI Dynamically You can't add the same child element twice! This is an easy way to get a Force Close

Layout. Inflater • • instantiates a layout XML file into its corresponding View objects use get. Layout. Inflater()

Layout. Inflater @Override public void on. Create(Bundle state) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Layout. Inflater inflater = get. Layout. Inflater(); } set. Content. View( inflater. inflate(R. layout. main, null)); }

Layout. Inflater @Override public void on. Create(Bundle state) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Layout. Inflater inflater = get. Layout. Inflater(); } set. Content. View( inflater. inflate(R. layout. main, null)); } Gives you the same result

Layout. Inflater @Override public void on. Create(Bundle state) { super. on. Create(saved. Instance. State); Second argument is of Layout. Inflater inflater = type View. Group get. Layout. Inflater(); set. Content. View(R. layout. main); } set. Content. View( inflater. inflate(R. layout. main, null)); }

Layout. Inflater @Override public void on. Create(Bundle state) { super. on. Create(saved. Instance. State); Use it if you want to Layout. Inflater inflater = argument embed the first into the second argument get. Layout. Inflater(); set. Content. View(R. layout. main); } set. Content. View( inflater. inflate(R. layout. main, null)); }

Fragments

Fragments • • A Fragment represents a behavior or a portion of user interface in an Activity. Add multiple fragments to a screen to avoid switching activities Fragments have their own lifecycle, state, and back stack Fragments require API Level 11 or greater

Fragments - Lifecycle A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. • http: //developer. android. com/guide/compone nts/fragments. html on. Create. View() is the one we'll be focusing on for now. . .

Fragments - Steps for Creating One 1. You must create a subclass of Fragment 1. Fragment, List. Fragment, etc 2. You must implement on. Create. View() 1. on. Create. View() must return a View i. e. @Override public View on. Create. View( /* required args */)

Fragments - on. Create. View() There are two ways to return a view in your implementation of on. Create. View()

Fragments - on. Create. View() 1. Use the Layout. Inflater argument to instantiate a predefined layout XML file e. g. public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { return inflater. inflate(R. layout. example_fragment, container, false); } }

Fragments - on. Create. View() 1. Use the Layout. Inflater argument to instantiate a predefined layout XML file e. g. extend Fragment, not Activity public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { return inflater. inflate(R. layout. example_fragment, container, false); } }

Fragments - on. Create. View() 1. Use the Layout. Inflater argument to instantiate a predefined layout XML file e. g. The XML layout public class My. Fragment extends Fragment { file to instantiate public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { return inflater. inflate(R. layout. example_fragment, container, false); } }

Fragments - on. Create. View() 1. Use the Layout. Inflater argument to instantiate a predefined layout XML file e. g. public class My. Fragment extends Fragment { The View. Group to insert it into public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { return inflater. inflate(R. layout. example_fragment, container, false); } }

Fragments - on. Create. View() 1. Use the Layout. Inflater argument to instantiate a predefined layout XML file e. g. public class My. Fragment extends Fragment { Passing true would create a redundant View. Group, so pass false for now public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { return inflater. inflate(R. layout. example_fragment, container, false); } }

Fragments - on. Create. View() 2. Build a UI dynamically and return the root View public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { Scroll. View scroller = new Scroll. View(get. Activity()); Text. View text = new Text. View(get. Activity()); scroller. add. View(text); text. set. Text("Sample Text"); return scroller; } }

Fragments - on. Create. View() 2. Build a UI dynamically and return the root View public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) You've seen most of this before! { Scroll. View scroller = new Scroll. View(get. Activity()); Text. View text = new Text. View(get. Activity()); scroller. add. View(text); text. set. Text("Sample Text"); return scroller; } }

Fragments - on. Create. View() 2. Build a UI dynamically and return the root View public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle Last time we used the state) keyword this to get the { Context Scroll. View scroller = new Scroll. View(get. Activity()); Text. View text = new Text. View(get. Activity()); scroller. add. View(text); text. set. Text("Sample Text"); return scroller; } }

Fragments - on. Create. View() 2. Build a UI dynamically and return the root View public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle Since we're in a state) Fragment here, and not { an Activity. . . Scroll. View scroller = new Scroll. View(get. Activity()); Text. View text = new Text. View(get. Activity()); scroller. add. View(text); text. set. Text("Sample Text"); return scroller; } }

Fragments - on. Create. View() 2. Build a UI dynamically and return the root View public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle We can get the controlling Activity from state) within the Fragment { using get. Activity() Scroll. View scroller = new Scroll. View(get. Activity()); Text. View text = new Text. View(get. Activity()); scroller. add. View(text); text. set. Text("Sample Text"); return scroller; } }

Fragments - on. Create. View() 2. Build a UI dynamically and return the root View public class My. Fragment extends Fragment { public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle This is how we get the Context. state) Alternatively, we can say { get. Activity(). get. Application. Context() Scroll. View scroller = new Scroll. View(get. Activity()); Text. View text = new Text. View(get. Activity()); scroller. add. View(text); text. set. Text("Sample Text"); return scroller; } }

Fragments - Adding a Fragment There are 2 ways to add a Fragment

Fragments - Adding a Fragment 1. Declare the Fragment inside of the Activity's layout file <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: orientation="horizontal" android: layout_width="match_parent" android: layout_height="match_parent"> <fragment android: name="edu. fsu. cs. mobile. example. My. Fragment" android: id="@+id/list" android: tag="list_fragment" android: layout_weight="1" android: layout_width="match_parent" android: layout_height="match_parent" /> <fragment android: name="edu. fsu. cs. mobile. example. My. Other. Fragment" android: id="@+id/viewer" android: tag="viewer_fragment" android: layout_weight="2" android: layout_width="match_parent" android: layout_height="match_parent" /> </Linear. Layout>

Fragments - Adding a Fragment 1. Declare the Fragment inside of the Activity's layout file <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: orientation="horizontal" <fragment> android: layout_width="match_parent" android: layout_height="match_parent"> <fragment android: name="edu. fsu. cs. mobile. example. My. Fragment" android: id="@+id/list" android: tag="list_fragment" android: layout_weight="1" android: layout_width="match_parent" android: layout_height="match_parent" /> <fragment android: name="edu. fsu. cs. mobile. example. My. Other. Fragment" android: id="@+id/viewer" android: tag="viewer_fragment" android: layout_weight="2" android: layout_width="match_parent" android: layout_height="match_parent" /> </Linear. Layout>

Fragments - Adding a Fragment 1. Declare the Fragment inside of the Activity's layout file <? xml version="1. 0" encoding="utf-8"? > absolute reference to <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" your Fragment class, android: orientation="horizontal" which includes the android: layout_width="match_parent"package name android: layout_height="match_parent"> <fragment android: name="edu. fsu. cs. mobile. example. My. Fragment" android: id="@+id/list" android: tag="list_fragment" android: layout_weight="1" android: layout_width="match_parent" android: layout_height="match_parent" /> <fragment android: name="edu. fsu. cs. mobile. example. My. Other. Fragment" android: id="@+id/viewer" android: tag="viewer_fragment" android: layout_weight="2" android: layout_width="match_parent" android: layout_height="match_parent" /> </Linear. Layout>

Fragments - Adding a Fragment 1. Declare the Fragment inside of the Activity's layout file <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: orientation="horizontal" android: layout_width="match_parent" android: layout_height="match_parent"> Fragment id <fragment android: name="edu. fsu. cs. mobile. example. My. Fragment" android: id="@+id/list" android: tag="list_fragment" android: layout_weight="1" android: layout_width="match_parent" android: layout_height="match_parent" /> <fragment android: name="edu. fsu. cs. mobile. example. My. Other. Fragment" android: id="@+id/viewer" android: tag="viewer_fragment" android: layout_weight="2" android: layout_width="match_parent" android: layout_height="match_parent" /> </Linear. Layout>

Fragments - Adding a Fragment 1. Declare the Fragment inside of the Activity's layout file <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: orientation="horizontal" android: layout_width="match_parent" android: layout_height="match_parent"> <fragment android: name="edu. fsu. cs. mobile. example. My. Fragment" Fragment TAG android: id="@+id/list" android: tag="list_fragment" android: layout_weight="1" android: layout_width="match_parent" android: layout_height="match_parent" /> <fragment android: name="edu. fsu. cs. mobile. example. My. Other. Fragment" android: id="@+id/viewer" android: tag="viewer_fragment" android: layout_weight="2" android: layout_width="match_parent" android: layout_height="match_parent" /> </Linear. Layout>

Fragments - Adding a Fragment 1. Declare the Fragment inside of the Activity's layout file This works because you return a View object in your on. Create. View() method for the specified Fragment The View then gets embedded into the UI

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="fill_parent" android: layout_height="fill_parent" android: orientation="vertical" > <Text. View Use a Frame. Layout as a container for your android: layout_width="fill_parent" Fragment android: layout_height="wrap_content" /> <Frame. Layout android: id="@+id/fragment_container" android: layout_width="match_parent" android: layout_height="match_parent" > </Frame. Layout> </Linear. Layout>

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="fill_parent" android: layout_height="fill_parent" android: orientation="vertical" > <Text. View android: layout_width="fill_parent" Remember the id of the android: layout_height="wrap_content" container. . . /> <Frame. Layout android: id="@+id/fragment_container" android: layout_width="match_parent" android: layout_height="match_parent" > </Frame. Layout> </Linear. Layout>

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="fill_parent" android: layout_height="fill_parent" android: orientation="vertical" > <Text. View android: layout_width="fill_parent" android: layout_height="wrap_content" /> <Frame. Layout android: id="@+id/fragment_container" android: layout_width="match_parent" android: layout_height="match_parent" > </Frame. Layout> </Linear. Layout> This is main. xml layout file

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override This XML layout file public void on. Create(Bundle saved. Instance. State) { contains the Frame. Layout container super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); Fragment. Manager allows you to interact with Fragments that set. Content. View(R. layout. main); are in an Activity Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Transaction allows you to perform operations on Fragments (add, remove, replace) Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Our Fragment doesn't exist at this point (was not specified in Fragment. Manager = get. Fragment. Manager(); XML), let's createmanager an instance of it Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Add our Fragment to the Frame. Layout containertrans = manager. begin. Transaction(); Fragment. Transaction My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); The unique TAG for your Fragment. Manager manager = get. Fragment. Manager(); Fragment, so that you can Fragment. Transaction trans = manager. begin. Transaction(); reference it later My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Adding a Fragment 2. Programmatically add the Fragment to an existing View. Group public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); Update the UI My. Fragment fragment = new My. Fragment(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Passing data to You will often want to pass data from your Activity to your Fragment. You can do this several ways. From within the Activity. . . public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance) { super. on. Create(saved. Instance); set. Content. View(R. layout. main); My. Fragment fragment = new My. Fragment(); fragment. my_custom_field = value; fragment. set. My. Custom. Field(value); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Passing data to You will often want to pass data from your Activity to your Fragment. You can do this several ways. From within the Activity. . .

Fragments - Passing data to You will often want to pass data from your Activity to your Fragment. You can do this several ways. From within the Activity. . . public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance) { You will need to have my_custom_field as super. on. Create(saved. Instance); a public field in your Fragment, and set. My. Custom. Field() as a public method set. Content. View(R. layout. main); My. Fragment fragment = new My. Fragment(); fragment. my_custom_field = value; fragment. set. My. Custom. Field(value); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Passing data to Alternatively, you can use Fragments set. Arguments(Bundle) and get. Arguments() methods.

Fragments - Passing data to Alternatively, you can use Fragments set. Arguments(Bundle) and get. Arguments() methods. public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance) { super. on. Create(saved. Instance); set. Content. View(R. layout. main); My. Fragment fragment = new My. Fragment(); Bundle extras = new Bundle(); extras. put. Int("selected", 15); fragment. set. Arguments(extras); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Passing data to Alternatively, you can use Fragments set. Arguments(Bundle) and get. Arguments() methods. public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance) { super. on. Create(saved. Instance); You've seen Bundle before! set. Content. View(R. layout. main); My. Fragment fragment = new My. Fragment(); Bundle extras = new Bundle(); extras. put. Int("selected", 15); fragment. set. Arguments(extras); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Passing data to Alternatively, you can use Fragments set. Arguments(Bundle) and get. Arguments() methods. public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance) { super. on. Create(saved. Instance); set. Content. View(R. layout. main); set. Arguments() takes a Bundle as fragment an argument. My. Fragment = new My. Fragment(); This is a standard Fragment method Bundle extras = new Bundle(); extras. put. Int("selected", 15); fragment. set. Arguments(extras); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); trans. add(R. id. fragment_container, fragment, "my_fragment"); trans. commit(); } }

Fragments - Passing data to Then, in your Fragment, you can get the arguments using get. Arguments()

Fragments - Passing data to Then, in your Fragment, you can get the arguments using get. Arguments() public class My. Fragment extends Fragment{ @Override public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { Bundle args = get. Arguments(); . . . return new Text. View(get. Activity()); } @Override public void on. Activity. Created(Bundle state) { super. on. Activity. Created(state); Bundle args = get. Arguments(); } }

Fragments - Passing data to Then, in your Fragment, you can get the arguments using get. Arguments() public class My. Fragment extends Fragment{ @Override Bundle args and Bundle state are two separate Bundles. Ignore Bundle state for now public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { Bundle args = get. Arguments(); . . . return new Text. View(get. Activity()); } @Override public void on. Activity. Created(Bundle state) { super. on. Activity. Created(state); Bundle args = get. Arguments(); } }

Fragments - Passing data to Then, in your Fragment, you can get the arguments using get. Arguments() public class My. Fragment extends Fragment{ @Override get the arguments that were passed by the Activity public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { Bundle args = get. Arguments(); . . . return new Text. View(get. Activity()); } @Override public void on. Activity. Created(Bundle state) { super. on. Activity. Created(state); Bundle args = get. Arguments(); } }

Fragments - Passing data to Then, in your Fragment, you can get the arguments using get. Arguments() public class My. Fragment extends Fragment{ @Override public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { on. Create. View must return a Bundle args = get. Arguments(); View. . . return new Text. View(get. Activity()); } @Override public void on. Activity. Created(Bundle state) { super. on. Activity. Created(state); Bundle args = get. Arguments(); } }

Fragments - Passing data to Then, in your Fragment, you can get the arguments using get. Arguments() public class My. Fragment extends Fragment{ @Override public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { Bundle args = get. Arguments(); . . . return new Text. View(get. Activity()); You may need to get the arguments at this point in the Lifecycle instead } @Override public void on. Activity. Created(Bundle state) { super. on. Activity. Created(state); Bundle args = get. Arguments(); } }

Fragments - Passing data to Then, in your Fragment, you can get the arguments using get. Arguments() public class My. Fragment extends Fragment{ @Override public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle state) { Bundle args = get. Arguments(); . . . return new Text. View(get. Activity()); This is usually if you extended List. Fragment instead } @Override public void on. Activity. Created(Bundle state) { super. on. Activity. Created(state); Bundle args = get. Arguments(); } }

Fragments - How to Reference • • A Fragment is not a View! You can't call find. View. By. Id() in order to reference a Fragment within an Activity use get. Fragment. Manager(). find. Fragment. By. Id() or get. Fragment. Manager(). find. Fragment. By. Tag() instead of course, an instance of your Fragment Manager needs to already be a part of your Activity Fragment. Manager manager = get. Fragment. Manager(); My. Fragment fragment = (My. Fragment) manager. find. Fragment. By. Id(R. id. fragment_id); My. Fragment fragment = (My. Fragment) manager. find. Fragment. By. Tag("my_fragment");

Fragments - Removing a Fragment You can also remove Fragments from the UI

Fragments - Removing a Fragment You can also replace one Fragment with another public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = manager. find. Fragment. By. Id(R. id. my_fragment); trans. remove(fragment); trans. commit(); } }

Fragments - Removing a Fragment You can also replace one Fragment with another public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); You can only remove it if it was added somehow. This gets a reference to a Fragment that Fragment. Manager manager = get. Fragment. Manager(); was already added. Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = manager. find. Fragment. By. Id(R. id. my_fragment); trans. remove(fragment); trans. commit(); } }

Fragments - Removing a Fragment You can also replace one Fragment with another public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Remove the Fragment from Fragment. Transaction trans = manager. begin. Transaction(); this Activity My. Fragment fragment = manager. find. Fragment. By. Id(R. id. my_fragment); trans. remove(fragment); trans. commit(); } }

Fragments - Replacing a Fragment You can replace a Fragment by removing an existing Fragment and adding an new one. But an easier way to do this is to call replace() instead

Fragments - Replacing a Fragment You can also replace Fragments public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = new My. Fragment(); trans. replace(R. id. fragment_container, fragment); trans. commit(); } }

Fragments - Replacing a Fragment You can also replace Fragments public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Reference the Fragment (if you don't already have a reference Fragment. Manager manager = get. Fragment. Manager(); to it) Fragment. Transaction trans = manager. begin. Transaction(); My. Fragment fragment = new My. Fragment(); trans. replace(R. id. fragment_container, fragment); trans. commit(); } }

Fragments - Replacing a Fragment You can also replace Fragments public class My. Activity extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Fragment. Manager manager = get. Fragment. Manager(); Remove Fragment from the Fragment. Transaction trans = manager. begin. Transaction(); Activity My. Fragment fragment = new My. Fragment(); trans. replace(R. id. fragment_container, fragment); trans. commit(); } }

Fragments - Reference the Activity • You can reference the controlling Activity from within the Fragment e. g, in the on. Create. View() method of the Fragment class: View list. View = get. Activity(). find. View. By. Id(R. id. list);

Permissions • no application, by default, has permission to perform any operations that would adversely impact o other applications o the operating system o the user • How does the system know that this app uses these permissions?

Using Permissions requested by your application must be specified in the Android Manifest file. For example, here we request permission to access the Internet <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. android. app. myapp" > <uses-permission android: name="android. permission. INTERNET" />. . . </manifest>

How to Request Permissions Your app doesn't request to use any permissions by default To request to use a permission 1. Open Android. Manifest. xml 2. Click the Permissions tab at the bottom of the Editor 3. Click Add. . . 4. Select Uses Permission 5. Select the permission you want to request from the drop-down list 6. Save Android. Manifest. xml

Permissions - Web. View Example A Web. View is a View that allows you to load and display web page content to the user

Permissions - Web. View Example @Override public void on. Create(Bundle saved. Instance. State) { Of course, this is in our XML layout file somewhere Web. View webview = (Web. View) find. View. By. Id(R. id. web. View 1); webview. set. Web. View. Client(new Web. View. Client() { @Override public boolean should. Override. Url. Loading(Web. View view, String url) { return false; } }); webview. load. Url("http: //www. google. com"); }

Permissions - Web. View Example @Override public voidofon. Create(Bundle This block code prevents the saved. Instance. State) { system from opening webview the URL in= (Web. View) find. View. By. Id(R. id. web. View 1); Web. View the default browser webview. set. Web. View. Client(new Web. View. Client() { @Override public boolean should. Override. Url. Loading(Web. View view, String url) { return false; } }); webview. load. Url("http: //www. google. com"); }

Permissions - Web. View Example @Override public void on. Create(Bundle saved. Instance. State) { Web. View webview = (Web. View) find. View. By. Id(R. id. web. View 1); webview. set. Web. View. Client(new Web. View. Client() { It has to return false, in order to load the URL in our webview @Override instead of the default browser public boolean should. Override. Url. Loading(Web. View view, String url) { return false; } }); webview. load. Url("http: //www. google. com"); }

Permissions - Web. View Example @Override public void on. Create(Bundle saved. Instance. State) { Web. View webview = (Web. View) find. View. By. Id(R. id. web. View 1); webview. set. Web. View. Client(new Web. View. Client() { @Override public boolean should. Override. Url. Loading(Web. View view, String url) { The Web. View will not load the false; URL if return you don't have INTERNET permissions!, i. e. } }); android. permissions. INTERNET webview. load. Url("http: //www. google. com"); }

Permissions There are many more permissions that can be used You can find a list of them here

Android Application Components 1. Activity 2. Broadcast 3. Content Provider 4. Service Receiver

Broadcast Receiver • • • � a component that responds to system wide broadcast announcements o � Incoming SMS, MMS, Email, phone call o Battery low, Screen has turned off, or a picture was captured. � think of a Broadcast receiver as a “gateway” to the other components. intended to very little work o Depending on the incoming event to the receiver, it could start a Service or Activity.

Broadcast Receiver To add a new Broadcast Receiver • • open the Android. Manifest. xml file click on the Application tab at the bottom of the window under the Application Nodes section, click Add. . . select Receiver in the Attributes for Receiver section, click on the Name* link Enter the name of your Broadcast Receiver o e. g. "My. Broadcast. Receiver" Finish

Broadcast Receiver - Manifest File If you view Android. Manifest. xml in the XML editor, you should see something like this <receiver android: name="My. Broadcast. Receiver"></receiver>

Broadcast Receiver Your new Broadcast Receiver file should look something like this public class My. Broadcast. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { } }

Broadcast Receiver public class Event listener for when a broadcast has been My. Broadcast. Receiver received. extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { } }

Broadcast Receiver How do you know which public class My. Broadcast. Receiver broadcasts to listen for? extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { } }

Registering Broadcast. Receiver There are two ways to register a Broadcast. Receiver to listen for a broadcast 1. Adding an <intent-filter> to the Manifest file 2. Programmatically

Registering Broadcast. Receiver 1. To add an <intent-filter> to the Manifest and listen for a Broadcast. . . 1. Open Android. Manifest. xml and click on the Application Tab at the bottom of the Window 2. Select your Broadcast. Receiver 3. Click Add. . . 4. Make sure "Create new element in the selected element. . . " is selected, then choose Intent Filter 5. Click Add. . . 6. Choose Action 7. Select the appropriate permission in the Name drop-down list a. e. g. android. intent. action. TIME_SET 8. Save Android. Manifest. xml

Registering Broadcast. Receiver Alternatively, if you view Android. Manifest. xml in the XML editor, you should see something like this <receiver android: name="My. Broadcast. Receiver"> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag <receiver android: name="My. Broadcast. Receiver"> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag Place the cursor here and press Ctrl + Space <receiver android: name="My. Broadcast. Receiver"> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag Select <intent-filter> <receiver android: name="My. Broadcast. Receiver"> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag <receiver android: name="My. Broadcast. Receiver"> <intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag an <intent-filter> <receiver android: name="My. Broadcast. Receiver"> describes what this receiver should listen for <intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag Place the cursor here <receiver and press android: name="My. Broadcast. Receiver"> Ctrl + Space <intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag <receiver android: name="My. Broadcast. Receiver"> Select <action/> <intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag <receiver Press Ctrl + Space android: name="My. Broadcast. Receiver"> again <intent-filter> <action </intent-filter> </receiver> />

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag Select android: name <receiver android: name="My. Broadcast. Receiver"> <intent-filter> <action </intent-filter> </receiver> />

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag Let's try receiving a broadcast for when the <receiver android: name="My. Broadcast. Receiver"> screen has been turned on <intent-filter> <action android: name=" "/> </intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag Press Ctrl + Space <receiver android: name="My. Broadcast. Receiver"> again <intent-filter> <action android: name=" "/> </intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag Select android. intent. action. TIME_SET <receiver android: name="My. Broadcast. Receiver"> <intent-filter> <action android: name=" "/> </intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag <receiver android: name="My. Broadcast. Receiver"> <intent-filter> <action android: name="android. intent. action. TIME_SET"/> </intent-filter> </receiver>

Registering Broadcast. Receiver We need to tell the system which broadcast(s) we want to listen for. First we need to add an <intent-filter> tag <receiver android: name="My. Broadcast. Receiver"> <intent-filter> <action android: name="android. intent. action. TIME_SET"/> </intent-filter> </receiver>

Registering Broadcast. Receiver 2. To register a Broadcast. Receiver programmatically. . .

Registering Broadcast. Receiver public class Main. Activity extends Activity { My. Broadcast. Receiver receiver; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); receiver = new My. Broadcast. Receiver(); Intent. Filter filter = new Intent. Filter(Intent. ACTION_SCREEN_ON); register. Receiver(receiver, filter); } }

Registering Broadcast. Receiver public class Main. Activity extends Activity { My. Broadcast. Receiver receiver; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); Create a new instance set. Content. View(R. layout. main); of your receiver = new My. Broadcast. Receiver(); Intent. Filter filter = new Intent. Filter(Intent. ACTION_SCREEN_ON); register. Receiver(receiver, filter); } }

Registering Broadcast. Receiver public class Main. Activity extends Activity { My. Broadcast. Receiver receiver; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); Create an intent filter receiver = new My. Broadcast. Receiver(); Intent. Filter filter = new Intent. Filter(Intent. ACTION_SCREEN_ON); register. Receiver(receiver, filter); } }

Registering Broadcast. Receiver public class Main. Activity extends Activity { My. Broadcast. Receiver receiver; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); receiver = new My. Broadcast. Receiver(); The receiver is now Intent. Filter filter = new Intent. Filter(Intent. ACTION_SCREEN_ON); registered register. Receiver(receiver, filter); } }

Broadcast. Receiver - on. Receive() Now let's look at our Broadcast. Receiver public class My. Broadcast. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { Intent my. Intent = new Intent(context, Activity. To. Be. Launched. class); Toast. make. Text( context, "Broadcast Received!", Toast. LENGTH_LONG). show(); my. Intent. add. Flags(Intent. FLAG_ACTIVITY_NEW_TASK); context. start. Activity(my. Intent); } }

Broadcast. Receiver - on. Receive() public class My. Broadcast. Receiver extends Broadcast. Receiver { @Override public At this point, the void on. Receive(Context broadcast has been received context, Intent intent) { Intent my. Intent = new Intent(context, Activity. To. Be. Launched. class); Toast. make. Text( context, "Broadcast Received!", Toast. LENGTH_LONG). show(); my. Intent. add. Flags(Intent. FLAG_ACTIVITY_NEW_TASK); context. start. Activity(my. Intent); }

Broadcast. Receiver - on. Receive() public class My. Broadcast. Receiver extends Broadcast. Receiver { @Override Let's say we want to launch a new Activity public void on. Receive(Context context, Intent intent) { when we receive the broadcast Intent my. Intent = new Intent(context, Activity. To. Be. Launched. class); Toast. make. Text( context, "Broadcast Received!", Toast. LENGTH_LONG). show(); my. Intent. add. Flags(Intent. FLAG_ACTIVITY_NEW_TASK); context. start. Activity(my. Intent); }

Broadcast. Receiver - on. Receive() public class My. Broadcast. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { And we want to show a Intent my. Intent = new Intent(context, Activity. To. Be. Launched. class); Toast. make. Text( context, "Broadcast Received!", Toast. LENGTH_LONG). show(); my. Intent. add. Flags(Intent. FLAG_ACTIVITY_NEW_TASK); context. start. Activity(my. Intent); } }

Broadcast. Receiver - on. Receive() public class My. Broadcast. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { Intent my. Intent = new Intent(context, Activity. To. Be. Launched. class); Toast. make. Text( context, "Broadcast Received!", You need to add flags Toast. LENGTH_LONG). show(); to your intent this time my. Intent. add. Flags(Intent. FLAG_ACTIVITY_NEW_TASK); context. start. Activity(my. Intent); } }

Broadcast. Receiver - on. Receive() public class My. Broadcast. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { Intent my. Intent = new Intent(context, Activity. To. Be. Launched. class); Toast. make. Text( context, "Broadcast. Calling Received!", start. Activity() from outside of an Activity context requires the Toast. LENGTH_LONG). show(); FLAG_ACTIVITY_NEW_TASK flag my. Intent. add. Flags(Intent. FLAG_ACTIVITY_NEW_TASK); context. start. Activity(my. Intent); } }

Unregistering Broadcast. Receiver Sometimes after you start listening for a Broadcast, you may want to STOP listening for it at some point To UNregister a Broadcast. Receiver programmatically. . .

Unregistering Broadcast. Receiver public class Main. Activity extends Activity { @Override public void on. Pause() { unregister. Receiver(receiver); super. on. Pause(); } }

Unregistering Broadcast. Receiver public class Main. Activity extends Activity { You will want to unregister before @Override calling a super. on. Something() method, you may public voidotherwise on. Pause() { get Force Close unregister. Receiver(receiver); super. on. Pause(); } }

Unregistering Broadcast. Receiver If you register a Broadcast. Receiver programmatically, you should unregister it before your Activity is destroyed! otherwise you will have a "leaked" receiver Please call unregister. Receiver in • • on. Pause() o on. Stop() o or on. Destroy() o This is important

Code Examples • • • Broadcast. Receiver. Example Simple. Fragment. Using. Xml Simple. Fragment. Programmatically Fragment. Activity. Reference. Example Fragment. Transition. Example Layout. Inflater. Example Permissions. Example Complex. Fragments. Example Dynamic. Ui. Example

References • The Busy Coder's Guide to Android Development - Mark Murphy • Android Developers • The Mobile Lab at Florida State University • Code Examples
- Slides: 123