Thursday, December 23, 2010

Android #9: Working with Android Intents - Part 4

Refer the previous post to get background on Intents - http://satworks.blogspot.com/2010/12/android-6-working-with-android-intents.html

Explicit Intents with return example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one activity but instead of passing the control to the invoked activity, will stay on the current invoking activity with values updated from invoked activity



layout/main.xml
<?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">
<TextView android:id="@+id/btnMsgId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/msg" />
<Button android:id="@+id/btnRtnId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/invokeRtnBtn" />
</LinearLayout>
BaseInvokingActivity.java
package in.satworks.android.samples.activity;

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

public class BaseInvokingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Invoke other activity and Return
Button invokeRtnBtn = (Button) findViewById(R.id.btnRtnId);
invokeRtnBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent explicitIntent = new Intent(BaseInvokingActivity.this,
OtherInvokedActivity.class);
explicitIntent.putExtra("FromBaseInvokingActivity",
"Initial Value from FromBaseInvokingActivity");
startActivityForResult(explicitIntent, 1);
}
});
}
/** Called when an activity called by using startActivityForResult finishes. */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle extras = data.getExtras();
String returnVal = (String) extras.get("OtherInvokedActivity");
Toast.makeText(BaseInvokingActivity.this, returnVal, Toast.LENGTH_SHORT)
.show();
}
}
OtherInvokedActivity.java
package in.satworks.android.samples.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class OtherInvokedActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String val=(String)extras.get("FromBaseInvokingActivity");
Intent modifiedIntent=new Intent();
modifiedIntent.putExtra("OtherInvokedActivity", val+":\nNow modified with this text included from OtherInvokedActivity");
setResult(1, modifiedIntent);
finish();
}
}
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ExplicitIntentsApp</string>
<string name="msg">BaseInvokingActivity!</string>
<string name="otherMsg">OtherInvokedActivity!</string>
<string name="invokeRtnBtn">Invoke Other Activity and Return</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.satworks.android.samples.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BaseInvokingActivity" 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 android:label="@string/app_name" android:name=".OtherInvokedActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>

Monday, December 20, 2010

Android #8: Working with Android Intents - Part 3

Refer the previous post to get background on Intents - http://satworks.blogspot.com/2010/12/android-6-working-with-android-intents.html

Explicit Intents example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one activity



layout/main.xml
<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/msg" />
<Button android:id="@+id/btnId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/invokeBtn" />
</LinearLayout>
layout/otherinvoked.xml
<?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">
<TextView android:id="@+id/otherMsgId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/otherMsg" />
</LinearLayout>
BaseInvokingActivity.java
package in.satworks.android.samples.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BaseInvokingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Invoke other activity
Button invokeBtn=(Button)findViewById(R.id.btnId);
invokeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent explicitIntent=new Intent(BaseInvokingActivity.this, OtherInvokedActivity.class);
explicitIntent.putExtra("FromBaseInvokingActivity", "Value from FromBaseInvokingActivity");
startActivity(explicitIntent);
}
});
}
}
OtherInvokedActivity.java
package in.satworks.android.samples.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class OtherInvokedActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.otherinvoked);
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String val=(String)extras.get("FromBaseInvokingActivity");
TextView txtView=(TextView)findViewById(R.id.otherMsgId);
txtView.setText("OtherInvokedActivity: "+val);
}
}
values/strings.xml
<?xml version="1.0" encoding="utf-8"?><resources>
<string name="app_name">ExplicitIntentsApp</string>
<string name="msg">BaseInvokingActivity!</string>
<string name="otherMsg">OtherInvokedActivity!</string>
<string name="invokeBtn">Invoke Other Activity</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.satworks.android.samples.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BaseInvokingActivity" 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 android:label="@string/app_name" android:name=".OtherInvokedActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>

Android #7: Working with Android Intents - Part 2

Refer the previous post to get background on Intents - http://satworks.blogspot.com/2010/12/android-6-working-with-android-intents.html

Implicit Intents example: Following example demonstrates the inter-activity communication and will show on invoking some of the built-in Android activities from one activity







layout/main.xml
<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/msg" />
<Button android:id="@+id/btnDialId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnDialTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnCallId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnCallTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnSMSId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnSMSTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnSearchId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnSearchTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnBrowseId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnBrowseTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnGoogleMapId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnGoogleMapTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnViewContactsId"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/btnViewContactsTxt" android:gravity="right|center_vertical" />
</LinearLayout>

ImplicitIntentsActivity.java
package in.satworks.android.samples.activity;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ImplicitIntentsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// To Dial a number
launchDialer();
// To Call a Number
// Requires android.permission.CALL_PHONE
callNumber();
// To SMS a Number
launchSMS();
// To perform local search
performLocalSearch();
// To open browser with URL
launchBrowser();
// To launch Google Maps
showGoogleMap();
// To View existing contacts stored in phone
showContacts();
}

// To View existing contacts stored in phone
private void showContacts() {
Button viewContacts = (Button) findViewById(R.id.btnViewContactsId);
viewContacts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(contacts);
}
});

}

// To launch Google Maps
private void showGoogleMap() {
Button googleMapBtn = (Button) findViewById(R.id.btnGoogleMapId);
googleMapBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("geo:13.05,80.20"));
startActivity(intent);
}
});
}

// Open a browser window to the URL specified.
private void launchBrowser() {
Button browseBtn = (Button) findViewById(R.id.btnBrowseId);
browseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://www.android.com"));
startActivity(intent);
}
});
}

