Android development - A Sample Twitter App Part 2

flosssnailsMobile - Wireless

Dec 10, 2013 (3 years and 8 months ago)

92 views

Android development - A Sample Twitter App
Part 2
Brink van der Merwe
24 February 2011
Je Sharkey talk about his app CompareEverywhere
http://developer.android.com/videos/index.html#v=V2-jLK0aNiI
In this lecture we show how to add the functionality to our micro
blogging app to make it possible for a user to specify his/her
preferences.
Preferences
 Preferences are user-specic settings for an application.

Consist of some conguration data as well as a user interface
to manipulate that data.

Form UI point of view preferences can be simple text values,
checkboxes,selections from a pull-down menu,or similar.

From data point of view,preferences is a collection of
name-value pairs
Preferences
 Preferences are user-specic settings for an application.

Consist of some conguration data as well as a user interface
to manipulate that data.

Form UI point of view preferences can be simple text values,
checkboxes,selections from a pull-down menu,or similar.

From data point of view,preferences is a collection of
name-value pairs
Preferences
 Preferences are user-specic settings for an application.

Consist of some conguration data as well as a user interface
to manipulate that data.

Form UI point of view preferences can be simple text values,
checkboxes,selections from a pull-down menu,or similar.

From data point of view,preferences is a collection of
name-value pairs
Preferences
 Preferences are user-specic settings for an application.

Consist of some conguration data as well as a user interface
to manipulate that data.

Form UI point of view preferences can be simple text values,
checkboxes,selections from a pull-down menu,or similar.

From data point of view,preferences is a collection of
name-value pairs
Preferences
The steps to creating preferences for our application will be to:
1
Create Preference resource le prefs.xml.
2
Implement PrefsActivity.java le that in ates that resource
le.
3
Register this new activity with the AndroidManifest.xml le.
4
Provide a way to start that activity from the rest of the
application.
Preferences
The steps to creating preferences for our application will be to:
1
Create Preference resource le prefs.xml.
2
Implement PrefsActivity.java le that in ates that resource
le.
3
Register this new activity with the AndroidManifest.xml le.
4
Provide a way to start that activity from the rest of the
application.
Preferences
The steps to creating preferences for our application will be to:
1
Create Preference resource le prefs.xml.
2
Implement PrefsActivity.java le that in ates that resource
le.
3
Register this new activity with the AndroidManifest.xml le.
4
Provide a way to start that activity from the rest of the
application.
Preferences
The steps to creating preferences for our application will be to:
1
Create Preference resource le prefs.xml.
2
Implement PrefsActivity.java le that in ates that resource
le.
3
Register this new activity with the AndroidManifest.xml le.
4
Provide a way to start that activity from the rest of the
application.
Preferences

We start by creating prefs.xml in/res/xml - a resource le
that outlines what our preference screen will look like.
 Typically,you will care about the following attributes:
 Key:a unique identier for each preference item.
 Title:the name of this preference that user will see.
 Summary:A short description of this preference item.
Preferences

We start by creating prefs.xml in/res/xml - a resource le
that outlines what our preference screen will look like.
 Typically,you will care about the following attributes:
 Key:a unique identier for each preference item.
 Title:the name of this preference that user will see.
 Summary:A short description of this preference item.
Preferences

We start by creating prefs.xml in/res/xml - a resource le
that outlines what our preference screen will look like.
 Typically,you will care about the following attributes:
 Key:a unique identier for each preference item.
 Title:the name of this preference that user will see.
 Summary:A short description of this preference item.
Preferences

We start by creating prefs.xml in/res/xml - a resource le
that outlines what our preference screen will look like.
 Typically,you will care about the following attributes:
 Key:a unique identier for each preference item.
 Title:the name of this preference that user will see.
 Summary:A short description of this preference item.
Preferences

We start by creating prefs.xml in/res/xml - a resource le
that outlines what our preference screen will look like.
 Typically,you will care about the following attributes:
 Key:a unique identier for each preference item.
 Title:the name of this preference that user will see.
 Summary:A short description of this preference item.
res/xml/prefs.xml
<?xml version="1.0"encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="@string/titleUsername"
android:summary="@string/summaryUsername"android:key="username"></EditTextPreference>
<EditTextPreference android:title="@string/titlePassword"
android:password="true"android:summary="@string/summaryPassword"
android:key="password"></EditTextPreference>
<EditTextPreference android:title="@string/titleApiRoot"
android:summary="@string/summaryApiRoot"android:key="apiRoot"></EditTextPreference>
</PreferenceScreen>
PrefsActivity
 Now that we have the preferences dened in their own XML
