Cara Membuat Aplikasi Sewa Mobil Menggunakan Android Studio dengan Database SQLite
maswijaba.my.id - Assalamualaikum wr.wb. Puji syukur kita bisa dipertemukan lagi di kesempatan kali ini, semoga semuanya sehat-sehat ya. Pada kesempatan kali ini saya akan membahas bagaimana Cara Membuat Aplikasi Sewa Mobil Menggunakan Android Studio dengan Database SQLite.
Tutorial sederhana ini dirasa bisa membantu kalian untuk membuat
aplikasi Android, walaupun tidak sempurna dan bisa dikatakan sederhana
mudah-mudahan bermanfaat untuk kalian yang ingin belajar tentang
Android.
Aplikasi ini di desain dengan sedemikian rupa dan pastinya untuk pemula sangat mudah untuk mempelajarinya. Aplikasi rental mobil ini adalah aplikasi offline. Bisa juga dibuat online.
Untuk mengambil datanya kita menggunakan database SQLite bukan menggunakan API.
Aplikasi ini sangat disarankan untuk membuat tugas Android sebagai referensi kalian untuk tugas-tugas mobile, buat tugas akhir atau skripsi juga tidak apa-apa asalkan kalian memperbaharuinya (tambahkan yang belum ada).
Langsung saja kita masuk ke cara pengaplikasiannya dengan mengikuti step-step di bawah ini.
1. Buat Project Baru di Android Studio
Klik File - Project Baru. Ketika diminta untuk memilih Default Activity, kita memilih Empty Activity dan klik Next. (Untuk minSDK, saya set API 21)
2. Buat activity_main.xml dan MainActivity.java
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<FrameLayout
android:id="@+id/flMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_home_wave"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="@+id/rlAppName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="15dp"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:layout_marginEnd="-20dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:src="@drawable/ic_date" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="25dp"
android:layout_toEndOf="@id/imgDate"
android:text="Minggu, 20 Oktober 2020"
android:textColor="@color/colorPrimary"
android:textSize="12sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
<TextView
android:id="@+id/tvMainSalam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/flMain"
android:elevation="5dp"
android:gravity="center"
android:text="Selamat Pagi"
android:textColor="@android:color/white"
android:textSize="14sp" />
<LinearLayout
android:id="@+id/llCurved"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/flMain"
android:background="@drawable/bg_home_curve"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/llCurved"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:layout_marginTop="56dp"
android:src="@drawable/ic_mobil_home" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginBottom="28dp"
android:gravity="center"
android:text="Aplikasi Rental Mobil"
android:textColor="@color/colorPrimary"
android:textSize="22sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_info_mobil"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_marginBottom="20dp"
android:background="@drawable/button_background"
android:text="INFORMASI MOBIL"
android:textColor="@android:color/white"
android:textSize="18sp" />
<Button
android:id="@+id/btn_sewa"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@drawable/button_background"
android:text="SEWA MOBIL"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.wijaba.rentalmobil.activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.wijaba.rentalmobil.R;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
TextView tvToday, tvMainSalam;
String hariIni;
Animation animTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button informasi = findViewById(R.id.btn_info_mobil);
Button sewa = findViewById(R.id.btn_sewa);
informasi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, DaftarMobilActivity.class);
startActivity(i);
}
});
sewa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent p = new Intent(MainActivity.this, PenyewaActivity.class);
startActivity(p);
}
});
tvToday = findViewById(R.id.tvDate);
tvMainSalam = findViewById(R.id.tvMainSalam);
animTv = AnimationUtils.loadAnimation(this, R.anim.anim_tv);
tvMainSalam.startAnimation(animTv);
Date dateNow = Calendar.getInstance().getTime();
hariIni = (String) DateFormat.format("EEEE", dateNow);
if (hariIni.equalsIgnoreCase("sunday")) {
hariIni = "Minggu";
} else if (hariIni.equalsIgnoreCase("monday")) {
hariIni = "Senin";
} else if (hariIni.equalsIgnoreCase("tuesday")) {
hariIni = "Selasa";
} else if (hariIni.equalsIgnoreCase("wednesday")) {
hariIni = "Rabu";
} else if (hariIni.equalsIgnoreCase("thursday")) {
hariIni = "Kamis";
} else if (hariIni.equalsIgnoreCase("friday")) {
hariIni = "Jumat";
} else if (hariIni.equalsIgnoreCase("saturday")) {
hariIni = "Sabtu";
}
getToday();
setSalam();
}
private void setSalam() {
Calendar calendar = Calendar.getInstance();
int timeOfDay = calendar.get(Calendar.HOUR_OF_DAY);
if (timeOfDay >= 0 && timeOfDay < 12) {
tvMainSalam.setText("Selamat Pagi" + " " + "Wijaba");
} else if (timeOfDay >= 12 && timeOfDay < 15) {
tvMainSalam.setText("Selamat Siang" + " " + "Wijaba");
} else if (timeOfDay >= 15 && timeOfDay < 18) {
tvMainSalam.setText("Selamat Sore" + " " + "Wijaba");
} else if (timeOfDay >= 18 && timeOfDay < 24) {
tvMainSalam.setText("Selamat Malam" + " " + "Wijaba");
}
}
private void getToday() {
Date date = Calendar.getInstance().getTime();
String tanggal = (String) DateFormat.format("d", date);
String monthNumber = (String) DateFormat.format("M", date);
String year = (String) DateFormat.format("yyyy", date);
int month = Integer.parseInt(monthNumber);
String bulan = null;
if (month == 1) {
bulan = "Januari";
} else if (month == 2) {
bulan = "Februari";
} else if (month == 3) {
bulan = "Maret";
} else if (month == 4) {
bulan = "April";
} else if (month == 5) {
bulan = "Mei";
} else if (month == 6) {
bulan = "Juni";
} else if (month == 7) {
bulan = "Juli";
} else if (month == 8) {
bulan = "Agustus";
} else if (month == 9) {
bulan = "September";
} else if (month == 10) {
bulan = "Oktober";
} else if (month == 11) {
bulan = "November";
} else if (month == 12) {
bulan = "Desember";
}
String formatFix = hariIni + ", " + tanggal + " " + bulan + " " + year;
tvToday.setText(formatFix);
}
}
3. Selanjutnya buat class activity_mobil.xml dan DaftarMobilActivity.java
Untuk menampilkan daftar mobil yang tersedia.
activity_mobil.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbInfoMbl"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tbInfoMbl"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical"
android:paddingBottom="16dp">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
</RelativeLayout>
DaftarMobilActivity.java
package com.wijaba.rentalmobil.activity;
import android.R.layout;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.wijaba.rentalmobil.R;
import com.wijaba.rentalmobil.helper.DataHelper;
public class DaftarMobilActivity extends AppCompatActivity {
String[] daftar;
ListView ListView1;
Menu menu;
protected Cursor cursor;
DataHelper dbcenter;
public static DaftarMobilActivity m;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mobil);
m = this;
dbcenter = new DataHelper(this);
RefreshList();
setupToolbar();
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.tbInfoMbl);
toolbar.setTitle("Informasi Daftar Mobil");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void RefreshList() {
SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM mobil", null);
daftar = new String[cursor.getCount()];
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
daftar[i] = cursor.getString(0);
}
ListView1 = findViewById(R.id.listView1);
ListView1.setAdapter(new ArrayAdapter(this, layout.simple_list_item_1, daftar));
ListView1.setSelected(true);
ListView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
final String selection = daftar[arg2];
Intent i = new Intent(DaftarMobilActivity.this, DetailMobilActivity.class);
i.putExtra("merk", selection);
startActivity(i);
}
});
((ArrayAdapter) ListView1.getAdapter()).notifyDataSetInvalidated();
}
}
4. Selanjutnya buat class activity_detail_mobil.xml dan DetailMobilActivity.java
Untuk menampilkan harga sewa per mobilnya.
activity_detail_mobil.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbDetailMbl"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tbDetailMbl"
android:clipToPadding="false"
android:fillViewport="false"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical"
android:paddingBottom="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_item"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<TextView
android:id="@+id/JMobil"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Kijang Innova"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="12dp"
android:background="@android:color/white" />
<ImageView
android:id="@+id/ivMobil"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/innova" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:background="@android:color/white" />
<LinearLayout
android:id="@+id/GHarga"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6.56"
android:text="Harga Mobil :"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:id="@+id/JHarga"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:background="@drawable/background_textview"
android:gravity="center"
android:text="Rp. 100.000"
android:textColor="@color/colorPrimary"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
DetailMobilActivity.java
package com.wijaba.rentalmobil.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.wijaba.rentalmobil.R;
import com.wijaba.rentalmobil.helper.DataHelper;
public class DetailMobilActivity extends AppCompatActivity {
protected Cursor cursor;
String sMerk, sHarga, sGambar;
DataHelper dbHelper;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_mobil);
Bundle terima = getIntent().getExtras();
dbHelper = new DataHelper(this);
Intent intent = getIntent();
String merk = terima.getString("merk");
SQLiteDatabase db = dbHelper.getReadableDatabase();
cursor = db.rawQuery("select * from mobil where merk = '" + merk + "'", null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
sMerk = cursor.getString(0);
sHarga = cursor.getString(1);
}
if (sMerk.equals("Avanza")) {
sGambar = "avanza";
} else if (sMerk.equals("Xenia")) {
sGambar = "xenia";
} else if (sMerk.equals("Ertiga")) {
sGambar = "ertiga";
} else if (sMerk.equals("APV")) {
sGambar = "apv";
} else if (sMerk.equals("Innova")) {
sGambar = "innova";
} else if (sMerk.equals("Xpander")) {
sGambar = "xpander";
} else if (sMerk.equals("Pregio")) {
sGambar = "pregio";
} else if (sMerk.equals("Elf")) {
sGambar = "elf";
} else if (sMerk.equals("Alphard")) {
sGambar = "alphard";
}
ImageView ivGambar = findViewById(R.id.ivMobil);
TextView tvMerk = findViewById(R.id.JMobil);
TextView tvHarga = findViewById(R.id.JHarga);
tvMerk.setText(sMerk);
ivGambar.setImageResource(getResources().getIdentifier(sGambar, "drawable", getPackageName()));
tvHarga.setText("Rp. " + sHarga);
setupToolbar();
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.tbDetailMbl);
toolbar.setTitle("Detail Mobil");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
5. Selanjutnya buat class activity_sewa.xml dan SewaMobilActivity.java
Diperuntukan menu sewa yang berisi mobil yang akan disewa dan data penyewa mobil.
activity_sewa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbSewaMobl"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tbSewaMobl"
android:clipToPadding="false"
android:fillViewport="false"
android:paddingBottom="20dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:strokeColor="@color/colorPrimary"
app:strokeWidth="2dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/idSewa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Identitas Penyewa"
android:textColor="@color/colorPrimary"
android:textSize="19sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/idSewa"
android:layout_marginTop="20dp"
android:text="@string/nama_penyewa"
android:textColor="@android:color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/eTNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtName"
android:hint="Masukkan Nama (Max 15 karakter)"
android:inputType="text"
android:maxLength="20"
android:textSize="16sp" />
<TextView
android:id="@+id/txtAlamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eTNama"
android:layout_marginTop="10dp"
android:text="@string/alamat"
android:textColor="@android:color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/eTAlamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtAlamat"
android:hint="Masukkan Alamat Anda"
android:inputType="text"
android:maxLength="20"
android:textSize="16sp" />
<TextView
android:id="@+id/txtTlp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eTAlamat"
android:layout_marginTop="10dp"
android:text="@string/no_telp_hp"
android:textColor="@android:color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/eTHP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTlp"
android:hint="Masukkan No. Telp/HP"
android:inputType="number"
android:maxLength="20"
android:textSize="16sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:strokeColor="@color/colorPrimary"
app:strokeWidth="2dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/idDtMbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Data Mobil"
android:textColor="@color/colorPrimary"
android:textSize="19sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtMerk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/idDtMbl"
android:layout_marginTop="20dp"
android:text="@string/merk_mobil"
android:textColor="@android:color/black"
android:textSize="17sp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtMerk" />
<TextView
android:id="@+id/txtPromo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_marginTop="20dp"
android:text="Promo"
android:textColor="@android:color/black"
android:textSize="17sp" />
<RadioGroup
android:id="@+id/promoGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPromo"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbWeekDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:checked="true"
android:text="Weekday (10%)"
android:textColor="@color/colorPrimary" />
<RadioButton
android:id="@+id/rbWeekEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Weekend (25%)"
android:textColor="@color/colorPrimary" />
</RadioGroup>
<TextView
android:id="@+id/txtLmSewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/promoGroup"
android:layout_marginTop="20dp"
android:text="@string/lama_sewa"
android:textColor="@android:color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/eTLamaSewa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLmSewa"
android:hint="Masukkan Lamanya Hari Penyewaan"
android:inputType="number"
android:maxLength="10"
android:textSize="16sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.button.MaterialButton
android:id="@+id/selesaiHitung"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="8dp"
android:text="Selesai"
app:cornerRadius="50dp"
android:textColor="@android:color/white"
android:textSize="16sp"
android:theme="@style/Theme.MaterialComponents.Light"
app:backgroundTint="@color/colorPrimary" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
SewaMobilActivity.java
package com.wijaba.rentalmobil.activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.wijaba.rentalmobil.R;
import com.wijaba.rentalmobil.helper.DataHelper;
import java.util.List;
public class SewaMobilActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
EditText nama, alamat, no_hp, lama;
RadioGroup promo;
RadioButton weekday, weekend;
Button selesai;
String sNama, sAlamat, sNo, sMerk, sLama;
double dPromo;
int iLama, iPromo, iHarga;
double dTotal;
private Spinner spinner;
DataHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sewa);
dbHelper = new DataHelper(this);
spinner = findViewById(R.id.spinner);
selesai = findViewById(R.id.selesaiHitung);
nama = findViewById(R.id.eTNama);
alamat = findViewById(R.id.eTAlamat);
no_hp = findViewById(R.id.eTHP);
promo = findViewById(R.id.promoGroup);
weekday = findViewById(R.id.rbWeekDay);
weekend = findViewById(R.id.rbWeekEnd);
lama = findViewById(R.id.eTLamaSewa);
spinner.setOnItemSelectedListener(this);
loadSpinnerData();
selesai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sNama = nama.getText().toString();
sAlamat = alamat.getText().toString();
sNo = no_hp.getText().toString();
sLama = lama.getText().toString();
if (sNama.isEmpty() || sAlamat.isEmpty() || sNo.isEmpty() || sLama.isEmpty()) {
Toast.makeText(SewaMobilActivity.this, "(*) tidak boleh kosong", Toast.LENGTH_SHORT).show();
return;
}
if (weekday.isChecked()) {
dPromo = 0.1;
} else if (weekend.isChecked()) {
dPromo = 0.25;
}
if (sMerk.equals("Avanza")) {
iHarga = 400000;
} else if (sMerk.equals("Xenia")) {
iHarga = 400000;
} else if (sMerk.equals("Ertiga")) {
iHarga = 400000;
} else if (sMerk.equals("APV")) {
iHarga = 450000;
} else if (sMerk.equals("Innova")) {
iHarga = 500000;
} else if (sMerk.equals("Xpander")) {
iHarga = 550000;
} else if (sMerk.equals("Pregio")) {
iHarga = 550000;
} else if (sMerk.equals("Elf")) {
iHarga = 700000;
} else if (sMerk.equals("Alphard")) {
iHarga = 1500000;
}
iLama = Integer.parseInt(sLama);
iPromo = (int) (dPromo * 100);
dTotal = (iHarga * iLama) - (iHarga * iLama * dPromo);
SQLiteDatabase dbH = dbHelper.getWritableDatabase();
dbH.execSQL("INSERT INTO penyewa (nama, alamat, no_hp) VALUES ('" +
sNama + "','" +
sAlamat + "','" +
sNo + "');");
dbH.execSQL("INSERT INTO sewa (merk, nama, promo, lama, total) VALUES ('" +
sMerk + "','" +
sNama + "','" +
iPromo + "','" +
iLama + "','" +
dTotal + "');");
PenyewaActivity.m.RefreshList();
finish();
}
});
setupToolbar();
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.tbSewaMobl);
toolbar.setTitle("Sewa Mobil");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadSpinnerData() {
DataHelper db = new DataHelper(getApplicationContext());
List categories = db.getAllCategories();
ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
sMerk = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView parent) {
}
}
6. Selanjutnya buat activity_penyewa.xml dan PenyewaActivity.java
Ini untuk menampilkan daftar orang yang menyewa mobil.
activity_penyewa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbPenyewa"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<LinearLayout
android:id="@+id/ListGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tbPenyewa"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical"
android:paddingBottom="16dp">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:scrollbars="none" />
</LinearLayout>
<Button
android:layout_alignParentBottom="true"
android:id="@+id/tambahPenyewa"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_marginTop="8dp"
android:background="@drawable/button_background"
android:text="Tambah Penyewa"
android:textColor="@android:color/white"
android:textSize="15sp" />
</RelativeLayout>
PenyewaActivity.java
package com.wijaba.rentalmobil.activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.wijaba.rentalmobil.R;
import com.wijaba.rentalmobil.helper.DataHelper;
import static android.R.layout.*;
public class PenyewaActivity extends AppCompatActivity {
String[] daftar;
int[] id;
ListView ListView1;
Menu menu;
protected Cursor cursor;
DataHelper dbcenter;
public static PenyewaActivity m;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_penyewa);
Button tambah = findViewById(R.id.tambahPenyewa);
tambah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent p = new Intent(PenyewaActivity.this, SewaMobilActivity.class);
startActivity(p);
}
});
m = this;
dbcenter = new DataHelper(this);
RefreshList();
setupToolbar();
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.tbPenyewa);
toolbar.setTitle("Daftar Penyewa");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void RefreshList() {
SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM penyewa", null);
daftar = new String[cursor.getCount()];
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
daftar[i] = cursor.getString(0);
}
ListView1 = findViewById(R.id.listView1);
ListView1.setAdapter(new ArrayAdapter(this, simple_list_item_1, daftar));
ListView1.setSelected(true);
ListView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
final String selection = daftar[arg2];
final CharSequence[] dialogitem = {"Lihat Data", "Hapus Data"};
AlertDialog.Builder builder = new AlertDialog.Builder(PenyewaActivity.this);
builder.setTitle("Pilihan");
builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: {
Intent i = new Intent(PenyewaActivity.this, DetailPenyewaActivity.class);
i.putExtra("nama", selection);
startActivity(i);
break;
}
case 1: {
SQLiteDatabase db = dbcenter.getWritableDatabase();
db.execSQL("DELETE FROM penyewa where nama = '" + selection + "'");
db.execSQL("DELETE FROM sewa where nama = '" + selection + "'");
RefreshList();
break;
}
}
}
});
builder.create().show();
}
});
((ArrayAdapter) ListView1.getAdapter()).notifyDataSetInvalidated();
}
}
7. Yang terakhir buat class activity_detail_penyewa.xml dan DetailPenyewaActivity.java
Untuk menampilkan nama, mobil dan harga yang disewa.
activity_detail_penyewa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbDetailPenyewa"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tbDetailPenyewa"
android:clipToPadding="false"
android:fillViewport="false"
android:paddingBottom="10dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:strokeColor="@color/colorPrimary"
app:strokeWidth="2dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/idPenyewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Identitas Penyewa"
android:textColor="@color/colorPrimary"
android:textSize="19sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/idPenyewa"
android:layout_marginTop="20dp"
android:text="Nama Penyewa :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HNama"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtName"
android:text="Penyewa"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
<TextView
android:id="@+id/txtAlamat"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/HNama"
android:layout_marginTop="10dp"
android:text="Alamat :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HAlamat"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtAlamat"
android:text="Penyewa"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
<TextView
android:id="@+id/txtTlp"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/HAlamat"
android:layout_marginTop="10dp"
android:text="No. Telp/HP :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HTelp"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtTlp"
android:text="021"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:strokeColor="@color/colorPrimary"
app:strokeWidth="2dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/dtMobil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Data Mobil"
android:textColor="@color/colorPrimary"
android:textSize="19sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtMerk"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/dtMobil"
android:layout_marginTop="20dp"
android:text="Merk Mobil :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HMerk"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtMerk"
android:text="Kijang Innova"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
<TextView
android:id="@+id/txtHarga"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/HMerk"
android:layout_marginTop="10dp"
android:text="Harga :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HHarga"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtHarga"
android:text="Rp. 500000"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
<TextView
android:id="@+id/txtPromo"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/HHarga"
android:layout_marginTop="10dp"
android:text="Promo :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HPromo"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtPromo"
android:text="Rp. 500000"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
<TextView
android:id="@+id/txtLamaSewa"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/HPromo"
android:layout_marginTop="10dp"
android:text="Lama Sewa :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HLamaSewa"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtLamaSewa"
android:text="5 Hari"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
<TextView
android:id="@+id/txtTotal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/HLamaSewa"
android:layout_marginTop="10dp"
android:text="Total Harga :"
android:textColor="@android:color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/HTotal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/txtTotal"
android:text="Rp. 350000"
android:textColor="@color/colorPrimary"
android:textSize="17sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
DetailPenyewaActivity.java
package com.wijaba.rentalmobil.activity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.wijaba.rentalmobil.R;
import com.wijaba.rentalmobil.helper.DataHelper;
public class DetailPenyewaActivity extends AppCompatActivity {
String sNama, sAlamat, sHP, sMerk, sHarga;
int iLama, iPromo, iTotal;
double dTotal;
protected Cursor cursor;
DataHelper dbHelper;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_penyewa);
dbHelper = new DataHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
cursor = db.rawQuery("select * from penyewa, mobil, sewa where penyewa.nama = sewa.nama AND mobil.merk = sewa.merk AND penyewa.nama = '" + getIntent().getStringExtra("nama") + "'", null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
cursor.moveToPosition(0);
sNama = cursor.getString(0);
sAlamat = cursor.getString(1);
sHP = cursor.getString(2);
sMerk = cursor.getString(3);
sHarga = cursor.getString(4);
iPromo = cursor.getInt(7);
iLama = cursor.getInt(8);
dTotal = cursor.getDouble(9);
}
TextView tvNama = findViewById(R.id.HNama);
TextView tvAlamat = findViewById(R.id.HAlamat);
TextView tvHP = findViewById(R.id.HTelp);
TextView tvMerk = findViewById(R.id.HMerk);
TextView tvHarga = findViewById(R.id.HHarga);
TextView tvLama = findViewById(R.id.HLamaSewa);
TextView tvPromo = findViewById(R.id.HPromo);
TextView tvTotal = findViewById(R.id.HTotal);
tvNama.setText(" " + sNama);
tvAlamat.setText(" " + sAlamat);
tvHP.setText(" " + sHP);
tvMerk.setText(" " + sMerk);
tvHarga.setText(" Rp. " + sHarga);
tvLama.setText(" " + iLama + " hari");
tvPromo.setText(" " + iPromo + "%");
iTotal = (int) dTotal;
tvTotal.setText(" Rp. " + iTotal);
setupToolbar();
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.tbDetailPenyewa);
toolbar.setTitle("Detail Penyewa");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Setelah sudah mengikuti langkah-langkah diatas dengan baik, tinggal
kalian Run saja. InsyaAllah akan berhasil seperti gambar dibawah ini.
Jika kalian mendapati error silahkan kalian cek kembali, jika sudah
di cek masih error silahkan kalian berkomentar dibawah agar kita bisa
diskusikan bersama.
Jika kalian ingin langsung source codenya tanpa tutorial diatas silahkan didownload disini
Sekian tutorial Cara Membuat Aplikasi Sewa Mobil Menggunakan Android Studio dengan Database SQLite
semoga bermanfaat. Dan jangan lupa membagikan artikel ini kepada
teman-teman kalian yang ingin belajar membuat aplikasi Android
sederhana, jangan pelit ilmu pokoknya.
Post a Comment for "Cara Membuat Aplikasi Sewa Mobil Menggunakan Android Studio dengan Database SQLite"