// Perform local search
private void performLocalSearch() {
Button searchBtn = (Button) findViewById(R.id.btnSearchId);
searchBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.putExtra(SearchManager.QUERY, "Test");
startActivity(intent);
}
});
}

// To launch SMS
private void launchSMS() {
Button smsBtn = (Button) findViewById(R.id.btnSMSId);
smsBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri
.parse("sms:123456789"));
intent.putExtra("sms_body", "Test SMS message.....");
startActivity(intent);
}
});
}

// Calls the entered phone number.
private void callNumber() {
Button callBtn = (Button) findViewById(R.id.btnCallId);
callBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:123456789"));
startActivity(intent);
}
});
}

// Dials (but does not actually initiate the call) the number given
private void launchDialer() {
Button dialBtn = (Button) findViewById(R.id.btnDialId);
dialBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri
.parse("tel:123456789"));
startActivity(intent);
}
});
}
}

values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ImplicitIntentsApp</string>
<string name="msg">ImplicitIntentsActivity! \nTry out few of the predefined intents\n</string>
<string name="btnDialTxt">Launch Dialer</string>
<string name="btnCallTxt">Launch Call</string>
<string name="btnSMSTxt">Launch SMS</string>
<string name="btnSearchTxt">Launch Search</string>
<string name="btnBrowseTxt">Launch Browser</string>
<string name="btnGoogleMapTxt">Launch Google Map</string>
<string name="btnViewContactsTxt">Launch Phone Contacts</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.satworks.android.samples.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ImplicitIntentsActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

Android #6: Working with Android Intents - Part 1

Android Intents are used for event notifications thus notifying applications about an event through passing messages between components/activities. In simple words, intents are messages that are passed between components. An Intent object holds the content of the message. For example, notifies when a SMS arrives, or SD card inserted into the device etc. Any component that wants to invoke another component has to express only through “intents” to perform a job.

Intents are typically used to activate components such as activities, services, and broadcast receivers. That is for launching an activity, broadcast messages for broadcast receivers, start a service/bind a service to communicate with any background service. Intents typically needs the basic information’s such as action to be performed and data to operate on.

  1. For activating the activity component, intent object holding the message content is passed to startActivity or startActivityForResult operations
  2. For activating the service component, intent object holding the message content is passed to startService operation
  3. For initiating a broadcast, intent object holding the message content is passed to sendBroadcast, sendOrderedBroadcast, and sendStickyBroadcast operations

Communication between components is achieved through intents. There are two primary form of intents namely

  1. Implicit Intents: Intents that do not name or specify a target component are termed as Implicit Intents. It is left to the platform to find an appropriate component to respond to the intent. Android platform resolves as to which component is most suitable to respond to such implicit intents using intent filters configured within Android Manifest file.
  2. Explicit Intents: Unlike Implicit Intents, you actually specify the target component that is required to respond to the intent.
Typical Intents Usage:

  • Inter-Activity (Activity-to-Activity) Communication: Here we can use Intents to pass information between activities wherever we have a need for creating multiple activities, and there exists a need for communicating/passing information between them. The activities can be implicit or explicit activities
  • Activity-Service Communication: Here we can use Intents to make a service perform a information update in an activity.
Examples:

Thought would create three different examples to show on inter-activity (activity-to-activity) communication in separate posts.

  1. Implicit Intents example: This example demonstrates the inter-activity communication and will show on invoking some of the built-in Android activities from one activity (http://satworks.blogspot.com/2010/12/android-7-working-with-android-intents.html)
  2. Explicit Intents example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one activity (http://satworks.blogspot.com/2010/12/refer-previous-post-to-get-background.html)
  3. Explicit Intents with return example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one activity but instead of passing the control to the invoked activity, will stay on the current invoking activity with values updated from invoked activity (http://satworks.blogspot.com/2010/12/android-9-working-with-android-intents.html)

Sunday, December 12, 2010

Android #5: Working with Custom Fonts

All Android devices comes with a standard collection of fonts namely Droid Sans, Droid Sans Mono, and Droid Serif as they are designed to be optimal for mobile displays. But there might be a need for using custom fonts for some special purposes say for example, Indic Tamil fonts which Android doesn’t support currently. So as a workaround in getting such fonts for a special need, we can go with using custom fonts features that Android supports.

Following example illustrates on using custom fonts in Android application



layout/main.xml:

<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="27px"
android:text="@string/msg" />
<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />
<TextView android:id="@+id/tamilTxtId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="27px"
android:textColor="#CDE472" />
<TextView android:id="@+id/txtId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/engFont"
android:textSize="27px" android:textColor="#CDE472" />
</LinearLayout>

CustomFontActivity.java:

package in.satworks.android.samples.activity;import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class CustomFontActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Use ENGLISH font
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/VLADIMIR.ttf">
TextView txtView=(TextView)findViewById(R.id.txtId);
txtView.setTypeface(face);
// Use TAMIL font
Typeface tamilFace=Typeface.createFromAsset(getAssets(), "fonts/Eltpan-n.ttf");
TextView tamilTxtView=(TextView)findViewById(R.id.tamilTxtId);
tamilTxtView.setText("1: \u0053\u00D6\u005D\u00EB \u0045\u005D\u00EB \u0053\u0081\u0054\u005D\u00EB");
tamilTxtView.setTypeface(tamilFace);
}
}
Custom Fonts:
Custom font files need to be dropped under assets folder of your android project
  • assets/fonts/VLADIMIR.TTF
  • assets/fonts/Eltpan-n.ttf