resource le,we can create the activity to display these
preferences.
PrefsActivity.java
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class PrefsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
Update Manifest File
Whenever we create one of these main building blocks (Activities,
Services,Broadcast Receivers,Content Providers) we need to
dene them in AndroidManifest.xml le.
AndroidManifest.xml
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"android:versionName="1.0"package="com.marakana.yamba2">
<application android:icon="@drawable/icon"android:label="@string/app_name">
<activity android:name=".StatusActivity"android:label="@string/titleStatus">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".PrefsActivity"android:label="@string/titlePrefs"/> <!-- 1 -->
</application>
<uses-sdk android:minSdkVersion="4"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Options Menu
 Options menu is an Android user interface component that
provides standardized menus to applications.

The menus appear at the bottom of the screen when the user
presses the Menu button on the device.
 To add support for an options menu to an application,we
need to do the following:
1
Create a menu.xml resource where we specify what the menu
consists of.
2
Add onCreateOptionsMenu() to the activity that we want to
provide this menu for.This is where we in ate the menu.xml
resource.
3
Provide handling of menu events in onOptionsItemSelected().
Options Menu
 Options menu is an Android user interface component that
provides standardized menus to applications.

The menus appear at the bottom of the screen when the user
presses the Menu button on the device.
 To add support for an options menu to an application,we
need to do the following:
1
Create a menu.xml resource where we specify what the menu
consists of.
2
Add onCreateOptionsMenu() to the activity that we want to
provide this menu for.This is where we in ate the menu.xml
resource.
3
Provide handling of menu events in onOptionsItemSelected().
Options Menu
 Options menu is an Android user interface component that
provides standardized menus to applications.

The menus appear at the bottom of the screen when the user
presses the Menu button on the device.
 To add support for an options menu to an application,we
need to do the following:
1
Create a menu.xml resource where we specify what the menu
consists of.
2
Add onCreateOptionsMenu() to the activity that we want to
provide this menu for.This is where we in ate the menu.xml
resource.
3
Provide handling of menu events in onOptionsItemSelected().
Options Menu
 Options menu is an Android user interface component that
provides standardized menus to applications.

The menus appear at the bottom of the screen when the user
presses the Menu button on the device.
 To add support for an options menu to an application,we
need to do the following:
1
Create a menu.xml resource where we specify what the menu
consists of.
2
Add onCreateOptionsMenu() to the activity that we want to
provide this menu for.This is where we in ate the menu.xml
resource.
3
Provide handling of menu events in onOptionsItemSelected().
Options Menu
 Options menu is an Android user interface component that
provides standardized menus to applications.

The menus appear at the bottom of the screen when the user
presses the Menu button on the device.
 To add support for an options menu to an application,we
need to do the following:
1
Create a menu.xml resource where we specify what the menu
consists of.
2
Add onCreateOptionsMenu() to the activity that we want to
provide this menu for.This is where we in ate the menu.xml
resource.
3
Provide handling of menu events in onOptionsItemSelected().
Options Menu
 Options menu is an Android user interface component that
provides standardized menus to applications.

The menus appear at the bottom of the screen when the user
presses the Menu button on the device.
 To add support for an options menu to an application,we
need to do the following:
1
Create a menu.xml resource where we specify what the menu
consists of.
2
Add onCreateOptionsMenu() to the activity that we want to
provide this menu for.This is where we in ate the menu.xml
resource.
3
Provide handling of menu events in onOptionsItemSelected().
Menu Resource

We start by dening the menu in an XML resource.

Some attributes:
1
Id:the unique identier of this resource.This identier is
typically of form @+id/someId
2
Title:The title of this menu as it will appear when displayed.
3
Icon:The icon that shows along with the menu items title.
While it is not required,it is a very useful visual cue from a
usability point of view.
Menu Resource

We start by dening the menu in an XML resource.

Some attributes:
1
Id:the unique identier of this resource.This identier is
typically of form @+id/someId
2
Title:The title of this menu as it will appear when displayed.
3
Icon:The icon that shows along with the menu items title.
While it is not required,it is a very useful visual cue from a
usability point of view.
Menu Resource

We start by dening the menu in an XML resource.

Some attributes:
1
Id:the unique identier of this resource.This identier is
typically of form @+id/someId
2
Title:The title of this menu as it will appear when displayed.
3
Icon:The icon that shows along with the menu items title.
While it is not required,it is a very useful visual cue from a
usability point of view.
Menu Resource

We start by dening the menu in an XML resource.

Some attributes:
1
Id:the unique identier of this resource.This identier is
typically of form @+id/someId
2
Title:The title of this menu as it will appear when displayed.
3
Icon:The icon that shows along with the menu items title.
While it is not required,it is a very useful visual cue from a
usability point of view.
Menu Resource

We start by dening the menu in an XML resource.

Some attributes:
1
Id:the unique identier of this resource.This identier is
typically of form @+id/someId
2
Title:The title of this menu as it will appear when displayed.
3
Icon:The icon that shows along with the menu items title.
While it is not required,it is a very useful visual cue from a
usability point of view.
res/menu/menu.xml
<?xml version="1.0"encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/itemPrefs"android:title="@string/titlePrefs"
android:icon="@android:drawable/ic_menu_preferences"></item>
</menu>
Menu
 The options menu is loaded by your activity when the user
