Saturday, September 8, 2012

Dial Example In Android

This example shows how you can use system intents and will allow you to enter a number and dial it.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it DialExample.
2.) You will see some default code into your strings.xml and android manifest file.
3.) Write following code into your mail.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/hello"/>
</LinearLayout>
4.) Build and run your code and check the output given below in the doc.
Steps:
1.) Create a project named DialExample and set the information as stated in the image.
Build Target: Android 2.2
Application Name: DialExample
Package Name: com.org. DialExample
Activity Name: DialExample
Min SDK Version: 8
2.) Open DialExample.java file and write following code there:
package com.org.DialExample;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class DialExample extends Activity {
          EditText mEditText_number = null;
          LinearLayout mLinearLayout_no_button = null;
          Button mButton_dial = null;
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mLinearLayout_no_button = new LinearLayout(this);
            mEditText_number = new EditText(this);
            mEditText_number.setText("5551222");
            mLinearLayout_no_button.addView(mEditText_number);
            mButton_dial = new Button(this);
            mButton_dial.setText("Dial!");
            mLinearLayout_no_button.addView(mButton_dial);
            mButton_dial.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v)
              {
                performDial();
              }
            });
            setContentView(mLinearLayout_no_button);
          }
          public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_CALL)
            {
              performDial();
              return true;
            }
            else if(keyCode == KeyEvent.KEYCODE_BACK)
            {
                finish();
                return true;
            }
            else
                return false;
          }
          public void performDial(){
            if(mEditText_number!=null){
              try {
                startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" +mEditText_number.getText())));
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
        }
3.) Compile and build the project.
4.) Run on 2.2 simulator for the output.

No comments:

Post a Comment