Tuesday 18 October 2011

DataBase


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class DataBase33 {


public static final String Row_categoryID="_categoryid";
public static final String Row_CategoryName="CategoryName";
public static final String Table_Name3="CategoryTable";

private static final String DataBase_Name="foodDataBase33";
private static final int version =1;
private static final String TAG = "DBAdapter";

private static final String DATA_CREATE2="create table CategoryTable(_categoryid integer primary key autoincrement,"+ " CategoryName text not null);";

private static Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DataBase33(Context con)
{
this.context=con;
DBHelper=new DatabaseHelper(con);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
       DatabaseHelper(Context context)
       {
           super(context, DataBase_Name, null, version);
       }

   
       public void onCreate(SQLiteDatabase db)
       {
         
           db.execSQL(DATA_CREATE2);
       }
 
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
       {
           Log.w(TAG, "Upgrading dataBase from version " + oldVersion + " to "+ newVersion + ", which will destroy all old data");
           db.execSQL("DROP TABLE IF EXISTS note_table");
           onCreate(db);
       }
   }  

public DataBase33 open() throws  Exception
{
db = DBHelper.getWritableDatabase();
       return this;
}

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

public long insertcategory(String categoryname)
{
ContentValues cv1=new ContentValues();
cv1.put(Row_CategoryName, categoryname);
Log.d("karannnnnnnnnn11111111111111111", categoryname);
return db.insert(Table_Name3, null, cv1);
}

public Cursor getcategory() throws SQLException
{
return db.query( Table_Name3,  new String[]{ Row_categoryID , Row_CategoryName }, null,null,null,null,null);  
}
 
public Cursor getAllcategory()
{
Cursor rt= db.rawQuery("select * from  CategoryTable",null);
return rt;
}


}

Change the Ringer Mode

Today i am going to write about changing the ringer mode of the Android phone programmitically.
Quite Simple: :-)

Step1):Make an object of Audio Manger class
                    AudioManager am;
                    am = (AudioManager)
                    context.getSystemService(Context.AUDIO_SERVICE);



Step2)Now after you have created the object of audio manager class you can at any time change the Ringer mode as follows:

For Silent Mode:
am.setRingerMode(0);        


For Vibrate mode:

am.setRingerMode(1);        


For Normal mode:

am.setRingerMode(2);        

Animation in Android

In this Post i will tell you about the simple blinking animation using apha property of android.
First create a class as follows by extending the Animation class:

BlinkAnimation,java

public class BlinkAnimation extends Animation {
    private int totalBlinks;
    private boolean finishOff;
   
    public BlinkAnimation(int totalBlinks, boolean finishOff) {
        this.totalBlinks = totalBlinks;
        this.finishOff = finishOff;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float period = interpolatedTime * totalBlinks * 3.14f + (finishOff ? 3.14f / 2 : 0);
        t.setAlpha(Math.abs(FloatMath.cos(period)));
    }

    @Override
    public boolean willChangeBounds() {
        return false;
    }

    @Override
    public boolean willChangeTransformationMatrix() {
        return false;
    }
}


Next step is to create a avtivity to instaniate the animation as follows:

TestActivity.java


public class TestActivity extends Activity implements OnClickListener, AnimationListener {
    private ImageView image;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        image = (ImageView) findViewById(R.id.image);
        ((Button) findViewById(R.id.button)).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            Animation animation;
            if (image.getVisibility() == View.VISIBLE) {
                animation = new BlinkAnimation(3, true);
                animation.setInterpolator(new DecelerateInterpolator());
            } else {
                animation = new BlinkAnimation(3, false);
                animation.setInterpolator(new AccelerateInterpolator());
            }
            animation.setDuration(1000L);
            animation.setAnimationListener(this);
           
            image.startAnimation(animation);
        }
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        image.setVisibility(image.getVisibility() == View.VISIBLE ?
                View.INVISIBLE : View.VISIBLE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {}

    @Override
    public void onAnimationStart(Animation animation) {}
}



and your XML file would look like this a follows:

main.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">
  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="30dip"
    android:src="@drawable/icon" />
  <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="30dip"
    android:text="Please click me" />
</LinearLayout>