clicks on the Menu button on the device.
 The rst time that the menu button is pressed,the system
will call the activity's onCreateOptionsMenu() method to
in ate the menu from the menu.xml resource.

The in ater reads the XML code and for every element,it
creates a corresponding Java object and for each attribute in
XML it sets that object's properties accordingly.

From that point on,the menu is in memory and
onCreateOptionsMenu() doesn't get called again until the
activity is destroyed.
 Each time a user selects a menu item though,
onOptionsItemSelected() gets called to process that click.
Menu
 The options menu is loaded by your activity when the user
clicks on the Menu button on the device.
 The rst time that the menu button is pressed,the system
will call the activity's onCreateOptionsMenu() method to
in ate the menu from the menu.xml resource.

The in ater reads the XML code and for every element,it
creates a corresponding Java object and for each attribute in
XML it sets that object's properties accordingly.

From that point on,the menu is in memory and
onCreateOptionsMenu() doesn't get called again until the
activity is destroyed.
 Each time a user selects a menu item though,
onOptionsItemSelected() gets called to process that click.
Menu
 The options menu is loaded by your activity when the user
clicks on the Menu button on the device.
 The rst time that the menu button is pressed,the system
will call the activity's onCreateOptionsMenu() method to
in ate the menu from the menu.xml resource.

The in ater reads the XML code and for every element,it
creates a corresponding Java object and for each attribute in
XML it sets that object's properties accordingly.

From that point on,the menu is in memory and
onCreateOptionsMenu() doesn't get called again until the
activity is destroyed.
 Each time a user selects a menu item though,
onOptionsItemSelected() gets called to process that click.
Menu
 The options menu is loaded by your activity when the user
clicks on the Menu button on the device.
 The rst time that the menu button is pressed,the system
will call the activity's onCreateOptionsMenu() method to
in ate the menu from the menu.xml resource.

The in ater reads the XML code and for every element,it
creates a corresponding Java object and for each attribute in
XML it sets that object's properties accordingly.

From that point on,the menu is in memory and
onCreateOptionsMenu() doesn't get called again until the
activity is destroyed.
 Each time a user selects a menu item though,
onOptionsItemSelected() gets called to process that click.
Menu
 The options menu is loaded by your activity when the user
clicks on the Menu button on the device.
 The rst time that the menu button is pressed,the system
will call the activity's onCreateOptionsMenu() method to
in ate the menu from the menu.xml resource.

The in ater reads the XML code and for every element,it
creates a corresponding Java object and for each attribute in
XML it sets that object's properties accordingly.

From that point on,the menu is in memory and
onCreateOptionsMenu() doesn't get called again until the
activity is destroyed.
 Each time a user selects a menu item though,
onOptionsItemSelected() gets called to process that click.
Update StatusActivity to Load Menu
//Called first time user clicks on the menu button
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return true;
}
Update StatusActivity to Handle Menu Events
//Called when an options item is clicked
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.itemPrefs:
startActivity(new Intent(this,PrefsActivity.class));
break;
}
return true;
}
res/values/strings.xml
<?xml version="1.0"encoding="utf-8"?>
<resources>
<string name="app_name">Yamba 2</string>
<string name="titleYamba">Yamba 2</string>
<string name="hintText">Please enter your 140-character status</string>
<string name="buttonUpdate">Update</string>
<string name="titleStatus">Status Update</string>
<string name="titlePrefs">Prefs</string>
<string name="titleUsername">Username</string>
<string name="titlePassword">Password</string>
<string name="titleApiRoot">API Root</string>
<string name="summaryUsername">Please enter your username</string>
<string name="summaryPassword">Please enter your password</string>
<string name="summaryApiRoot">URL of Root API for your service</string>
</resources>
PrefsActivity
You should be able to run your application at this point and see
the new PrefsActivity by clicking on Menu.
Shared Preferences
 To programmatically access your preferences,we'll use the
SharedPreference class provided by the Android framework.
 The fact that this class is called SharedPreference refers to
the fact that this preference is easily accessible from anywhere
in this application.
Shared Preferences
 To programmatically access your preferences,we'll use the
SharedPreference class provided by the Android framework.
 The fact that this class is called SharedPreference refers to
the fact that this preference is easily accessible from anywhere
in this application.
Shared Preferences
Now that we have username,password and API root coming from
user-dened preferences,we can refactor our twitter code so that it
no longer hard-codes them.
private Twitter getTwitter() {
if (twitter == null) {
String username,password,apiRoot;
username = prefs.getString("username","");
password = prefs.getString("password","");
apiRoot = prefs.getString("apiRoot","https://identi.ca/api");
//Connect to twitter.com
twitter = new Twitter(username,password);
twitter.setAPIRootUrl(apiRoot);
}
return twitter;
}
Summary
At this point,the user can specify the username and password for
the micro-blogging site.