Tentang shareilmu2ilmu.blogspot.com

http://shareilmu2ilmu.blogspot.com/ adalah blog tentang komputer yang di buat untuk Pembelajaran, tugas-tugas,aplikasi komputer,Coding,pemograman visual, sistem manajemen basis data

Sunday, 29 December 2013

TUTORIAL PEMBUATAN Database(Part 1). PADA ADT BUNDLE ANDROID



  1. Buat Project Baru
  2. Desain interface baru untuk tampilan awal program
  3. Buat Layout baru dengan nama create data dengan tampilan seperti berikut :
    4.Buat Class baru pada folder src dengan nama createData,tuliskan coding seperti di bawah ini:
    package com.example.dbandroid;


    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;


    public class createData extends Activity implements OnClickListener {
    private Button Submit;
    private EditText nama,merk,harga;
    private DBDataSource datasource;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createdata);

    Submit=(Button) findViewById(R.id.btnSubmit);
    Submit.setOnClickListener(this);
    nama=(EditText) findViewById(R.id.txtnama);
    merk=(EditText) findViewById(R.id.txtmerk);
    harga=(EditText) findViewById(R.id.txtharga);

    datasource=new DBDataSource(this);
    datasource.open();
    }
    public void onClick(View arg0){
    String nm=null;
    String mk=null;
    String hrg=null;

    Barang barang=null;

    if(nama.getText()!=null&&merk.getText()!=null && harga.getText()!=null){
    nm=nama.getText().toString();
    mk=merk.getText().toString();
    hrg=harga.getText().toString();
    }
    switch(arg0.getId())
    {
    case R.id.btnSubmit:
    barang=datasource.createBarang(nm,mk,hrg);
    Toast.makeText(this, "masuk Barang\n"+
    "nama"+barang.getNama_barang()+
    "merk"+barang.getMerk_barang()+
    "harga"+barang.getHarga_barang(),
    Toast.LENGTH_LONG).show();
    break;
    }

    }

    }
    5.Buat class baru dengan nama dbHelper,dan tuliskan coding berikut :
    package com.example.dbandroid;


    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;


    public class dbHelper extends SQLiteOpenHelper {
    public static final String TABLE_NAME="data_inventory";
    public static final String COLOUMN_ID="_id";
    public static final String COLOUMN_NAME="nama_barang";
    public static final String COLOUMN_MERK="merk_barang";
    public static final String COLOUMN_HARGA="harga_barang";
    private static final String db_name="inventory.db";
    private static final Integer db_version=1;

    private static final String db_create="create table"
    +TABLE_NAME+"("
    +COLOUMN_ID+"integer primary key autoincrement,"
    +COLOUMN_NAME+"varchar(50) not null,"
    +COLOUMN_MERK+"varchar(50) not null,"
    +COLOUMN_HARGA+"varchar(50) not null);";

    public dbHelper(Context sia){
    super(sia,db_name,null,db_version);
    }
    public void onCreate(SQLiteDatabase db){
    db.execSQL(db_create);
    }
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
    Log.w(dbHelper.class.getName(),"Upgrading database from"+
    oldVersion+"to"+newVersion+"which will destroy all old data");
    db.execSQL("Drop Table If Exists"+TABLE_NAME);
    onCreate(db);
    }
    }
    6.Buat Class baru dengan namaDBDataSource dengan coding seperti berikut:
    package com.example.dbandroid;


    import android.content.ContentValues;
    import android.content.Context;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;


    public class DBDataSource {
    private SQLiteDatabase database;
    private dbHelper dbHelper;

    private String[] allcolomns={
    dbHelper.COLOUMN_ID,
    dbHelper.COLOUMN_NAME,
    dbHelper.COLOUMN_MERK,
    dbHelper.COLOUMN_HARGA
    };

    public DBDataSource(Context c){
    dbHelper=new dbHelper(c);
    }
    public void open()throws SQLException{
    database=dbHelper.getWritableDatabase();
    }
    public void close(){
    dbHelper.close();
    }
    public Barang createBarang(String nm,String mk,String hrg){
    ContentValues values=new ContentValues();
    values.put(dbHelper.COLOUMN_NAME, nm);
    values.put(dbHelper.COLOUMN_MERK, mk);
    values.put(dbHelper.COLOUMN_HARGA, hrg);

    long insertId=database.insert(dbHelper.TABLE_NAME,null, values);
    Cursor cursor=database.query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit, cancellationSignal)
    }
    }



TUTORIAL PEMBUATAN CANVAS PADA ADT BUNDLE ANDROID



  1. Buat Projek Baru.
  2. Isikan Kriteria dan desain sesuai yang anda inginkan.
  3. Ketikan Coding dibawah ini pada layout mainActivity.


<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


</RelativeLayout>


  1. Pilih gambar dan masukan pada folder xxhdpi.
  2. Isikan coding berikut pada Activity Main pada src.
package com.example.canvas2;


import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new canvas7(this));
}

static public class canvas7 extends View{
Bitmap gambarkudangambarmu;

public canvas7(Context context){
super(context);
gambarkudangambarmu = BitmapFactory.decodeResource(context.getResources(),R.drawable.aaa);
}
@SuppressLint("DrawAllocation")
protected void onDraw(Canvas canvas){
Paint paint=new Paint();
canvas.drawBitmap(gambarkudangambarmu, 100, 200,paint);
}
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



