Pages - Menu

Saturday 27 October 2012

Invoking a Built in Activity

In addition to the user defined activities, an android application contains a number of built-in activities such as browser, contacts, call_ dallier etc. In this part we are going to see how to call a built-in activity from your  own activity. Like the calling of user defined activities a built in activity is also called using the intent object. 

We will start with an example, for that create a new android project and named it as BuiltinApp. In this project the main.xml file contain two buttons, first one is for invoking the built-in activity browser and the other one is for dialing a particular number.
The main.xml file for the project is given below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello" />

 <Button
 android:id="@+id/browser"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Open Browser" />

<Button
 android:id="@+id/start_call"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Make a Call" />
</LinearLayout>

The BuiltinAppActivity.java file is shown bellow.

package com.codeglympse;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BultinAppActivity extends Activity {
Button b1;
Button b2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.browser);
b2 = (Button) findViewById(R.id.start_call);
b1.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View arg0) 
{
// TODO Auto-generated method stub
Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(i);
}
});
b2.setOnClickListener(new OnClickListener()
 {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:+571894567"));
startActivity(i);
}
});
}
}

In the above example we call two built in activities, the  browser and dial. Here we also passing a data to the called activity by using the Uri.parse() method. That means here the intent object is an used as an action and data pair object.
Intent i = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:+571894567"));
Intent i =new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));

For invoking the built in activities like browser, you have set the android permission in the androidmanifest.xml file.The permission is set within the <manifest>  </manifest> tag in AndroidManifest.xml file.
AndroidManifest,xml file is given bellow.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codeglympse"
    android:versionCode="1"
    android:versionName="1.0" >

 <uses-sdk android:minSdkVersion="8" />
 <application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name" >
 <activity
   android:label="@string/app_name"
   android:name=".BultinAppActivity" >
 <intent-filter >
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>
<uses-permission android:name="android.permission.INTERNET" > </uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission>
</manifest>

Output of the project is given bellow.








Thursday 25 October 2012

Passing data to an Activity

Here we are going to see how to invoke an activity by passing a data to it. Passing of data to an activity is by means of attaching the data to the intent object. The steps you have to follow for invoking an activity by sending a data to it are given bellow.

Step 1:
             Create the intent object.
             It is the same step that we  already see in the start activity and start activity for result.
             Example:
                              Intent i = new Intent("com.codeglympse.SecondActivity");

Step 2:
            Create an object of the Bundle class
            The Bundle class is the supporting class for passing data to an activity.   
            Example:
                              Bundle bundle = new Bundle(); 

Step 3:
             Adding data to bundle object using putString() method
             Adding the data to the bumdle object by using the method putString().
             Syntax:
             bundle_object.putString(key,value);
             Here key is used for identifying a specific data from a pair of data. you can get a brief idea about it
             from the example provide bellow in this topic.
             Example:
                          bundle.putString("name", "Enter your name");
Step 4:
             Attach the Bundle object to the intent by using the method putExtras().
             Example:
                          i.putExtras(bundle); 

        
             
Step 5:
            Now start the activity using either startActivity() or startActivityForResult() method.

Step 6:
In order to receive the data from the intent object the target activity (called activity) must contains the following code segments.
       
             (a)    Get back the bundle object using the getIntent() . getExtras();
                  Example:
                                 Bundle bundle =  getIntent().getExtras();
             (b)   Get the data from the bundle object using getString() method
                  Example:
                                 String name = bundle.getString("Name");  where "Name" is the key value.

Consider the following example for more details about the concept. Followings are the codes for the android project PassingData.

(1) PassingDataActivity.java

package com.codeglympse;
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 PassingDataActivity extends Activity {
int request_code =1;
Button bn;
 /** Called when the activity is first created. */
  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    bn = (Button) findViewById(R.id.button1);
    bn.setOnClickListener(new OnClickListener() {

  @Override
    public void onClick(View arg0)
   {  
   Intent i = new Intent("com.codeglympse.DataActivity");
   Bundle extras = new Bundle();
   extras.putString("name", "Enter your name here");
   i.putExtras(extras);
   startActivityForResult(i, request_code);
   }
   }); 
   }
  public void onActivityResult(int requestcode, int resultcode, Intent data)
   {
 if(requestcode==request_code)
   {
 if(resultcode==RESULT_OK)
   {
   Toast.makeText(this,data.getData().toString(), Toast.LENGTH_SHORT).show();
   }
   }
   }
   }

(2) Code for the called activity (here it is DataActivity.java )

package com.codeglympse;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DataActivity extends Activity{
Button bn;
EditText txt;
String defaultname;
@Override
protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.data_activity);
bn = (Button) findViewById(R.id.b2);
txt = (EditText) findViewById(R.id.editText1);
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
defaultname=extras.getString("name");
}
txt.setHint(defaultname);
bn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent();
data.setData(Uri.parse(txt.getText().toString()));
setResult(RESULT_OK, data);
finish();
}
});
}
}
In the above example we use a method setHint ( ), which is same as setText () except that it is used for providing guidelines for the user. (It is appear in diminished text and when the user begin to fill the text area it will disappear)


Here is the output of the project