Tutorial Membuat aplikasi Swipe
1. Buat File Baru Android Application Project,
Tentukan nama Aplikasi dan Project, kemudian klik next
Tunggu sampai proses berikutnya,
Langkah berikutnya adalah mengkonfigurasi Icon Launcher
Langkah berikutnya membuat Activity, pilih blank activity, klik next
Biarkan Nama Activity default seperti gambar berikut, klik tombol Finish
2. Buat Beberapa Buah Filel XML dengan Cara Klik pada Layout dan Pilih New ->android xml File
Lalu beri nama file xml contoh: halaman1
Lakukan langkah ini sampai tiga kali sehingga kita mendapatkan tiga buah file xml yaitu halaman1,
halaman2 dan halaman3.
Kemudian untuk masing masing file xml isi kan code berikut ini.
Script Untuk halaman1.xml
<?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"
android:background="#CCFF66"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Page3"
android:textSize="33dp"
android:layout_marginTop="22dp"
android:layout_marginLeft="22dp"
/>
</LinearLayout>
Script Untuk halaman2.xml
<?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"
android:background="#66FFFF"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Page2"
android:textSize="33dp"
android:layout_marginTop="22dp"
android:layout_marginLeft="22dp"
/>
</LinearLayout>
Script Untuk halaman3.xml
<?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"
android:background="#CCFF66"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Page3"
android:textSize="33dp"
android:layout_marginTop="22dp"
android:layout_marginLeft="22dp"
/>
</LinearLayout>
3. Edit file Activity_Main.xml yang ada dalam folder “Layout”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/customviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
4. Buat sebuah class baru
pada folder “src” klik kanan di com. Nama aplikasi, kemudian pilih New->class
ikuti seperti gambar dibawah.
Kemudian berikan nama kelas dengan nama CustomPagerAdapter, kseperti
gambar dibawah ini
Kemudian edit file CustomPagerAdapter .java seperti script dibawah ini :
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
public class CustomPagerAdapter extends PagerAdapter {
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)
collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.halaman1;
break;
}
case 1: {
resId = R.layout.halaman2;
break;
}
case 2: {
resId = R.layout.halaman3;
break;
}
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public int getCount() {
return 3;
}
}
5. Edit Class MainActivity.java
package com.fokmenus.aplikasiswaip;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create and set adapter
CustomPagerAdapter adapter = new CustomPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Jika sudah selesai, eksekusi aplikasi anda.

Latihan android

  • 1.
    Tutorial Membuat aplikasiSwipe 1. Buat File Baru Android Application Project, Tentukan nama Aplikasi dan Project, kemudian klik next Tunggu sampai proses berikutnya,
  • 2.
    Langkah berikutnya adalahmengkonfigurasi Icon Launcher Langkah berikutnya membuat Activity, pilih blank activity, klik next
  • 3.
    Biarkan Nama Activitydefault seperti gambar berikut, klik tombol Finish 2. Buat Beberapa Buah Filel XML dengan Cara Klik pada Layout dan Pilih New ->android xml File Lalu beri nama file xml contoh: halaman1
  • 4.
    Lakukan langkah inisampai tiga kali sehingga kita mendapatkan tiga buah file xml yaitu halaman1, halaman2 dan halaman3. Kemudian untuk masing masing file xml isi kan code berikut ini. Script Untuk halaman1.xml <?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" android:background="#CCFF66" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Page3" android:textSize="33dp" android:layout_marginTop="22dp" android:layout_marginLeft="22dp" /> </LinearLayout> Script Untuk halaman2.xml <?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" android:background="#66FFFF" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Page2" android:textSize="33dp" android:layout_marginTop="22dp" android:layout_marginLeft="22dp" /> </LinearLayout> Script Untuk halaman3.xml <?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"
  • 5.
    android:background="#CCFF66" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Page3" android:textSize="33dp" android:layout_marginTop="22dp" android:layout_marginLeft="22dp" /> </LinearLayout> 3. Edit fileActivity_Main.xml yang ada dalam folder “Layout” <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/customviewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 4. Buat sebuah class baru pada folder “src” klik kanan di com. Nama aplikasi, kemudian pilih New->class ikuti seperti gambar dibawah. Kemudian berikan nama kelas dengan nama CustomPagerAdapter, kseperti gambar dibawah ini
  • 6.
    Kemudian edit fileCustomPagerAdapter .java seperti script dibawah ini : import android.content.Context; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; public class CustomPagerAdapter extends PagerAdapter { public Object instantiateItem(View collection, int position) { LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resId = 0; switch (position) { case 0: { resId = R.layout.halaman1; break; } case 1: { resId = R.layout.halaman2; break; } case 2: {
  • 7.
    resId = R.layout.halaman3; break; } } Viewview = inflater.inflate(resId, null); ((ViewPager) collection).addView(view, 0); return view; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == ((View) arg1); } @Override public Parcelable saveState() { return null; } @Override public int getCount() { return 3; } } 5. Edit Class MainActivity.java package com.fokmenus.aplikasiswaip; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.Menu; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create and set adapter CustomPagerAdapter adapter = new CustomPagerAdapter(); ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager); myPager.setAdapter(adapter); myPager.setCurrentItem(0); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
  • 8.
    Jika sudah selesai,eksekusi aplikasi anda.