Sunday, July 17, 2011

Create Database SQLite,Create Table and columns and Insert Data

package Database;
import android.content.Intent;
import android.database.SQLException;
import java.util.Locale;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;Save Now
import android.app.Activity;
import android.content.ContentValues;

public class SQLiteMainActivity extends Activity {
SQLiteDatabase databaseObj;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createOpenDatabase();
createTable();
insertInTable();
}

boolean createOpenDatabase() {
try {
String dbName = "MyDataBase";
databaseObj= openOrCreateDatabase(dbName,
SQLiteDatabase.CREATE_IF_NECESSARY, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}

boolean createTable() {
databaseObj.setVersion(1);
databaseObj.setLocale(Locale.getDefault());
databaseObj.setLockingEnabled(true);
try {
final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS TBL_MyTable ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "Name TEXT,"
+ "Address TEXT,"
+ "Country TEXT)";

databaseObj.execSQL(CREATE_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}

void insertInTable() {
ContentValues values = new ContentValues();
values.put("Name", "NasirAftab");
values.put("Address", "JLT Dubai");
values.put("Country", "UAE");
try {
databaseObj.insertOrThrow("TBL_MyTable", null, values);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Create Database SQLite,Create Table and columns and Insert Data

package Database;
import android.content.Intent;
import android.database.SQLException;
import java.util.Locale;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;

public class SQLiteMainActivity extends Activity {
SQLiteDatabase databaseObj;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createOpenDatabase();
createTable();
insertInTable();
}

boolean createOpenDatabase() {
try {
String dbName = "MyDataBase";
databaseObj= openOrCreateDatabase(dbName,
SQLiteDatabase.CREATE_IF_NECESSARY, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}

boolean createTable() {
databaseObj.setVersion(1);
databaseObj.setLocale(Locale.getDefault());
databaseObj.setLockingEnabled(true);
try {
final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS TBL_MyTable ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "Name TEXT,"
+ "Address TEXT,"
+ "Country TEXT)";

databaseObj.execSQL(CREATE_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}

void insertInTable() {
ContentValues values = new ContentValues();
values.put("Name", "NasirAftab");
values.put("Address", "JLT Dubai");
values.put("Country", "UAE");
try {
databaseObj.insertOrThrow("TBL_MyTable", null, values);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Saturday, May 7, 2011

Develop Android Date Picker



public class MyDatePicker extends Activity { private int myYear, myMonth, myDay;
static final int ID_DATEPICKER = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button datePickerButton = (Button)findViewById(R.id.datepickerbutton);
datePickerButton.setOnClickListener(datePickerButtonOnClickListener);
}
private Button.OnClickListener datePickerButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
myYear = c.get(Calendar.YEAR);
myMonth = c.get(Calendar.MONTH);
myDay = c.get(Calendar.DAY_OF_MONTH);
showDialog(ID_DATEPICKER);
}
};
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case ID_DATEPICKER:
Toast.makeText(DatePicker.this, "- onCreateDialog -",
Toast.LENGTH_LONG).show();
return new DatePickerDialog(this,
myDateSetListener,
myYear, myMonth, myDay);
default:
return null;
}
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case ID_DATEPICKER:
((DatePickerDialog) dialog).updateDate(myYear, myMonth, myDay);
break;
}
}

private DatePickerDialog.OnDateSetListener myDateSetListener
= new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
String date = "Year: " + String.valueOf(year) + "\n"
+ "Month: " + String.valueOf(monthOfYear+1) + "\n"
+ "Day: " + String.valueOf(dayOfMonth);
Toast.makeText(DatePicker.this, date,
Toast.LENGTH_LONG).show();
}
};
}


main.xml



Wednesday, May 4, 2011

Create HTTP connection in Android and get input and output Streams


String PROFILE_SERVER_URL="http://your-erver-URL";
URL url = new URL(BF_PROFILE_SERVER_URL);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
OutputStream out = urlConnection.getOutputStream();
XmlWriter writer = new XmlWriter(out);
InputStream in = urlConnection.getInputStream();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(in));


Android Thread Constructs: Comparisons

In below table you can learn some Comparisons in. . .
1.
Intent Services
2. About threads and communication between them different thread
3. About Main thread and user interface thread
4. Async Tasks



ServiceThreadIntentServiceAsyncTask
When to use ?Task with no UI, but shouldn't be too long. Use threads within service for long tasks.- Long task in general.

- For tasks in parallel use Multiple threads (traditional mechanisms)
- Long task usuallywith no communication to main thread.
(Update)- If communication is required, can use main thread handler or broadcast intents[3]

- When callbacks are needed (Intent triggered tasks).
- Long task having to communicate with main thread.

- For tasks in parallel use multiple instances OR Executor [1]
TriggerCall to method
onStartService()
Thread start() methodIntentCall to method execute()
Triggered From (thread)Any threadAny ThreadMain Thread (Intent is received on main thread and then worker thread is spawed)Main Thread
Runs On (thread)Main ThreadIts own threadSeparate worker threadWorker thread. However, Main thread methods may be invoked in between to publish progress.
Limitations /
Drawbacks
May block main thread- Manual thread management

- Code may become difficult to read
- Cannot run tasks in parallel.

- Multiple intents are queued on the same worker thread.
- one instance can only be executed once (hence cannot run in a loop) [2]

- Must be created and executed from the Main thread

Learn and Share Android: Hi Dear Starting Android Blog

Learn and Share Android: Hi Dear Starting Android Blog: " Starting this blog hope this will help to all of you . . . . . ."

Add Dynamic groups and childes in ExpandableListView in Android

package src.com;
import android.app.Activity;
import android.os.Bundle;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Hookup extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
Context mContext;

private String[] groups = { "BFBFBF", "Testing" };
private String[][] children = { { "M", "N" }, { "One", "Four", "Six" } };

private static final String TAG = "Hookup";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter(this);
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Hookup Menu");
menu.add(0, 0, 0, R.string.hello);
}

public boolean onContextItemSelected(MenuItem item) {
System.out.println("Insidded onContextItemSelected");

// Intent intent = new Intent();
// intent.setClass(this, HookupMapView.class);
// startActivity(intent);
Log.i(TAG, "onContextItemSelected");
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
.getMenuInfo();

String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView
.getPackedPositionType(info.packedPosition);

if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView
.getPackedPositionChild(info.packedPosition);
Toast.makeText(
this,
title + ": Child " + childPos + " clicked in group "
+ groupPos, Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked",
Toast.LENGTH_SHORT).show();

return true;
}

return false;
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for
// groups[i].
// private String[] groups = { "Xxxxxxx", "yyyyyy near me" };
// private String[][] children = { { "A", "B", "C" },
// { "1", "2", "3" } };

public MyExpandableListAdapter(Context context) {
mContext = context;
}

public Object getChild(int groupPosition, int childPosition) {
Log.i(TAG, "getChild");
return children[groupPosition][childPosition];
}

public long getChildId(int groupPosition, int childPosition) {
Log.i(TAG, "getChildId");
return childPosition;
}

public int getChildrenCount(int groupPosition) {
Log.i(TAG, "getChildId");
return children[groupPosition].length;
}

public LinearLayout getGenericView(String string) {
Log.i(TAG, "getGenericView");
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);

LinearLayout ll = new LinearLayout(mContext);
ImageView icon = new ImageView(mContext);
icon.setImageResource(R.drawable.icon);
icon.setPadding(35, 0, 0, 0);
ll.addView(icon);

TextView textView = new TextView(mContext);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
// CheckBox cBox = new CheckBox(mContext);
ll.setLayoutParams(lp);
ll.addView(textView);
// ll.addView(cBox);
textView.setText(string);
// cBox.setClickable(true);
// CheckBox cb = new CheckBox(mContext);
// ll.addView(cb);
return ll;

}

public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.i(TAG, "getChildView");
// String myText =
// this.getChild(groupPosition,childPosition).toString();
// LinearLayout ll = getGenericView(myText+"BCD");
//textView.setText(getChild(groupPosition,childPosition).toString())
// ;
// return ll;
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.bluetest_row, null);
TextView tt = (TextView) v.findViewById(R.id.text1);
String myText = this.getChild(groupPosition, childPosition)
.toString();
tt.setText(myText);
CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
cb.setVisibility(View.VISIBLE);
ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
icon.setImageResource(R.drawable.icon);
return v;
}

public Object getGroup(int groupPosition) {
Log.i(TAG, "getGroup");
return groups[groupPosition];
}

public int getGroupCount() {
Log.i(TAG, "getGroupCount");
return groups.length;
}

public long getGroupId(int groupPosition) {
Log.i(TAG, "getGroupId");
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.i(TAG, "getGroupView");
String myText = this.getGroup(groupPosition).toString();
return getGenericView(myText);
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
Log.i(TAG, "isChildSelectable");
return true;
}

public boolean hasStableIds() {
Log.i(TAG, "hasStableIds");
return true;
}

public void registerDataSetObserver(DataSetObserver observer) {
}

}

}


bluetest_row.xml


android:layout_weight="1" android:background="#fafafa">
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"
/>
android:text="myTest"
/>

Saturday, April 2, 2011

Hi Dear Starting Android Blog

I am starting this blog hope this will help to all of you . . . . .

I am looking for Contributor for my this Blog .

You are welcome to be Admin for this  Blog Posting ...