Wednesday 18 February 2015

Adding a Share Button to Your Android Apps

Code:

<RelativeLayout 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">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="title"
    android:id="@+id/btnNext"/>

</RelativeLayout>

MainActivity:

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

public class MainActivity extends Activity {

private Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_main);
   btn = (Button)findViewById(R.id.btnNext);
   btn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       
        //create the send intent
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
       
        //set the type
        shareIntent.setType("text/plain");
       
        //add a subject
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,  
        "Insert Subject Here");
       
        //build the body of the message to be shared
        String shareMessage = "Insert message body here.";
       
        //add the message
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,  
        shareMessage);
       
        //start the chooser for sharing
        startActivity(Intent.createChooser(shareIntent,  
        "Insert share chooser title here"));  
       
       }
   });    
}

}




Thursday 5 February 2015

Android SQLite database

 What is SQLite?

SQLite is an Open Source Database which is embedded into Android. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements. In addition it requires only little memory at runtime (approx. 250 KByte).

SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar to long in Java) and REAL (similar to double in Java). All other types must be converted into one of these fields before saving them in the database. SQLite itself does not validate if the types written to the columns are actually of the defined type, e.g. writing an integer into a string column and vice versa. 

SQLite is available on every Android device. Using an SQLite database in Android does not require any database setup or administration. The SQL statements for creating and updating the database must only be defined and the database is automatically managed for users by the Android platform.


SQLite Operations:

1. Create a database. 
2. Open the database. 
3. Create a table. 
4. Create an insert interface for data-sets. 
5. Create a query interface for data sets. 
6. Close the database.

Creating Table:

static final String DATABASE_USERDETAILS = "create table "+"emp_details"+"( " 
        +"EMPLOYEE_ID INTEGER primary key AUTOINCREMENT, "
          + "EMPLOYEE_NAME  TEXT NOT NULL," +
           "MOBILE TEXT NOT NULL ," +
            "IMAGENAME TEXT ); ";

Inserting Data into Table:

public void insertEmployeeDetails(String emp_name,String mobile,String imageName)
{
ContentValues newValues = new ContentValues();
 newValues.put("EMPLOYEE_NAME", emp_name);
 newValues.put("MOBILE", mobile);
 newValues.put("IMAGENAME", imageName);
 db.insert("emp_details", null, newValues);
 }  

Checking Table:

                        public String checkempty()
               {
                String checkdb="EXIST";
                String selectQuery = "SELECT  * FROM emp_details";
                Cursor cursor = db.rawQuery(selectQuery, null);
                if(cursor.getCount()<1) 
                {
                       cursor.close();
                       checkdb="NOT EXIST";
                }
                return checkdb;
               }

Selecting Table Values:

public ArrayList<EmployeeModel> getEmployeeeDetails() {
 ArrayList<EmployeeModel> EmployeeList = new ArrayList<EmployeeModel>();
        try {
        String selectQuery = "select * from emp_details";
        Cursor cursor = db.rawQuery(selectQuery, null);
        int c1 = cursor.getColumnIndex("EMPLOYEE_NAME");
        int c2 = cursor.getColumnIndex("MOBILE");
        int c3 = cursor.getColumnIndex("IMAGENAME");
        int c4 = cursor.getColumnIndex("EMPLOYEE_ID");
       
        if (cursor.moveToFirst()) {
        do {
        EmployeeModel emplist=new EmployeeModel();
        emplist.setName(cursor.getString(c1));
        emplist.setMobileNo(cursor.getString(c2));
        emplist.setImage(cursor.getString(c3));
        emplist.setEmp_ID(cursor.getString(c4));
        EmployeeList.add(emplist);
        } while (cursor.moveToNext());
        }
        } catch (Exception e) {
        e.printStackTrace();
        }

        return EmployeeList;
        }
Delete Query:

public void deleteContact(String number)
    {
        db.delete("emp_details", "EMPLOYEE_ID=?", new String[]{""+number});
     }

Update Query:

 public void Updatedetails(String name,String mobileNumber,String EmpId)
 {
 ContentValues updatedValues = new ContentValues();
 updatedValues.put("EMPLOYEE_NAME", name);
 updatedValues.put("MOBILE", mobileNumber);
String where="EMPLOYEE_ID = ?";
 db.update("emp_details",updatedValues, where, new String[]{EmpId});
 }
       
For Sample Program:    Click Here