Membuat Aplikasi Android Untuk Pemula PDF
Membuat Aplikasi Android Untuk Pemula PDF
ii
6.2. TEXTVIEW ..................................................................................... 62
6.3. EDITTEXT ...................................................................................... 64
6.4. BUTTON ......................................................................................... 66
6.5. IMAGEVIEW ................................................................................... 69
6.6. IMAGEBUTTON .............................................................................. 70
6.7. SPINNER ........................................................................................ 73
6.8. CHECKBOX .................................................................................... 75
6.9. AUTOCOMPLETETEXTVIEW .......................................................... 77
6.10. TOGGLEBUTTON .......................................................................... 79
6.11. RADIOBUTTON ............................................................................ 81
6.12. RADIOGROUP .............................................................................. 83
6.13. PROGRESSBAR ............................................................................. 85
7. LAYOUT ................................................................................................. 88
7.1. ATRIBUT LAYOUT .......................................................................... 88
7.2. FRAME LAYOUT ............................................................................. 92
7.3. LINEARLAYOUT ............................................................................. 94
7.4. RELATIVELAYOUT ......................................................................... 97
8. DIALOG ................................................................................................ 100
8.1. TOAST .......................................................................................... 100
8.2. SNACK BAR.................................................................................. 100
8.3. ALERT DIALOG ............................................................................ 101
8.4. PROGRESS DIALOG ...................................................................... 101
8.5. ACTIVITY SEBAGAI DIALOG ......................................................... 102
9. MENU.................................................................................................... 103
9.1. MENU SEBAGAI ACTION .............................................................. 104
9.2. POPUP MENU .............................................................................. 105
9.3. CONTEXT MENU .......................................................................... 107
10. SQLITE ............................................................................................... 110
10.1. SQLITEHELPER ........................................................................ 114
10.2. MELAKUKAN QUERY DATA ........................................................ 116
10.3. MENAMBAH DATA ..................................................................... 116
10.4. MENGUPDATE DATA .................................................................. 117
12. LIST VIEW......................................................................................... 118
12.1. APA ITU LIST VIEW? .................................................................. 118
12.2. DEFAULT ADAPTER ................................................................... 118
13. PROJECT: MEMBUAT APLIKASI SEKOLAHKU .................. 120
iii
Ebook ini tersedia gratis bagi siapa saja yang ingin belajar
membuat Aplikasi Android.
Anda bisa mendapatkan versi cetak dari Ebook ini, Anda bisa
mengunjungi halaman berikut:
http://bit.ly/VersiCetak
www.rumahcoding.co.id
@idrumahcoding
iv
Jika Ingin Serius Belajar, Anda Harus Invest Pada Buku
Versi Cetak
v
@idrumahcoding
www.oracle.com/technetwork/java/javase/downloads/jdk7-
downloads-1880260.html
www.rumahcoding.co.id 1
@idrumahcoding
developer.android.com/sdk/index.html
2 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 3
@idrumahcoding
4 www.rumahcoding.co.id
@idrumahcoding
Sejak pertama kali dirilis hingga saat ini, Google telah merilis
beberapa versi android mulai dari froyo, gingerbread, hingga yang
terbaru adalah marshmallow. Versi terbaru dibuat untuk melengkapi
dan menyempurnakan versi sebelumnya. Oleh karena itu, pada versi-
versi terbaru terdapat penambahan fitur atau mungkin saja
penggantian fitur dengan yang lebih canggih. Hal tersebut berdampak
pula dari sisi pembuatan aplikasi.
www.rumahcoding.co.id 5
@idrumahcoding
6 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 7
@idrumahcoding
8 www.rumahcoding.co.id
@idrumahcoding
4. Pilih versi android yang akan anda pakai kemudian klik tombol
Next maka akan muncul jendela verifikasi konfigurasi. Anda
dapat melakukan konfigurasi tambahan pada jendela tersebut.
Kemudian klik tombol Finish maka proses pembuatan
perangkat virtual telah selesai. Perangkat virtual yang baru
saja kita buat sudah bisa kita pergunakan untuk pengetesan
aplikasi.
www.rumahcoding.co.id 9
@idrumahcoding
10 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 11
@idrumahcoding
1.5. GenyMotion
www.genymotion.com/download
12 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 13
@idrumahcoding
14 www.rumahcoding.co.id
@idrumahcoding
Klik pada tautan Click here. Sampai disini, akun anda telah
berhasil diaktivasi. Sekarang anda bisa mengunjungi kembali halaman
untuk mengunduh GenyMotion pada tautan
www.genymotion.com/download. Kemudian pada halaman unduhan
tersebut, klik tombol Download for Mac OSX. Label dari tombol
tersebut tergantung sistem operasi yang anda gunakan.
www.rumahcoding.co.id 15
@idrumahcoding
16 www.rumahcoding.co.id
@idrumahcoding
Silahkan pilih jenis perangkat yang ingin anda buat lalu klik
tombol Next. Kemudian muncul jendela selanjutnya, lanjutkan saja
dengan meng-klik tombol Next.
www.rumahcoding.co.id 17
@idrumahcoding
18 www.rumahcoding.co.id
@idrumahcoding
1.6. AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
20 www.rumahcoding.co.id
@idrumahcoding
Jika kita perhatikan contoh kode xml di atas, kita dapat melihat
bahwa xml merupakan sebuah sarana untuk menyimpan informasi.
Setiap potongan informasi dimulai dengan tanda pembuka dan tanda
penutup. Tanda pembuka dan tanda penutup dapat kita definisikan
sendiri sesuai kebutuhan dengan syarat keduanya harus sama.
22 www.rumahcoding.co.id
@idrumahcoding
1. byte
2. short
3. int
4. long
5. float
6. double
7. boolean
8. char
Sebuah kelas dapat mewarisi kelas yang lain jika antara kedua
kelas tersebut memiliki kesamaan pada beberapa sifat dan perilaku.
Cara mendefinisikan kelas yang mewarisi kelas lain adalah dengan
menggunakan keyword extends. Kelas yang mewarisi kelas lain
disebut dengan kelas turunan. Perhatikan contoh kelas di bawah ini:
26 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 27
@idrumahcoding
28 www.rumahcoding.co.id
@idrumahcoding
Ada beberapa field yang harus kita isi, isilah dengan data berikut:
www.rumahcoding.co.id 29
@idrumahcoding
Pada jendela ini kita bisa mengatur target ponsel yang akan
menggunakan aplikasi yang akan kita buat. Untuk saat ini hanya
centang pilihan Phone and Tablet karena kita hanya akan membuat
aplikasi tersebut berjalan pada ponsel dan tablet. Pilihan lainnya untuk
sementara bisa diabaikan.
SDK kita bisa mengatur aplikasi hanya dapat berjalan jika ponsel
target terinstall android dengan versi minimal sesuai dengan versi yang
kita atur pada pilihan tersebut. Misal kita set API 15 (Ice Cream
Sandwich), maka aplikasi tersebut tidak akan bisa diinstall pada
ponsel dengan android di bawah Ice Cream Sandwich. Selain itu,
aplikasi tersebut juga tidak akan muncul di Google Play Store jika kita
mencari melalui ponsel dengan android di bawah Ice Cream Sandwich.
www.rumahcoding.co.id 31
@idrumahcoding
www.rumahcoding.co.id 33
@idrumahcoding
34 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 35
@idrumahcoding
36 www.rumahcoding.co.id
@idrumahcoding
4. Mengenal Activity
www.rumahcoding.co.id 37
@idrumahcoding
38 www.rumahcoding.co.id
@idrumahcoding
package com.fazrilabs.projekpertama;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)
findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action
bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
www.rumahcoding.co.id 39
@idrumahcoding
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action
bar will
// automatically handle clicks on the Home/Up
button, so long
// as you specify a parent activity in
AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
tools:context="com.fazrilabs.projekpertama.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
40 www.rumahcoding.co.id
@idrumahcoding
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fazrilabs.projekpertama.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
42 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 43
@idrumahcoding
44 www.rumahcoding.co.id
@idrumahcoding
5. Resource
setTitle("Judul Pertama");
setTitle(R.string.app_name);
<resources>
<string name="app_name">Judul Pertama</string>
</resources>
www.rumahcoding.co.id 45
@idrumahcoding
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fazrilabs.projekpertama.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hallo" />
</LinearLayout>
www.rumahcoding.co.id 47
@idrumahcoding
maka teks bisa kita pisahkan dari kode java dan diletakkan dalam
sebuah file xml. File ini diletakkan dalam folder /res/values.
<resources>
<string name="app_name">Projek Pertama</string>
<string name="action_settings">Settings</string>
<string name="action_add">Add</string>
<string name="action_delete">Delete</string>
<string name="action_edit">Edit</string>
</resources>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
www.rumahcoding.co.id 49
@idrumahcoding
String appName =
getResources().getString(R.string.app_name);
setTitle(R.string.app_name);
<string-array name="countries">
<item>Indonesia</item>
<item>Malaysia</item>
<item>Jepang</item>
<item>Singapura</item>
<item>Thailand</item>
</string-array>
50 www.rumahcoding.co.id
@idrumahcoding
String[] countries =
getResources().getStringArray(R.array.countries);
www.rumahcoding.co.id 51
@idrumahcoding
xmlns:app="http://schemas.android.com/apk/res-auto"
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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/adzkar"/>
</RelativeLayout>
52 www.rumahcoding.co.id
@idrumahcoding
1. drawable-ldpi (low)
2. drawable-mdpi (medium)
3. drawable-hdpi (high)
4. drawable-xhdpi (extra-high)
5. drawable-xxhdpi (extra-extra-high)
www.rumahcoding.co.id 53
@idrumahcoding
48 x 48 72 x 72 96 x 96 144 x 144
54 www.rumahcoding.co.id
@idrumahcoding
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.fazrilabs.projekpertama.MainActivity">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
www.rumahcoding.co.id 55
@idrumahcoding
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
56 www.rumahcoding.co.id
@idrumahcoding
if (id == R.id.action_settings) {
// lakukan sesuatu disini
return true;
}
return super.onOptionsItemSelected(item);
}
www.rumahcoding.co.id 57
@idrumahcoding
6. View
1. id
58 www.rumahcoding.co.id
@idrumahcoding
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
www.rumahcoding.co.id 59
@idrumahcoding
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:text="Button 1"/>
</RelativeLayout>
Nilai Keterangan
wrap_content Ukuran menyesuaikan dengan ukuran konten
view tersebut
match_parent Ukuran menyesuaikan dengan ukuran
kontainer
<TextView
android:layout_width="150dp"
android:layout_height="20dp"
android:text="@string/hello_world" />
60 www.rumahcoding.co.id
@idrumahcoding
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="5dp"
android:text="@string/hello_world" />
Atribut Keterangan
android:paddingTop Mengatur padding atas view
android:paddingRight Mengatur padding kanan view
android:paddingBottom Mengatur padding bawah view
android:paddingLeft Mengatur padding kiri view
android:layout_marginTop Mengatur margin atas view
android:layout_marginRight Mengatur margin kanan view
android:layout_marginBottom Mengatur margin bawah view
android:layout_marginLeft Mengatur margin kiri view
6.2. TextView
Atribut Keterangan
android:id Mendefinisikan ID unik untuk TextView
android:gravity Mengatur penyelarasan (alignment) teks
pada TextView
android:text Teks yang akan ditampilkan
android:textAllCaps Menampilkan teks dalam huruf besar
semua
android:textColor Mengatur warna teks dalam format #rgb
android:textSize Mengatur ukuran teks dalam dp
android:textStyle Mengatur gaya teks: normal, italic, or
bold
android:typeface Mengatur typeface: normal, sans, serif or
monospace
62 www.rumahcoding.co.id
@idrumahcoding
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world"
android:textColor="#3ebc3e"
android:textStyle="bold"
android:layout_centerInParent="true"
android:textSize="50dp"/>
</RelativeLayout>
www.rumahcoding.co.id 63
@idrumahcoding
6.3. EditText
64 www.rumahcoding.co.id
@idrumahcoding
Atribut Keterangan
<EditText
android:id="@+id/editText1"
android:layout_width="200dp"
android:layout_height="50dp"
android:hint="Silahkan input nama"
android:layout_centerInParent="true"/>
</RelativeLayout>
www.rumahcoding.co.id 65
@idrumahcoding
6.4. Button
66 www.rumahcoding.co.id
@idrumahcoding
Atribut Keterangan
android:id Mendefinisikan ID unik dari Button
android:text Teks yang akan ditampilkan pada Button
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_centerInParent="true"/>
</RelativeLayout>
www.rumahcoding.co.id 67
@idrumahcoding
68 www.rumahcoding.co.id
@idrumahcoding
6.5. ImageView
Atribut Keterangan
android:id Mendefinisikan ID unik dari ImageView
android:scaleType Mengatur jenis skala yang digunakan,
antara lain: center, center_crop,
center_inside, fit_center, fit_end, fit_start,
fit_xy, matrix.
android:src Image yang akan ditampilkan
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="@drawable/adzkar"
android:layout_centerInParent="true"/>
</RelativeLayout>
www.rumahcoding.co.id 69
@idrumahcoding
6.6. ImageButton
70 www.rumahcoding.co.id
@idrumahcoding
Atribut Keterangan
android:id Mendefinisikan ID unik dari ImageButton
android:src Image yang akan ditampilkan
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/adzkar"
android:layout_centerInParent="true"/>
</RelativeLayout>
www.rumahcoding.co.id 71
@idrumahcoding
72 www.rumahcoding.co.id
@idrumahcoding
6.7. Spinner
Atribut Keterangan
android:id Mendefinisikan ID unik dari ImageButton
android:entries Array teks yang akan dipilih oleh user
android:prompt Hint untuk user
<Spinner
android:id="@+id/spinner1"
android:layout_width="200dp"
www.rumahcoding.co.id 73
@idrumahcoding
android:layout_height="50dp"
android:entries="@array/countries"
android:prompt="@string/country_prompt"
android:layout_centerInParent="true">
</Spinner>
</RelativeLayout>
74 www.rumahcoding.co.id
@idrumahcoding
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View
view, int position, long id) {
String selectedCountry =
spinner.getSelectedItem().toString();
}
});
6.8. CheckBox
Atribut Keterangan
android:id Mendefinisikan ID unik dari
CheckBox
android:text Teks yang akan ditampilkan
android:drawableRight Gambar yang akan ditampilkan di
sebelah kanan teks
android:drawableBottom Gambar yang akan ditampilkan di
sebelah bawah teks
www.rumahcoding.co.id 75
@idrumahcoding
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Saya setuju"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
76 www.rumahcoding.co.id
@idrumahcoding
6.9. AutoCompleteTextView
Atribut Keterangan
android:id Mendefinisikan ID unik dari
AutoCompleteTextView
android:completionThreshold Jumlah minimal karakter yang
harus diketika sebelum drop
down menu muncul
<string-array name="countries">
<item>Afrika Selatan</item>
<item>Arab Saudi</item>
<item>Afganistan</item>
<item>Albania</item>
<item>Aljazair</item>
<item>Amerika Serikat</item>
<item>Indonesia</item>
<item>India</item>
<item>Malaysia</item>
<item>Jepang</item>
<item>Singapura</item>
<item>Thailand</item>
</string-array>
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
AutoCompleteTextView autoCompleteTextView =
(AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
String[] countries =
getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,
countries);
autoCompleteTextView.setAdapter(adapter);
78 www.rumahcoding.co.id
@idrumahcoding
6.10. ToggleButton
Atribut Keterangan
android:id Mendefinisikan ID unik dari
ToggleButton
android:textOff Teks yang akan ditampilkan ketika
kondisi Off
android:textOn Teks yang akan ditampilkan ketika
kondisi On
www.rumahcoding.co.id 79
@idrumahcoding
<ToggleButton
android:id=”@+id/toggleButton1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textOff=”Belum”
android:textOn=”Sudah”/>
<ToggleButton
android:id=”@+id/toggleButton2”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textOff=”Belum”
android:textOn=”Sudah”
android:checked=”true”/>
</LinearLayout>
80 www.rumahcoding.co.id
@idrumahcoding
6.11. RadioButton
www.rumahcoding.co.id 81
@idrumahcoding
Atribut Keterangan
android:id Mendefinisikan ID unik dari
RadioButton
android:text Teks yang akan ditampilkan
android:textColor Warna teks dengan format #rgb
android:checked Jika true, maka kondisi tercentang,
sebaliknya tidak tercentang
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:checked="true"
android:id="@+id/radioButton1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:checked="true"
android:id="@+id/radioButton2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/radioButton3" />
</LinearLayout>
82 www.rumahcoding.co.id
@idrumahcoding
6.12. RadioGroup
Atribut Keterangan
android:id Mendefinisikan ID unik dari
RadioGroup
android:checkedButton ID dari RadioButton yang tercentang
www.rumahcoding.co.id 83
@idrumahcoding
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:checked="true"
android:id="@+id/radioButton1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:checked="true"
android:id="@+id/radioButton2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/radioButton3" />
</RadioGroup>
</LinearLayout>
84 www.rumahcoding.co.id
@idrumahcoding
6.13. ProgressBar
www.rumahcoding.co.id 85
@idrumahcoding
Atribut Keterangan
android:id Mendefinisikan ID unik dari
ProgressBar
android:progress Nilai progress saat ini
android:max Nilai maksimum progress
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="50dp"
android:progress="50"
android:max="100"
android:layout_gravity="center_horizontal" />
</LinearLayout>
86 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 87
@idrumahcoding
7. Layout
Nilai Keterangan
top Sejajar atas
bottom Sejajar bawah
left Sejajar kiri
right Sejajar kanan
88 www.rumahcoding.co.id
@idrumahcoding
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e3e2ad"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
www.rumahcoding.co.id 89
@idrumahcoding
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#d6c6cd"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="layout_gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:layout_width="200dp"
90 www.rumahcoding.co.id
@idrumahcoding
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#aacaff"
android:text="center" />
</LinearLayout>
</LinearLayout>
www.rumahcoding.co.id 91
@idrumahcoding
2. layout_weight
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
www.rumahcoding.co.id 93
@idrumahcoding
7.3. LinearLayout
94 www.rumahcoding.co.id
@idrumahcoding
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
www.rumahcoding.co.id 95
@idrumahcoding
android:layout_weight="5"
android:text="Button 2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button 3"/>
</LinearLayout>
96 www.rumahcoding.co.id
@idrumahcoding
7.4. RelativeLayout
Atribut Keterangan
layout_below Memposisikan view di bawah view
dengan ID yang diacu
layout_above Memposisikan view di atas view
dengan ID yang diacu
layout_toLeftOf Memposisikan view di kiri view
dengan ID yang diacu
layout_toRightOf Memposisikan view di kanan view
dengan ID yang diacu
layout_alignParentTop Jika true, posisikan view sejajar
dengan batas atas parent
layout_alignParentRight Jika true, posisikan view sejajar
dengan batas kanan parent
layout_alignParentBottom Jika true, posisikan view sejajar
dengan batas bawah parent
layout_alignParentLeft Jika true, posisikan view sejajar
dengan batas kiri parent
layout_centerInParent Jika true, posisikan view di
tengah-tengah parent
layout_centerHorizontal Jika true, posisikan view di
tengah parent secara horizontal
layout_centerVertical Jika true, posisikan view di
tengah parent secara vertikal
www.rumahcoding.co.id 97
@idrumahcoding
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="Button 1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Button 2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Button 3"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Button 4"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 5"/>
</RelativeLayout>
98 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 99
@idrumahcoding
8. Dialog
8.1. Toast
100 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 101
@idrumahcoding
102 www.rumahcoding.co.id
@idrumahcoding
9. Menu
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/one"
android:title="One"/>
<item
android:id="@+id/two"
android:title="Two"/>
<item
android:id="@+id/three"
android:title="Three"/>
</menu>
www.rumahcoding.co.id 103
@idrumahcoding
Attribute Keterangan
android:id id yang untuk menu dimana akan
digunakan dalam java untuk memberikan
aksi ke menu ini.
android:title judul untuk menu
android:icon icon untuk men
app:showAsAction Show as action menunjukkan kapan menu
ini muncul di app bar
104 www.rumahcoding.co.id
@idrumahcoding
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/one"
android:title="One"/>
www.rumahcoding.co.id 105
@idrumahcoding
<item
android:id="@+id/two"
android:title="Two"/>
<item
android:id="@+id/three"
android:title="Three"/>
</menu>
106 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 107
@idrumahcoding
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.context_menu_list_activity,
menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
108 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 109
@idrumahcoding
10. SQLite
110 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 111
@idrumahcoding
www.rumahcoding.co.id 113
@idrumahcoding
10.1. SQLiteHelper
@Override
public void onCreate(SQLiteDatabase db) {
@Override
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion) {
114 www.rumahcoding.co.id
@idrumahcoding
@Override
public void onCreate(SQLiteDatabase db) {
String QUERY_CREATE_TABLE_ORANG = "CREATE TABLE orang (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"nama TEXT, " +
"no_hp TEXT, " +
"alamat TEXT)";
db.execSQL(QUERY_CREATE_TABLE_ORANG);
}
1. Kolom _id dengan tipe datanya adalah INTEGER karena tipe data
dari class property id pada class Orang juda merupakan tipe
data angka. Perhatikan juga, setelah penulisan tipe data
INTEGER terdapat keyword PRIMARY KEY dan AUTOINCREMENT.
Keyword PRIMARY KEY akan membuat kolom id pada table
orang hanya boleh diisikan nilai yang unik. Sedangkan keyword
AUTOINCREMENT akan membuat kolom id selalu
www.rumahcoding.co.id 115
@idrumahcoding
Query diatas menjelaskan bahwa ambil semua data dari kolom nama
dan alamat yang ada pada table person. Untuk mengambil data dengan
batasan tertentu maka setelah penulisan nama table pada query,
tambahkan klausa WHERE, contoh :
Arti dari query diatas adalah ambil semua data dari kolom
nama dan alamat yang ada pada table person dimana namanya
adalah rumah_coding.
116 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 117
@idrumahcoding
118 www.rumahcoding.co.id
@idrumahcoding
Merupakan view adapter yang hanya terdiri dari satu view, view
yang dimaksud adalah berupa TextView yang memiliki id dengan nama
text1. Adapter view ini sudah secara default ada didalam android SDK.
Untuk menggunakan adapter view ini maka diperlukan untuk
mengubah adapter dari ListView menjadi
Contoh :
Sama halnya denga simple list item 1, adapter view ini juga
sudah ada didalam SDK android dan perbedaannya hanya terletak di
jumlah TextView. Pada view adapter ini terdapat 2 buah TextView dan
masing-masing id nya adalah text1 dan text2. Secara tampilan,
TextView dengan id text1 merupakan header sefangkan TextView
dengan id text2 merupakan sub judul, Contoh :
www.rumahcoding.co.id 119
@idrumahcoding
120 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 121
@idrumahcoding
Pada bagian ini, hanya beri tanda ceklist pada Phone and Tablet
kemudian pilih targetnya API-nya versi 15. Klik Next untuk langkah
selanjutnya sehingga muncul tampilan berikut:
122 www.rumahcoding.co.id
@idrumahcoding
Pada langkah ini pilih Add No Activity karena nanty kita akan
membuatnya nanti. Klik finish sehingga muncul tampilan berikut:
www.rumahcoding.co.id 123
@idrumahcoding
2. Membuat FormActivity
Pilih New > Activity > Empty Activity sehingga muncul dialog
seperti gambar dibawah ini :
124 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 125
@idrumahcoding
<LinearLayout
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:layout_margin="10dp"
android:orientation="vertical"
tools:context=".FormActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<EditText
android:id="@+id/nama_depan_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
www.rumahcoding.co.id 127
@idrumahcoding
android:hint="Nama Depan"
android:inputType="textPersonName" />
<EditText
android:id="@+id/nama_belakang_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPersonName"
android:hint="Nama Belakang" />
</LinearLayout>
<EditText
android:id="@+id/no_hp_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="No.Hp"
android:inputType="number" />
<EditText
android:id="@+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email"/>
<EditText
android:id="@+id/tgl_lahir_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:clickable="true"
android:focusable="false"
android:hint="Tanggal Lahir (dd/MM/yyyy)"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender" />
128 www.rumahcoding.co.id
@idrumahcoding
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/pria_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Pria" />
<RadioButton
android:id="@+id/wanita_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wanita" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jenjang" />
<Spinner
android:id="@+id/jenjang_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobi" />
<CheckBox
android:id="@+id/baca_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
www.rumahcoding.co.id 129
@idrumahcoding
android:text="Membaca" />
<CheckBox
android:id="@+id/chk_nulis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menulis" />
<CheckBox
android:id="@+id/chk_gambar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menggambar" />
<EditText
android:id="@+id/input_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:hint="Alamat"/>
<Button
android:id="@+id/btn_save_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Simpan"
android:background="@color/colorPrimary"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
130 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 131
@idrumahcoding
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".FormActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<EditText
android:id="@+id/nama_depan_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Nama Depan"
android:inputType="textPersonName" />
<EditText
android:id="@+id/nama_belakang_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPersonName"
android:hint="Nama Belakang" />
</LinearLayout>
<EditText
android:id="@+id/no_hp_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="No.Hp"
android:inputType="number" />
132 www.rumahcoding.co.id
@idrumahcoding
<EditText
android:id="@+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email"/>
<EditText
android:id="@+id/tgl_lahir_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:clickable="true"
android:focusable="false"
android:hint="Tanggal Lahir (dd/MM/yyyy)"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/pria_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Pria" />
<RadioButton
android:id="@+id/wanita_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wanita" />
</RadioGroup>
www.rumahcoding.co.id 133
@idrumahcoding
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jenjang" />
<Spinner
android:id="@+id/jenjang_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobi" />
<CheckBox
android:id="@+id/baca_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Membaca" />
<CheckBox
android:id="@+id/chk_nulis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menulis" />
<CheckBox
android:id="@+id/chk_gambar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menggambar" />
<EditText
android:id="@+id/input_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:hint="Alamat"/>
134 www.rumahcoding.co.id
@idrumahcoding
<Button
android:id="@+id/btn_save_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Simpan"
android:background="@color/colorPrimary"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
https://drive.google.com/file/d/1fpxBGR-
DgjoJPVbjxD1dHFnLueCDFebU/view?usp=sharing
<Button
android:id="@+id/btn_save_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Simpan"
android:drawableStart="@drawable/ic_save"
android:background="@color/colorPrimary"
www.rumahcoding.co.id 135
@idrumahcoding
android:textColor="@color/white"
android:textStyle="bold" />
Klik kanan pada folder res kemudian pilih New > Android
resource file. Lihat gambar dibawah ini untuk lebih jelasnya :
136 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 137
@idrumahcoding
<item
android:id="@+id/save_menu"
android:title="Simpan"
android:icon="@drawable/ic_save"
app:showAsAction="always"/>
</menu>
138 www.rumahcoding.co.id
@idrumahcoding
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
namaDepanField =
www.rumahcoding.co.id 139
@idrumahcoding
findViewById(R.id.nama_depan_field);
namaBelakangField =
findViewById(R.id.nama_belakang_field);
noHpField = findViewById(R.id.no_hp_field);
emailField = findViewById(R.id.email_field);
birtDateField = findViewById(R.id.tgl_lahir_field);
rbPria = findViewById(R.id.pria_rb);
rbWanita = findViewById(R.id.wanita_rb);
spJenjang = findViewById(R.id.jenjang_spinner);
chkBaca = findViewById(R.id.baca_checkbox);
chkNulis = findViewById(R.id.chk_nulis);
chkGambar = findViewById(R.id.chk_gambar);
alamatField = findViewById(R.id.input_alamat);
Button btnSave =
findViewById(R.id.btn_save_student);
}
}
140 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 141
@idrumahcoding
142 www.rumahcoding.co.id
@idrumahcoding
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
}
});
emailField.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialogPilihTanggal();
}
});
@Override
namaDepanField = findViewById(R.id.nama_depan_field);
namaBelakangField =
findViewById(R.id.nama_belakang_field);
noHpField = findViewById(R.id.no_hp_field);
emailField = findViewById(R.id.email_field);
birtDateField = findViewById(R.id.tgl_lahir_field);
rbPria = findViewById(R.id.pria_rb);
rbWanita = findViewById(R.id.wanita_rb);
spJenjang = findViewById(R.id.jenjang_spinner);
www.rumahcoding.co.id 143
@idrumahcoding
chkBaca = findViewById(R.id.baca_checkbox);
chkNulis = findViewById(R.id.chk_nulis);
chkGambar = findViewById(R.id.chk_gambar);
alamatField = findViewById(R.id.input_alamat);
emailField.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
showDialogPilihTanggal();
}
});
}
144 www.rumahcoding.co.id
@idrumahcoding
return true;
www.rumahcoding.co.id 145
@idrumahcoding
if (!email.contains("@")) {
emailField.setError("Email tidak valid");
return false;
}
return true;
alamatField.requestFocus();
return false;
}
return true;
return true;
www.rumahcoding.co.id 147
@idrumahcoding
Perhatikan kode:
Dalam hal ini kita akan membuat sebuah koleksi data dari sejumlah
hobi yang dipilih oleh pengguna aplikasi dan setiap hobi yang dipilih
merupakan nilai yang tipe datanya adalah String. Setelah data koleksi
tersebut sudah siap, kita akan membuat String baru berdasarkan dari
koleksi tersebut dan akan setiap hobinya akan dipisahkan dengan
tanda koma(,). Dan untuk melakukannya kita menggunakan kode ini :
, yang nantinya akan mengembalikan nilai berupa String.
if(chkBaca.isChecked()) hobies.add("Membaca"),
www.rumahcoding.co.id 149
@idrumahcoding
//kode lainnya..
@Override
birtDateField.setText(dateInString);
150 www.rumahcoding.co.id
@idrumahcoding
datePickerDialog.show();
Perhatikan kode:
kode ini berguna untuk mengetahui tanggal hari ini. Selain itu
perhatikan juda kode inisialisasi DatePickerDialog. Diproses ini kita
akan membuat dialog pilih tanggal dan mengatur waktu default yang
terpilih adalah tanggal hari ini.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.form_activity_menu,
menu);
www.rumahcoding.co.id 151
@idrumahcoding
return super.onCreateOptionsMenu(menu);
}
@Override
return super.onOptionsItemSelected(item);
import java.io.Serializable;
import java.io.Serializable;
www.rumahcoding.co.id 153
@idrumahcoding
154 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 155
@idrumahcoding
"sekolah_ku.db";
private static final int DATABASE_VERSION = 1;
@Override
public void onCreate(SQLiteDatabase db) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
@Override
156 www.rumahcoding.co.id
@idrumahcoding
db.execSQL(QUERY_CREATE_TABLE_SISWA);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
String QUERY_DROP_TABLE_SISWA = "DROP TABLE IF EXISTS
siswa";
db.execSQL(QUERY_DROP_TABLE_SISWA);
}
www.rumahcoding.co.id 157
@idrumahcoding
www.rumahcoding.co.id 159
@idrumahcoding
contentValues.put("nama_depan", siswa.getNamaDepan());
contentValues.put("nama_belakang",
siswa.getNamaBelakang());
contentValues.put("no_hp", siswa.getNoHp());
contentValues.put("gender", siswa.getGender());
contentValues.put("jenjang", siswa.getJenjang());
contentValues.put("hobi", siswa.getHobi());
contentValues.put("alamat", siswa.getAlamat());
contentValues.put("email", siswa.getEmail());
contentValues.put("birth_day", siswa.getBirthDay());
return contentValues;
}
160 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 161
@idrumahcoding
close();
}
open();
Cursor cursor = database.rawQuery("SELECT * FROM siswa",
null);
while (cursor.moveToNext()) {
Siswa siswa = fetchRow(cursor);
siswaList.add(siswa);
}
cursor.close();
close();
return siswaList;
}
162 www.rumahcoding.co.id
@idrumahcoding
cursor.close();
close();
return siswaList;
}
www.rumahcoding.co.id 163
@idrumahcoding
Fitur lain yang nantinya juga ada adalah kita akan membuat
context menu, menampilkan detail siswa dan juga action menu. Context
menu ini sendiri nantinya akan digunakan untuk melakukan operasi
lebih lanjut dalam hal memanipulasi data siswa, baik itu menghapus
data siswa terpilih dan mengupdate data siswa terpilih.
• Klik kanan pada folder res > menu dan pilih new > Menu resource
file, seperti yang nampak pada gambar berikut ini :
www.rumahcoding.co.id 165
@idrumahcoding
• Setelah itu buka file menu tersebut dan update kodenya menjadi
seperti dibawah ini :
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/context_update"
android:title="Update" />
<item
android:id="@+id/context_delete"
android:title="Delete" />
</menu>
166 www.rumahcoding.co.id
@idrumahcoding
Buatlah layout baru dalam folder res > layout dan beri
namanya siswa_item. Layout ini nantinya akan menjadi tampilan
default dari setiap item yang muncul di layout ListView Sekarang
buka file tersebut dan ubah kodenya menjadi seperti dibawah ini :
android:padding="10dp">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_person"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_nama"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Nama"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/txt_jenjang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jenjang"
android:textAlignment="textEnd"
android:gravity="end" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
168 www.rumahcoding.co.id
@idrumahcoding
<TextView
android:id="@+id/txt_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gender"/>
<TextView
android:id="@+id/txt_no_hp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="No. Hp"
android:textAlignment="textEnd"
android:gravity="end" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
@NonNull
@Override
public View getView(int position, @Nullable View
convertView, @NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(getContext())
.inflate(R.layout.siswa_item, null);
www.rumahcoding.co.id 169
@idrumahcoding
return view;
}
}
<android.support.v7.widget.SearchView
android:id="@+id/search_view"
170 www.rumahcoding.co.id
@idrumahcoding
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:iconifiedByDefault="false"
app:queryHint="Cari siswa">
</android.support.v7.widget.SearchView>
<ListView
android:id="@+id/list_view_students"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_siswa);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_siswa);
}
@Override
protected void onResume() {
super.onResume();
}
}
@Override
protected void onResume() {
super.onResume();
172 www.rumahcoding.co.id
@idrumahcoding
adapter.addAll(siswaList);
adapter.notifyDataSetChanged();
}
}
};
.setTitle("Hapus Siswa")
.setMessage("Yakin Hapus Siswa Dengan Nama "+nama)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Ya", confirm)
.setNegativeButton("Tidak", cancel)
.show();
Perhatikan kode:
176 www.rumahcoding.co.id
@idrumahcoding
@Override
protected void onResume() {
super.onResume();
loadAllSiswa();
ListView listView = findViewById(R.id.list_view_siswa);
listView.setAdapter(adapter);
@Override
public boolean onQueryTextChange(String newText) {
loadSiswaBy(newText);
return false;
}
});
}
www.rumahcoding.co.id 177
@idrumahcoding
@Override
public boolean onQueryTextChange(String newText) {
loadSiswaBy(newText);
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_siswa_action_menu,
menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.add_student_menu) {
addNewSiswa();
}
178 www.rumahcoding.co.id
@idrumahcoding
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.list_activity_context,
menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
Setelah itu, barulah kita dapat memberikan aksi pada setiap item
menu yang terdapat dalam list_activity_context.xml. Untuk
memberikan aksinya maka kita perlu untuk meng-override method
onContextItemSelected. Ketikkan kode berikut dibawah method
onCreateContextMenu :
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuInfo =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = menuInfo.position;
Siswa siswa = adapter.getItem(position);
www.rumahcoding.co.id 179
@idrumahcoding
update(siswa);
} else if (idItem == R.id.context_delete) {
delete(siswa);
}
return super.onContextItemSelected(item);
}
registerForContextMenu(listView)
180 www.rumahcoding.co.id
@idrumahcoding
www.rumahcoding.co.id 181
@idrumahcoding
Kode diatas yang akan mengatur alur aksi dari menyimpan data
ke database. Jika aksinya adalah update maka akan memanggil
method updateDataSiswa dan jika aksi adalah tambah data siswa bari
maka method saveDataSiswaBaru yang akan dieksekusi.
www.rumahcoding.co.id 183
@idrumahcoding
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/ic_person" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_label" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama" />
<TextView
android:id="@+id/txt_nama"
184 www.rumahcoding.co.id
@idrumahcoding
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<include layout="@layout/separator"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_smartphone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No. Hp" />
<TextView
android:id="@+id/txt_no_hp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No. Hp"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
www.rumahcoding.co.id 185
@idrumahcoding
<include layout="@layout/separator"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_favorite" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender" />
<TextView
android:id="@+id/txt_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<include layout="@layout/separator"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<ImageView
186 www.rumahcoding.co.id
@idrumahcoding
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_flight_takeoff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jenjang" />
<TextView
android:id="@+id/txt_jenjang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jenjang"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_fitness_center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
www.rumahcoding.co.id 187
@idrumahcoding
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobi" />
<TextView
android:id="@+id/txt_hobi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobi"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<include layout="@layout/separator"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_location_on" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alamat" />
<TextView
android:id="@+id/txt_alamat"
188 www.rumahcoding.co.id
@idrumahcoding
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alamat"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
Perhatikan kode;
<include layout="@layout/separator"/>,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.detail_activity_action_menu
, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.home_menu) {
finish();
}
return super.onOptionsItemSelected(item);
}
intent.putExtra("siswa", siswa);
startActivity(intent);
}
www.rumahcoding.co.id 191
@idrumahcoding
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View
view, int position, long id) {
viewDetailSiswa(position);
}
});
@Override
protected void onResume() {
super.onResume();
loadAllSiswa();
ListView listView = findViewById(R.id.list_view_siswa);
listView.setAdapter(adapter);
registerForContextMenu(listView);
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View
view,
int position, long id) {
viewDetailSiswa(position);
}
});
192 www.rumahcoding.co.id
@idrumahcoding
SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
loadSiswaBy(newText);
return false;
}
});
}
Buka file configurasi layout dari activity ini yang memiliki nama
activity_login kemudian update kodenya menjadi seperti dibawah ini :
<LinearLayout
www.rumahcoding.co.id 193
@idrumahcoding
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/header"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sekolah Ku"
android:textColor="@color/white"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:hint="Username"
android:drawableStart="@drawable/ic_action_person"
android:drawableLeft="@drawable/ic_action_person"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
194 www.rumahcoding.co.id
@idrumahcoding
android:hint="Password"
android:inputType="textPassword"
android:drawableStart="@drawable/ic_action_lock"
android:drawableLeft="@drawable/ic_action_lock"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="@color/colorPrimary"
android:text="Login"
android:textStyle="bold"
android:textColor="@color/white"/>
</LinearLayout>
}
private boolean isGranted(String username, String password)
{
return true;
}
return true;
}
if (password.isEmpty()) {
passwordField.setError("Password Masih Kosong");
return false;
196 www.rumahcoding.co.id
@idrumahcoding
}
return true;
}
if (username.isEmpty()) {
usernameField.setError("Username Masih Kosong");
return false;
}
return true;
}
www.rumahcoding.co.id 197
@idrumahcoding
if (granted) {
Intent intent = new Intent(this,
ListSiswaActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(this, "Username / Password Salah",
Toast.LENGTH_LONG)
.show();
}
}
Buka file configurasi layout dari activity ini yang memiliki nama
activity_login.xml kemudian update kodenya menjadi seperti
dibawah ini :
198 www.rumahcoding.co.id
@idrumahcoding
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/ic_home"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#fff"
android:text="Sekolah Ku"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
www.rumahcoding.co.id 199
@idrumahcoding
</RelativeLayout>
Perhatikan kode:
www.rumahcoding.co.id 201
@idrumahcoding
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreenActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".FormActivity" />
<activity android:name=".ListSiswaActivity" />
<activity android:name=".DetailActivity" />
</application>
</manifest>
<activity android:name=".SplashScreenActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
202 www.rumahcoding.co.id
@idrumahcoding
</intent-filter>
</activity>
www.rumahcoding.co.id 203