Sunday, 18 May 2014

How to connect sqlite database in android

Connect sqlite database in android

Using the Android SQLite Database

Try this

    public class Database extends SQLiteOpenHelper {

private static final int DATABASE_VERSION=1 ;

private static final String DATABASE_NAME="DB_NAME";

public static final String TABLE_NAME="T_NAME";

public static final String FIELD1="field_name";

    public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " +
+"("+TABLE_NAME+ " TEXT,")";
System.out.println(CREATE_TABLE);
db.execSQL(CREATE_TABLE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+CREATE_TABLE);

onCreate(db);
}


Friday, 9 May 2014

How to set Date picker in Android

Date picker in Android :

datebutton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// On button click datepicker dialog show
showDialog(DATE_PICKER_ID);
}
});

Set Date in textview

private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year  = selectedYear;
month = selectedMonth;
day   = selectedDay;
// Show selected date
dob.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(""));
}
};

Use this......

Thursday, 8 May 2014

How to create Grid layout or Grid menu in android

Grid layout:

Try this :

grid_view.xml

<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">

<GridView
        android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="spacingWidthUniform" >
    </GridView>

</RelativeLayout>

Create Grid View Adapter class file

public class GridViewAdapter extends ArrayAdapter<Listitem> {
Context context;
int layoutResourceId;
ArrayList<Listitem> data = new ArrayList<Listitem>();
public GridViewAdapter(Context context, int layoutResourceId,ArrayList<Listitem>gridArray) {
super(context, layoutResourceId, gridArray);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = gridArray;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Listitem item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}

Create class file Activity_menu.java


public class Activity_menu extends Activity {
      
       GridViewAdapter GridViewAdapter;
       GridView gridView;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.grid_view);
             
              Bitmap bus = BitmapFactory.decodeResource(this.getResources(), R.drawable.bus);
              Bitmap train = BitmapFactory.decodeResource(this.getResources(), R.drawable.train);            
              Bitmap travel = BitmapFactory.decodeResource(this.getResources(), R.drawable.travel);
              Bitmap busroute = BitmapFactory.decodeResource(this.getResources(), R.drawable.busrr);

              gridArray.add(new Listitem(bus,"Bus"));
              gridArray.add(new Listitem(train, "Train"));
              gridArray.add(new Listitem(travel,"Flight"));
              gridArray.add(new Listitem(busroute, "Bus"));
             
              gridView = (GridView) findViewById(R.id.StugridView);
             
              GridViewAdapter = new GridViewAdapter(this, R.layout.gridlist, gridArray);
              gridView.setAdapter(GridViewAdapter);             
              gridView.setOnItemClickListener(new OnItemClickListener() {

                     @Override
                     public void onItemClick(AdapterView<?> parent, View v,
                                  int position, long id) {

                           switch(position)
                           {
                           case 0:
                                 
                                  ProgressDialog pDialog = new ProgressDialog(Activity_menu.this);
                                  pDialog.setMessage("Loading..");
                                  pDialog.setIndeterminate(false);
                                  pDialog.setCancelable(true);
                                  pDialog.show();
                                  Intent intent = new Intent(getApplicationContext(), Bus.class);
                                  startActivity(intent);
                                  break;
                           case 1:
                                 
                                  ProgressDialog pDialog1 = new ProgressDialog(Activity_menu.this);
                                  pDialog1.setMessage("Loading..");
                                  pDialog1.setIndeterminate(false);
                                  pDialog1.setCancelable(true);
                                  pDialog1.show();
                                  Intent intent1 = new Intent(getApplicationContext(), Train.class);
                                  startActivity(intent1);
                                  break;
                           case 2:
                                  ProgressDialog pDialog2 = new ProgressDialog(Activity_menu.this);
                                  pDialog2.setMessage("Loading..");
                                  pDialog2.setIndeterminate(false);
                                  pDialog2.setCancelable(true);
                                  pDialog2.show();
                                  Intent intent2 = new Intent(getApplicationContext(), Flight.class);
                                  startActivity(intent2);
                                  break;
                           case 3:
                                  ProgressDialog pDialog3 = new ProgressDialog(Activity_menu.this);
                                  pDialog3.setMessage("Loading..");
                                  pDialog3.setIndeterminate(false);
                                  pDialog3.setCancelable(true);
                                  pDialog3.show();
                                  Intent intent3 = new Intent(getApplicationContext(), Bus.class);
                                  startActivity(intent3);
                                  break;                                 
                           }
                     }
              });
         }
}

