View

Thursday 7 July 2011

How to use Multi Language in android?

How to use multi language of text in android?

Its just a easy way to use it.How ? The html decimal unicode is suppoted by android os.Here i show three languages,Tamil,hindi and Telungu.First of all u should download the DLL file.For example :
"Akshar.ttf",Its a one of the Library file,It help to convert the decimal unicode format.Your Android application there is assests folder ,you just create a new folder and name as fonts,then put inside of the "Akshar.ttf" file in that folder,Now come to our Code section.

 TextView english,tamil,hin,tel;
String tam,h,t;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tamil=(TextView)findViewById(R.id.textView2);
        hin=(TextView)findViewById(R.id.hindi);
        tel=(TextView)findViewById(R.id.telngu);
        tam="ஸக-அன்ட்ரொஇட் அப்ப்லிகடிஒன்.ப்லொக்ஸ்பொட்.கொம்";
        h="सग-अन्ड्रोइड् अप्प्लिcअटिओन्.ब्लोग्स्पोट्.cओम्";
        t="సగ-అన్డ్రొఇడ్ అప్ప్లిcఅటిఒన్.బ్లొగ్స్పొట్.cఒమ్        ";
       
        Typeface tf=Typeface.createFromAsset(getAssets(), "fonts/Akshar.ttf");
       
      
        tamil.setTypeface(tf,Typeface.BOLD);
        tamil.setText(Html.fromHtml(tam));
       
        hin.setTypeface(tf,Typeface.ITALIC);
        hin.setText(Html.fromHtml(h));
       
       
        tel.setTypeface(tf,Typeface.NORMAL);
        tel.setText(Html.fromHtml(t));
       
    }
}


The output is:
Download full source code

Saturday 2 July 2011

How to use Autocomplete searchbox in Database?

Autocomplete means it show the complete text automatically.It have two types
1.Single line autocomplete
2.Multi line autocomplete
what letter we type in the searchbox,that letter of the complete words  to show.Here i use autocomplete searchbox ,The select item can pass into the textview
.I use DATABASE NAME as="itemsearchsqlite.db",TABLE NAME as="itemsearch" and DB COLUMN NAME as="item_name".
I use two java classes namely Home.java and SQLiteItemsearch.java
//SQLiteItemSearch.java//
------------------------
package com.autocomplete.sample;
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;
public class SQLiteItemSearch extends SQLiteOpenHelper
{
    private static final String DB_NAME = "itemsearchsqlite.db";
    private static final int DB_VERSION_NUMBER = 1;
    private static final String DB_TABLE_NAME = "itemsearch";
    private static final String DB_COLUMN_1_NAME = "item_name";

    private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME +
                            " (_id integer primary key autoincrement, item_name text not null);)";

    private SQLiteDatabase sqliteDBInstance = null;

    public SQLiteItemSearch(Context context)
    {
        super(context, DB_NAME, null, DB_VERSION_NUMBER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // TODO: Implement onUpgrade
    }

    @Override
    public void onCreate(SQLiteDatabase sqliteDBInstance)
    {
        Log.i("onCreate", "Creating the database...");
        sqliteDBInstance.execSQL(DB_CREATE_SCRIPT);
    }

    public void openDB() throws SQLException
    {
        Log.i("openDB", "Checking sqliteDBInstance...");
        if(this.sqliteDBInstance == null)
        {
            Log.i("openDB", "Creating sqliteDBInstance...");
            this.sqliteDBInstance = this.getWritableDatabase();
        }
    }

    public void closeDB()
    {
        if(this.sqliteDBInstance != null)
        {
            if(this.sqliteDBInstance.isOpen())
                this.sqliteDBInstance.close();
        }
    }

    public long insertitmSearch(String ItemBrandName)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DB_COLUMN_1_NAME, ItemBrandName);
        Log.i(this.toString() + " - insertitmSearch", "Inserting: " + ItemBrandName);
        return this.sqliteDBInstance.insert(DB_TABLE_NAME, null, contentValues);
    }

    public boolean removeitmSearch(String ItemBrandName)
    {
        int result = this.sqliteDBInstance.delete(DB_TABLE_NAME, "item_name='" + ItemBrandName + "'", null);

        if(result > 0)
            return true;
        else
            return false;
    }

    public long updateitmSearch(String oldItemBrandName, String newItemBrandName)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DB_COLUMN_1_NAME, newItemBrandName);
        return this.sqliteDBInstance.update(DB_TABLE_NAME, contentValues, "item_name='" + oldItemBrandName + "'", null);
    }

    public String[] getAllItemFilter()
    {
        Cursor cursor = this.sqliteDBInstance.query(DB_TABLE_NAME, new String[] {DB_COLUMN_1_NAME}, null, null, null, null, null);

        if(cursor.getCount() >0)
        {
            String[] str = new String[cursor.getCount()];
            int i = 0;

            while (cursor.moveToNext())
            {
                 str[i] = cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_NAME));
                 i++;
             }
            return str;
        }
        else
        {
            return new String[] {};
        }
    }
}

Then again we have to insert a rows of dataitems inside of the Home.java class
public class Home extends Activity {
 private SQLiteItemSearch sqllitebb;
 TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
       
        final AutoCompleteTextView actv=(AutoCompleteTextView)findViewById(R.id.autocompleteitem);
        sqllitebb=new SQLiteItemSearch(Home.this);
        sqllitebb.openDB();
     // Insert a few item list statically//
     
        sqllitebb.insertitmSearch("Color Monitor");
        sqllitebb.insertitmSearch("Compact Disk");
        sqllitebb.insertitmSearch("Computer");
       sqllitebb.insertitmSearch("Copy Righter");
       sqllitebb.insertitmSearch("Hard Disk");
       sqllitebb.insertitmSearch("HP Printer");
       sqllitebb.insertitmSearch("HP Laser Printer");
       sqllitebb.insertitmSearch("HP Injet Printer");
      //  sqllitebb.removeitmsearch("Computer");
       // sqllitebb.updateitmSearch("Computer","DELL");
     final  String[] deal = sqllitebb.getAllItemFilter();
      
       // Print out the values to the log
       for(int i = 0; i < deal.length; i++)
       {
           Log.i(this.toString(), deal[i]);
       }
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,deal);
       actv.setAdapter(adapter); 
       actv.setThreshold(1);
       actv.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            tv=(TextView)findViewById(R.id.selecteditem_tv);
            tv.setText(deal[arg2]);
            arg0.getItemAtPosition(arg2);
                Log.i("SELECTED TEXT WAS------->",  deal[arg2]);
           }
       });
    }
    public void onDestroy()
    {
        super.onDestroy();
        sqllitebb.close();
    }
}