public class MyExpadableListActivity extends ExpandableListActivity implements
ExpandableListView.OnChildClickListener {
ExpandableListAdapter mAdapter;
Context mContext;
static final String groups[] = { "Chats", "Contacts (289)",
"Group Chat (7)", "e-Card (137)", "Pending (37)",
"Bookmarks" };
static final String children[][] = {
{ "ListofChats" },
{ "Group", "groupt01", "groupt02", "groupt03" },
{ "Groupd 01", "", "Group 02", "", "Group 03", "" },
{ "E-Card-01", "", "E-Card-02", "",
"Business Card Received", "" },
{ " Req", "", " Req", "", " Req", "" } };
private static final String TAG = "Hookup";
XMPPConnection connection = SignInMainActivity.connection;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyExpandableListAdapter(this);
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
System.out.println("Insidded onContextItemSelected");
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
@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");
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 {
Context mContext;
public MyExpandableListAdapter() {
// TODO Auto-generated constructor stub
}
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 View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.i(TAG, "getChildView");
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = null;
TextView tt = null;
if (groupPosition == 4) {
v = layoutInflater.inflate(R.layout.button_group, null);
} else {
v = layoutInflater.inflate(R.layout.album_row, null);
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);
if (groupPosition == 0) {
ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
icon.setImageResource(R.drawable.add_picture);
} else {
ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
icon.setImageResource(R.drawable.add_picture);
}
}
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) {
TextView groupTitle = null;
ImageView imgDot;
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = null;
v = layoutInflater.inflate(R.layout.button_group, null);
groupTitle = (TextView) v.findViewById(R.id.TextView01);
String myText = this.getGroup(groupPosition).toString();
groupTitle.setText(myText);
imgDot = (ImageView) v.findViewById(R.id.ImageView01);
imgDot.setVisibility(View.VISIBLE);
return v;
}
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) {
}
} // closing of MyExpandableListAdapter
} // closing of MyExpadableListActivity
XML Viewxml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingRight="?android:attr/scrollbarSize"
android:layout_weight="1" android:background="#fafafa">android:id="@+id/rowicon" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="6dip" />
android:id="@+id/text1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
/>android:id="@+id/checkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="2dip"
android:focusable="true" android:clickable="true" android:gravity="center_vertical"
android:orientation="vertical" android:duplicateParentState="true"
android:visibility="visible"
android:text="myTest"
/>
Andxml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingRight="?android:attr/scrollbarSize"
android:layout_weight="1" android:background="#fafafa">
Learn and Share Android
Friday, January 6, 2012
Dynamic Expandable ListView in Android
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
1. Intent Services
2. About threads and communication between them different thread
3. About Main thread and user interface thread
4. Async Tasks
3. About Main thread and user interface thread
4. Async Tasks
| Service | Thread | IntentService | AsyncTask | |
|---|---|---|---|---|
| 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] |
| Trigger | Call to method onStartService() | Thread start() method | Intent | Call to method execute() |
| Triggered From (thread) | Any thread | Any Thread | Main Thread (Intent is received on main thread and then worker thread is spawed) | Main Thread |
| Runs On (thread) | Main Thread | Its own thread | Separate worker thread | Worker 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 . . . . . ."
Subscribe to:
Posts (Atom)