add reference library file
gridlayout_v7 .jar download link : https://github.com/jacobmoncur/gridlayout_v7/tree/master/bin

Sunday, 4 May 2014

How to create Tabview layout in Android

Tab view Layout

Create Tab_View.java

public class Tab_View extends TabActivity implements OnTabChangeListener {

TabHost tabHost;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab__view);
   
     // Get TabHost Refference
     tabHost = getTabHost();
   
     // Set TabChangeListener called when tab changed
     tabHost.setOnTabChangedListener(this);

     TabHost.TabSpec spec;
     Intent intent;

      /** TAB1 **/
     // Create  Intents to launch an Activity for the tab (to be reused)
     intent = new Intent().setClass(this, Tab_View1.class);
 
     spec = tabHost.newTabSpec("First").setIndicator("")
                   .setContent(intent);
   
     //Add intent to tab
     tabHost.addTab(spec);

     /** TAB2 **/
     intent = new Intent().setClass(this, Tab_View2.class);
 
     spec = tabHost.newTabSpec("Second").setIndicator("")
                   .setContent(intent);
     tabHost.addTab(spec);

     /** TAB3 **/
     intent = new Intent().setClass(this, Tab_View3.class);
 
     spec = tabHost.newTabSpec("Third").setIndicator("")
                   .setContent(intent);
     tabHost.addTab(spec);

     /** TAB4 **/
     intent = new Intent().setClass(this, Tab_View4.class);
 
     spec = tabHost.newTabSpec("Third").setIndicator("")
                   .setContent(intent);
     tabHost.addTab(spec);
   
     // Set drawable images to tab
 tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2);
 tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3);
 tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4);
   // Set Tab1 as Default tab and change image
     tabHost.getTabWidget().setCurrentTab(0);
     tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1_over);    
}
@Override
public void onTabChanged(String tabId) {
   
    /** Called when tab changed **/
   
//** Check current selected tab and change according images **/
   
   for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
    if(i==0)
       tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab1_over);
    else if(i==1)
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab2_over);
    else if(i==2)
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab3_over);
    else if(i==3)
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab4_over);
   
   }  
 
   Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
 
   if(tabHost.getCurrentTab()==0)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab1);
   else if(tabHost.getCurrentTab()==1)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab2);
   else if(tabHost.getCurrentTab()==2)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab3);
   else if(tabHost.getCurrentTab()==3)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab5);
 
}

}



 Create four class files Tab_View1.java,Tab_View2.java,Tab_View3.java,Tab_View4.java

Enjoy........

 


How to Run Google map API v2 on emulator

How to Install and Run Google Maps API v2:
                  Google Play Services installed in you AVD, but due to Google Privacy and Security Reasons it is not Allowed to Install Google Play and you will not be able to even add your Google account.

Create new emulator with next configuration:
Start emulator and install following applications: 

  • GoogleLoginService.apk, 
  • GoogleServicesFramework.apk,
  • Phonesky.apk.


You can do this with next commands:

> adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock0 /system
> adb shell chmod 777 /system/app
> adb push Each_of_the_3_apk_files.apk /system/app/


Links to download apk files- www.dropbox.com/sh/20hcf8kxdk19z3c/Ukc8uGnk7p

GoogleLoginService.apk - www.dropbox.com/s/lfde3juixuy88rg/GoogleLoginService.apk
GoogleServicesFramework.apk - www.dropbox.com/s/9kurwyhbbuecaea/GoogleServicesFramework.apk
Phonesky.apk - www.dropbox.com/s/9x8924gtb52ksn6/Phonesky.apk

    Install on the emulator Google Play services and Google maps. I have an error 491, if installing from Google Play store. I uploaded apps to the emulator and run installation from it.

 Links to apps:

Google maps - www.dropbox.com/s/koo4wiwqg8agy8n/com.google.android.apps.maps-1.apk
Google Play services - www.dropbox.com/s/bh058hbrelccfsr/com.google.android.gms-2.apk

Try this.......