  1. Pada coding R.drawable.aaa,”aaa” diganti dengan nama gambar yang anda masukan tadi pada folder xx-hdpi.
  2. Output :

Sunday, 15 December 2013

TUTORIAL PEMBUATAN TABHOST PADA ADT BUNDLE ANDROID

1. Buat Proyek android .
2. Tentukan desain untuk aplikasinya.
3. Buat proyek blank activity
4 .Masukan Main Activity Details

5. Tambahkan Android’s Support library
Dengan ADT plugin version 20.0.2, Android’s support library adalah menambahkan untuk automisasi proyek. Jika tidak secara automatis ditambahkan, Anda dapat menambahkannya dengan cara click kanan “NavigationTabDemo” ( Package Explorer ) dan pilih Android Tools -> Add Support Library


6. Buat folder baru drawable pada res dan buat file baru namai dengan android.xml dan tuliskan kode berikut:
 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/android_selected" />
    <item
        android:drawable="@drawable/android_unselected" />
</selector>

7.Buat file baru dengan nama apple.xml pada res/drawable directory buat seperti rangkaian diatas dan tuliskan kode berikut:
 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/apple_selected" />
 
    <item
        android:drawable="@drawable/apple_unselected" />
</selector>
 

8.Ubah layout file res/layout/activity_main.xml
 
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>
 
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
 
        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
 
    </LinearLayout>
</TabHost>

9. Buat Class baru  src/in/wptrafficanalyzer/navigationtabdemo/AndroidFragment.java
 
package in.wptrafficanalyzer.navigationtabdemo;
 
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
 
public class AndroidFragment extends ListFragment{
 
    /** An array of items to display in ArrayList */
    String android_versions[] = new String[]{
        “Nama:Dionisius Riyan Edlianto”,
              “Kelas:SI12A”,
              “NIM:12.12.0009”
 
    };
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 
        /** Creating array adapter to set data in listview */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
 
        /** Setting the array adapter to the listview */
        setListAdapter(adapter);
 
        return super.onCreateView(inflater, container, savedInstanceState);
    }
 
    @Override
    public void onStart() {
        super.onStart();
 
        /** Setting the multiselect choice mode for the listview */
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}
 

10. Buat file class baru  src/in/wptrafficanalyzer/navigationtabdemo/AppleFragment.java
 
package in.wptrafficanalyzer.navigationtabdemo;
 
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
 
public class AppleFragment extends ListFragment{
 
    /** An array of items to display in ArrayList */
    String apple_versions[] = new String[]{
     “Alamat blog:Shareilmu2ilmu.blogspot.com”,
     “Mata kuliah:Pemmograman Mobile”        
 
    };
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 
        /** Creating array adapter to set data in listview */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, apple_versions);
 
        /** Setting the array adapter to the listview */
        setListAdapter(adapter);
 
        return super.onCreateView(inflater, container, savedInstanceState);
    }
 
    @Override
    public void onStart() {
        super.onStart();
 
        /** Setting the multiselect choice mode for the listview */
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}
 

14. Buat file class baru src/in/wptrafficanalyzer/navigationtabdemo/DummyTabContent.java
 
package in.wptrafficanalyzer.navigationtabdemo;
 
import android.content.Context;
import android.view.View;
import android.widget.TabHost.TabContentFactory;
 
public class DummyTabContent implements TabContentFactory{
    private Context mContext;
 
    public DummyTabContent(Context context){
        mContext = context;
    }
 
    @Override
    public View createTabContent(String tag) {
        View v = new View(mContext);
        return v;
    }
}
 

15. Ubah file pada src/in/wptrafficanalyzer/navigationtabdemo/MainActivity.java
 
package in.wptrafficanalyzer.navigationtabdemo;
 
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TabHost;
 
public class MainActivity extends FragmentActivity {
    TabHost tHost;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tHost = (TabHost) findViewById(android.R.id.tabhost);
        tHost.setup();
 
        /** Defining Tab Change Listener event. This is invoked when tab is changed */
        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
 
            @Override
            public void onTabChanged(String tabId) {
                android.support.v4.app.FragmentManager fm =   getSupportFragmentManager();
                AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android");
                AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple");
                android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
 
                /** Detaches the androidfragment if exists */
                if(androidFragment!=null)
                    ft.detach(androidFragment);
 
                /** Detaches the applefragment if exists */
                if(appleFragment!=null)
                    ft.detach(appleFragment);
 
                /** If current tab is android */
                if(tabId.equalsIgnoreCase("android")){
 
                    if(androidFragment==null){
                        /** Create AndroidFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new AndroidFragment(), "android");
                    }else{
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        ft.attach(androidFragment);
                    }
 
                }else{    /** If current tab is apple */
                    if(appleFragment==null){
                        /** Create AppleFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");
                     }else{
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        ft.attach(appleFragment);
                    }
                }
                ft.commit();
            }
        };
 
        /** Setting tabchangelistener for the tab */
        tHost.setOnTabChangedListener(tabChangeListener);
 
        /** Defining tab builder for Andriod tab */
        TabHost.TabSpec tSpecAndroid = tHost.newTabSpec("android");
        tSpecAndroid.setIndicator("Tab 1",getResources().getDrawable(R.drawable.android));
        tSpecAndroid.setContent(new DummyTabContent(getBaseContext()));
        tHost.addTab(tSpecAndroid);
 
        /** Defining tab builder for Apple tab */
        TabHost.TabSpec tSpecApple = tHost.newTabSpec("apple");
        tSpecApple.setIndicator("Tab 2",getResources().getDrawable(R.drawable.apple));
        tSpecApple.setContent(new DummyTabContent(getBaseContext()));
        tHost.addTab(tSpecApple);
 
    }
}
 

16. Ubah AndroidManifest.xml
 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.navigationtabdemo"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black" >
 
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Hasil Output:

Tab.1
Tab 2.