Android Code to Send Sms

In this post you can learn on android code to send sms from an android app. There are many applications in the android market where sms can be sent to the corresponding subscribers from the android web app using this functionality. Let us discuss about it in detail. You can find below an example on how to send sms from an android app. There is a text field for entering the phone number to whom you are gonna send the sms and there is a text field for typing the sms and there is a button named send. On filling the corresponding details in the field and on submitting the button the sms is sent to the corresponding phone number. Using this example you can develop more complex sms functionality for sending sms for a list of users. Let us see the brief android code to send sms below.

1. Create a layout named activity_main.xml in the layout folder of the project.

    activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/EditText_PhoneNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Number"
        android:inputType="phone" >
    </EditText>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="250dp" >
        <EditText
            android:id="@+id/MainActivity_Message"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ems="10"
            android:hint="Enter message"
            android:inputType="textMultiLine" />
    </RelativeLayout>
    <Button
        android:id="@+id/Send_msg_Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send_Message" />
</LinearLayout>


2. Create a MainActivity.java in the project

    MainActivity.java

package com.example.sms;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
    Button send;
    EditText phone_Number, message;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) findViewById(R.id.Send_msg_Btn);    
        phone_Number = (EditText) findViewById(R.id.EditText_PhoneNumber);
        message = (EditText) findViewById(R.id.MainActivity_Message);
        send.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        String phone_Num = phone_Number.getText().toString();
        String send_msg = message.getText().toString();
        try {
            SmsManager sms = SmsManager.getDefault();  // using android SmsManager            sms.sendTextMessage(phone_Num, null, send_msg, null, null);  // adding number and text 
        } catch (Exception e) {
            Toast.makeText(this, "Sms not Send", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}

3. AndroidManifest File.

    Add Uses permission to the below android manifest file to send the sms.
    AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smsdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


After completing the above steps you can run your android app and can send sms to the corresponding user.

Bar Chart in Android Example

In this post, you can learn how to design a bar chart for android applications. Using an external jar file named as "aChartEngine", we can develop a bar chart for android apps. First download "achartengine.jar" from code.google.com/p/achartengine/downloads/list and paste it in the "lib" folder of your android structure. Now follow the below steps and do complete your bar chart for android apps.

Live Chat Discussion

1.  Just add the following activity code in the android manifest XML file.
<activity android:name="org.achartengine.GraphicalActivity"/> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.graphdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.graphdemo.GraphActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--Add the below activity code in your Android Manifest file -->
        <activity android:name="org.achartengine.GraphicalActivity"/>
    

    </application>
</manifest>


2.  Place the "activity_graph.xml" in the "res" folder.


activity_graph.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<Button
      android:id="@+id/BarGraph"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="BarGraph" />
</LinearLayout>


3. Now write the Java code units for "Graph Activity".


GraphActivity.java 

package com.udhaya.graphdemo;
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 GraphActivity extends Activity implements OnClickListener {
    Button  barGraph;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graph);
        barGraph = (Button) findViewById(R.id.BarGraph);
        barGraph.setOnClickListener(this);
        }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
                  case R.id.BarGraph:
                  BarGraph bar = new BarGraph();
                  Intent barIntent = bar.getIntent(this);
                  startActivity(barIntent);
                  break;
        }
        }
}


4. Create a class "BarGraph.java".

    
BarGraph is a class in which the graph is actually created by using the methods of "achartengine" library.
Here we are using static values inside the code units for generating the chart. It can be modified dynamically by using corresponding code units as per your requirements and the values can be fetched from static device memory (SQLite Database) or even from the server database.

BarGraph.java 

package com.udhaya.graphdemo;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
public class BarGraph {
        public Intent getIntent(Context context){
        int y[] = {25,10,15,20};
        CategorySeries series = new CategorySeries("Bar1");
        for(int i=0; i < y.length; i++){
            series.add("Bar"+(i+1),y[i]);
        }
        XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset();  // collection of series under one object.,there could any
        dataSet.addSeries(series.toXYSeries());                            // number of series
        //customization of the chart
        XYSeriesRenderer renderer = new XYSeriesRenderer();     // one renderer for one series
        renderer.setColor(Color.RED);
        renderer.setDisplayChartValues(true);
        renderer.setChartValuesSpacing((float) 5.5d);
        renderer.setLineWidth((float) 10.5d);
         XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();   // collection multiple values for one renderer or series
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.setChartTitle("Demo Graph");
//      mRenderer.setXTitle("xValues");
        mRenderer.setYTitle("Rupee");
        mRenderer.setZoomButtonsVisible(true);    mRenderer.setShowLegend(true);
        mRenderer.setShowGridX(true);      // this will show the grid in  graph
        mRenderer.setShowGridY(true);              
//      mRenderer.setAntialiasing(true);
        mRenderer.setBarSpacing(.5);   // adding spacing between the line or stacks
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setXAxisMin(0);
//      mRenderer.setYAxisMin(.5);
        mRenderer.setXAxisMax(5);
        mRenderer.setYAxisMax(50);
//    
        mRenderer.setXLabels(0);
        mRenderer.addXTextLabel(1,"Income");
        mRenderer.addXTextLabel(2,"Saving");
        mRenderer.addXTextLabel(3,"Expenditure");
        mRenderer.addXTextLabel(4,"NetIncome");
        mRenderer.setPanEnabled(true, true);    // will fix the chart position
        Intent intent = ChartFactory.getBarChartIntent(context, dataSet, mRenderer,Type.DEFAULT);
        return intent;
    }
}


After completing the above steps your bar chart is ready for android devices.
My Profile