Tuesday, November 20, 2012

scroll view example


package com.example.scrollexample;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;

public class MainActivity extends Activity
{

@Override
  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

ImageView iv = (ImageView) findViewById(R.id.imageView1);

ScrollView sv = (ScrollView) findViewById(R.id.scrollView1);
sv.setHorizontalScrollBarEnabled(true);
Resources res = getResources();
BitmapDrawable bitmap = (BitmapDrawable) res.
    getDrawable(R.drawable.bts_silom_line_map);
int imgW = bitmap.getIntrinsicWidth();
int imgH = bitmap.getIntrinsicHeight();

iv.setImageDrawable(bitmap);

ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) iv.getLayoutParams();
params.width = imgW;
params.height = imgH;

  }
}

//////////////////////////////////////////////////////////////////////////////////


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
<ScrollView
       android:id="@+id/scrollView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
<ImageView
       android:id="@+id/imageView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
   </ScrollView>
    </HorizontalScrollView>
</LinearLayout>

Download:
http://www.mediafire.com/?ge1jql7g7s96mmb

Monday, November 19, 2012

nice SlidingDrawer layout


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />

    <SlidingDrawer
        android:id="@+id/drawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="horizontal" >

        <ImageView
            android:id="@id/handle"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/next_button" />

        <LinearLayout
            android:id="@id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#55000000"
            android:orientation="vertical"
            android:padding="10dp" >

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" - Button - " />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" - Button - " />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" - Button - " />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" - Button - " />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" - Button - " />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" - Button - " />
        </LinearLayout>
    </SlidingDrawer>

</FrameLayout>

get back Logcat

window => show view => Logcat

Saturday, November 17, 2012

android database raw query


package home.database;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class CRUDonDB  {

private final String SAMPLE_DB_NAME = "shoppingDb";
private final String SAMPLE_TABLE_NAME = "item_info";
Context context;
SQLiteDatabase sampleDB = null;
ArrayList<String> results = new ArrayList<String>();

public CRUDonDB(Context _context)
{
context = _context;

try
{
sampleDB = context.openOrCreateDatabase(SAMPLE_DB_NAME, context.MODE_WORLD_WRITEABLE,
null);

sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + SAMPLE_TABLE_NAME
+ " (stationId VARCHAR, stationName VARCHAR,"
+ " shoppingMallName VARCHAR,productName VARCHAR,"
+ " purchased INT(3),UNIQUE(shoppingMallName, productName) ) ;");

Log.d("---------database created-----------", " ");

}
catch (SQLiteException se)
{
Log.e(getClass().getSimpleName(),
"Could not create or Open the database");
Log.d("---------catch-----------", " ");
}
finally
{
Log.d("---------finally-----------", " ");
// if (sampleDB != null)
// sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
//sampleDB.close();
}
}

public void insert_item_into_db(String etItemOne, String etItemTwo,String etItemThree,String etItemFour)
{
if( !etItemOne.equals("") )
{
try
{
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME
+ " Values ('s3','silom','nondon','"+etItemOne+"','0');");

Log.d("---------insert_item_into_db()-----------", etItemOne.toString() );
}
catch(SQLException e)
{
Log.d("----------SQLException------------", " ");
}
}

if( !etItemTwo.equals("") )
{
try
{
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME
+ " Values ('s3','silom','nondon','"+etItemTwo+"','0');");

Log.d("---------insert_item_into_db()-----------", etItemTwo.toString() );
}
catch(SQLException e)
{
Log.d("----------SQLException------------", " ");
}

}

if( !etItemThree.equals("") )
{
try
{
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME
+ " Values ('s3','silom','nondon','"+etItemThree+"','0');");

Log.d("---------insert_item_into_db()-----------", etItemThree.toString() );
}
catch(SQLException e)
{
Log.d("----------SQLException------------", " ");
}
}

if( !etItemFour.equals("") )
{
try
{
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME
+ " Values ('s3','silom','nondon','"+etItemFour+"','0');");

Log.d("---------insert_item_into_db()-----------", etItemFour.toString() );
}
catch(SQLException e)
{
Log.d("----------SQLException------------", " ");
}
}


}

public Cursor select_item_from_db()
{
Cursor cursor = sampleDB.rawQuery("SELECT * FROM "
+ SAMPLE_TABLE_NAME , null);

/*if (cursor != null) {
if (c.moveToFirst()) {
do {
String firstName = cursor.getString(cursor
.getColumnIndex("FirstName"));
int age = cursor.getInt(cursor.getColumnIndex("Age"));

Log.d("---------firstName"+firstName+"-----------", " ");

results.add("" + firstName + ",Age: " + age);
} while (cursor.moveToNext());
}
}
*/
Log.d("---------select_item_from_db()-----------", " ");

return cursor;
}

public void update_purchased_info(String productName)
{
try
{
sampleDB.execSQL("update " + SAMPLE_TABLE_NAME
+ " set purchased=1 where productName='"+productName+"'");

Log.d("----------update success------------", " ");
}
catch(SQLException e)
{
Log.d("----------update SQLException="+e.getStackTrace().toString()+"------------", " ");
}

}

public void delete_item_info(String productName)
{
try
{
sampleDB.execSQL("delete from " + SAMPLE_TABLE_NAME
+ " where productName='"+productName+"'");

Log.d("----------delete success------------", " ");
}
catch(SQLException e)
{
Log.d("----------update SQLException="+e.getStackTrace().toString()+"------------", " ");
}

}

public void close_db()
{
sampleDB.close();
}

}

android simple alert dialog


package com.example.alertdialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

AlertDialog.Builder builder;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

showDialog();

AlertDialog alert = builder.create();
alert.show();

}

void showDialog() {
//final CharSequence[] items = { "Red", "Green", "Blue" };
final CharSequence[] items = getResources().getStringArray(R.array.countries_array);
builder = new AlertDialog.Builder(this);
builder.setTitle("Set text color");

builder.setItems(items, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int selectItem) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
items[selectItem],
Toast.LENGTH_SHORT).show();
}
});

}

}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countries_array">
        
        <item name="bangladesh">bangladesh</item>
        <item name="usa">usa</item>
        <item name="russia">russia</item>
        <item name="england">england</item>
        
    </string-array>
</resources>

Download:
http://www.mediafire.com/?pp0fyijhpm5ks52