Monday 31 December 2012

Send SMS Demo

Demo:





1)Activity:
 
package com.venky.sendsmsdemo;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.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 {

      Button btnSend;

      EditText edtPhNumber, edtMessage;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btnSend = (Button) findViewById(R.id.btnSend);

            edtPhNumber = (EditText) findViewById(R.id.edtPhoneNumber);
            edtMessage = (EditText) findViewById(R.id.edtMessage);

            btnSend.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        SmsManager manager = SmsManager.getDefault();

                        String phoneNumber = edtPhNumber.getText().toString();
                        String message = edtMessage.getText().toString();

                        Toast.makeText(MainActivity.this, "Message Sending ...", 5000)
                                    .show();

                        manager.sendTextMessage(phoneNumber, null, message, null, null);

                  }
            });
      }

}


2)main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Sending SMS"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Enter Phone Number"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <EditText
        android:id="@+id/edtPhoneNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Enter Message"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <EditText
        android:id="@+id/edtMessage"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:hint="Enter message" />

    <Button
        android:id="@+id/btnSend"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Send" />

</LinearLayout>


3)Add this Permission in AndroidManifest.xml: 

    <uses-permission android:name="android.permission.SEND_SMS"/>


4)Download this Project Click Here

Difference of two Alter Commands -SQLite


String statement = "ALTER TABLE MyTable ADD COLUMN" + name + "text";
sqLiteDatabase.execSQL(statement);

creating table MyTable with column COLUMNname


String statement = "ALTER TABLE MyTable ADD COLUMN " + name + "text";
sqLiteDatabase.execSQL(statement);

creating table MyTable with column name


<div dir="ltr" style="text-align: left;" trbidi="on"> <br /> <pre class="default prettyprint prettyprinted" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 10px; max-height: 600px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; vertical-align: baseline; width: auto;"><div class="post-text" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 5px; margin-left: 0px; margin-right: 5px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline; width: 660px;"> </div> </pre> </div>

Sunday 30 December 2012

How to Custamize Toast in Android?

You can customize your tost:
LayoutInflater mInflater=LayoutInflater.from(this);

View view=mInflater.inflate(R.layout.your_layout_file,null);
Toast toast=new Toast(this);
toast.setView(view);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
Or General way:
Toast.makeText(context,"Your message.", Toast.LENGTH_LONG).show();

How do you get the selected value of a spinner — Android


public class YourItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}
Finally, your ItemSelectedListener needs to be registered in the Spinner:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

Thursday 27 December 2012

SQLiteOpenHelper Demo



1.Activity:

package com.venkool.databasedemo;

import android.app.Activity;

public class DatabaseDemoActivity extends Activity {
      /** Called when the activity is first created. */

      MyDatabase database;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            database = new MyDatabase(this, "MyDB.sqlite", null, 1);

            database.insert("venkool", "2000");
      }
}

2.Database Class:
public class MyDatabase extends SQLiteOpenHelper {

      SQLiteDatabase sqliteDatabase;

      public MyDatabase(Context context, String name, CursorFactory factory,
                  int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub

            db.execSQL("create table MyTable(name text,salary text)");

      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

      }

      public void open() {
            sqliteDatabase = getWritableDatabase();
      }

      public void close() {
            sqliteDatabase.close();
      }

      public void insert(String name, String salary) {

            open();
           
            try {
                  ContentValues values = new ContentValues();
                  values.put("name", name);
                  values.put("salary", salary);

                  sqliteDatabase.insert("MyTable", null, values);
            } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }

            close();
      }
}