banner

Wednesday, May 16, 2018

author photo
Membuat Aplikasi Android Produk Halal MUI Menggunakan Android Studio
Assalamualaikum warahmatullahi wabarakatuh...
Pada posting sebelumnya saya sudah berbagi tentang Tutorial Membuat Login dengan Username dan Hak Akses di Laravel 5.5. Nah pada kesempatan kali ini kita akan belajar membuat aplikasi android sederhana yaitu Produk Halal MUI. Aplikasi ini berguna untuk mencari produk yang sudah disertifikasi halal oleh MUI (Majelis Ulama Indonesia).

Apa saja yang harus kita siapkan?

  1. Personal Computer (PC) atau laptop yang sudah terinstall Android Studio
  2. Smartphone Android (Optional)
  3. Kabel USB (Optional)
Pada tutorial ini kita akan memanfaatkan JSON API yang sudah dibuat oleh Bachors. Data yang diambil langsung dari situs resmi MUI (Majelis Ulama Indonsia). Ok lanjut ke langkah pembuatan aplikasi android produk halal MUI menggunakan Android Studio.

Langkah-langkah pembuatan :

  1. Buat projek baru di Android Studio. Pilih File > New Project dan pilih Basic Activity.
  2. Buka build.gradle yang ada di folder app. Lalu tambahkan librari berikut ini. Lalu sync project, pastikan PC / Laptop kalian terhubung ke internet untuk mendownload librari.
  3. dependencies {
        implementation 'com.android.support:cardview-v7:27.0.2'
        implementation 'com.squareup.retrofit2:retrofit:2.4.0'
        implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
        implementation 'com.android.support:recyclerview-v7:27.0.2'
    }
    
  4. Selanjutnya buka AndroidManifest.xml lalu tambahkan internet permission.
    <uses-permission android:name="android.permission.INTERNET"/>
  5. <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.cakfan.produkhalalmui">
    
        <uses-permission android:name="android.permission.INTERNET"/>
    
        <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=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  6. Buka colors.xml lalu ubah sesuai selera teman-teman, untuk lebih gampangnya bisa buka materialpalette.com
  7. <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#4caf50</color>
        <color name="colorPrimaryDark">#388e3c</color>
        <color name="colorAccent">#ff9800</color>
    </resources>
  8. Selanjutnya buka string.xml ubah seperti berikut.
  9. <resources>
        <string name="app_name">Produk Halal MUI</string>
        <string name="keterangan">Cari produk makanan, minuman dan obat-obatan yang telah lolos spesifikasi halal oleh Majelis Ulama Indonesia (MUI). Cari produk berdasarkan nama produk, nama produsen atau nomor sertifikat.</string>
        <string name="nama_produk">Nama Produk</string>
        <string name="produsen">Produsen</string>
        <string name="nama_produsen">Nama Produsen</string>
        <string name="nomor_sertifikat">Nomor Sertifikat</string>
        <string name="berlaku_hingga">Berlaku Hingga</string>
        <string name="_27_september_2018">27 September 2018</string>
        <string name="_00090000300799">00090000300799</string>
        <string name="bagikan">Bagikan</string>
        <string name="belum_ada_data_yang_ditampilkan">Belum ada data yang ditampilkan.</string>
        <string name="nama_produk_nama_produsen_nomor_sertifikat">Nama Produk, Nama Produsen, Nomor Sertifikat</string>
        <string name="pencarian">Pencarian</string>
    </resources>
  10. Buka activity_main.xml lalu ubah menjadi seperti berikut ini.
  11. <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <android.support.v7.widget.CardView
            android:id="@+id/cardView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <EditText
                    android:id="@+id/input_keyword"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:ems="10"
                    android:hint="@string/nama_produk_nama_produsen_nomor_sertifikat"
                    android:inputType="text"
                    android:singleLine="true"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/btn_cari"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
                <ImageView
                    android:id="@+id/btn_cari"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginTop="16dp"
                    android:contentDescription="@string/pencarian"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@mipmap/ic_btn_cari" />
            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.CardView>
    
        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="136dp"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView2" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_halal"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView2">
    
        </android.support.v7.widget.RecyclerView>
    
        <include
            android:id="@+id/tidak_ada_data"
            layout="@layout/tidak_ada_data"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/progressBar" />
    
    </android.support.constraint.ConstraintLayout>
  12. Buat layout baru, caranya klik kanan di res > layout berinama produk_halal.xml. Lalu ubah menjadi seperti berikut.
  13. <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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="wrap_content">
    
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <TextView
                    android:id="@+id/tv_nama_produk"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:padding="8dp"
                    android:text="@string/nama_produk"
                    android:textAlignment="viewStart"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textColor="@android:color/white"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:text="@string/produsen"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tv_nama_produk" />
    
                <TextView
                    android:id="@+id/tv_nama_produsen"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:text="@string/nama_produsen"
                    android:textColor="#212121"
                    android:textSize="16sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textView2" />
    
                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="16dp"
                    android:text="@string/nomor_sertifikat"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tv_nama_produsen" />
    
                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginTop="16dp"
                    android:text="@string/berlaku_hingga"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tv_nama_produsen" />
    
                <TextView
                    android:id="@+id/tv_berlaku"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginTop="8dp"
                    android:text="@string/_27_september_2018"
                    android:textColor="@color/colorAccent"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textView7" />
    
                <TextView
                    android:id="@+id/tv_nomor_sertifikat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:text="@string/_00090000300799"
                    android:textColor="#212121"
                    android:textSize="16sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textView5" />
    
                <View
                    android:id="@+id/divider"
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_marginTop="8dp"
                    android:background="?android:attr/listDivider"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tv_nomor_sertifikat" />
    
                <Button
                    android:id="@+id/btn_bagikan"
                    style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginTop="8dp"
                    android:text="@string/bagikan"
                    android:textColor="@color/colorPrimary"
                    android:textColorLink="@color/colorPrimary"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/divider" />
    
            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.CardView>
    </android.support.constraint.ConstraintLayout>
  14. Buat layout baru lagi dengan nama tidak_ada_data.xml. Lalu ubah menjadi seperti berikut.
  15. <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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="wrap_content">
    
        <TextView
            android:id="@+id/tv_belum_ada"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/belum_ada_data_yang_ditampilkan"
            android:textAlignment="center"
            android:textColor="#212121"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent" />
    
        <TextView
            android:id="@+id/tv_keterangan"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="32dp"
            android:text="@string/keterangan"
            android:textAlignment="center"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_belum_ada" />
    </android.support.constraint.ConstraintLayout>
  16. Lalu buka MainActivity.java dan ubah menjadi seperti berikut ini.
  17. import android.annotation.SuppressLint;
    import android.support.annotation.NonNull;
    import android.support.constraint.ConstraintLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.text.Html;
    import android.text.Layout;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    
    public class MainActivity extends AppCompatActivity {
    
        private EditText keyword;
        private ImageView btn_cari;
    
        private List<DataHalal> data = new ArrayList<>();
    
        private ProgressBar progressBar;
        private RecyclerView halal_list;
        private LinearLayoutManager manager;
        private HalalAdapter halalAdapter;
    
        private ConstraintLayout tidak_ada;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            keyword = findViewById(R.id.input_keyword);
            btn_cari = findViewById(R.id.btn_cari);
            progressBar = findViewById(R.id.progressBar);
            halal_list = findViewById(R.id.list_halal);
            tidak_ada = findViewById(R.id.tidak_ada_data);
    
            btn_cari.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (!TextUtils.isEmpty(keyword.getText())){
    
                        tidak_ada.setVisibility(View.GONE);
                        progressBar.setVisibility(View.VISIBLE);
                        manager.scrollToPositionWithOffset(0,0);
                        data.clear();
                        dapatkanData();
    
                    } else {
                        Toast.makeText(MainActivity.this, "Masukkan nama barang, nama produsen atau nomor sertifikat.",
                                Toast.LENGTH_LONG).show();
                    }
    
                }
            });
    
            manager = new LinearLayoutManager(this);
            halalAdapter = new HalalAdapter(this,data);
            halal_list.setLayoutManager(manager);
            halal_list.setAdapter(halalAdapter);
    
        }
    
        private void dapatkanData() {
    
            String url_satu = API.BASE_URL + API.KEY + API.MENU + API.NAMA_PRODUK + API.KUERI + keyword.getText();
            final String url_dua = API.BASE_URL + API.KEY  + API.MENU + API.NAMA_PRODUSEN + API.KUERI + keyword.getText();
            final String url_tiga = API.BASE_URL + API.KEY + API.MENU + API.NOMOR_SERTIFIKAT + API.KUERI + keyword.getText();
    
            final Call<ProdukHalal> produkHalal = API.getService().getProdukList(url_satu);
    
            produkHalal.enqueue(new Callback<ProdukHalal>() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onResponse(Call<ProdukHalal> call, Response<ProdukHalal> response) {
    
                    ProdukHalal halal = response.body();
    
                    if (!halal.getStatus().equals("error")) {
                        data.addAll(halal.getData());
                        halalAdapter.notifyDataSetChanged();
                        progressBar.setVisibility(View.GONE);
    
                        //Toast.makeText(MainActivity.this,"Hasil: "+ halal.toString(),Toast.LENGTH_LONG).show();
                    } else {
                        final Call<ProdukHalal> produkHalal2 = API.getService().getProdukList(url_dua);
                        produkHalal2.enqueue(new Callback<ProdukHalal>() {
                            @Override
                            public void onResponse(Call<ProdukHalal> call, Response<ProdukHalal> response) {
                                ProdukHalal halal2 = response.body();
                                if (!halal2.getStatus().equals("error")){
                                    data.addAll(halal2.getData());
                                    halalAdapter.notifyDataSetChanged();
                                    progressBar.setVisibility(View.GONE);
                                } else {
                                    final Call<ProdukHalal> produkHalal3 = API.getService().getProdukList(url_tiga);
                                    produkHalal3.enqueue(new Callback<ProdukHalal>() {
                                        @Override
                                        public void onResponse(Call<ProdukHalal> call, Response<ProdukHalal> response) {
    
                                            ProdukHalal halal3 = response.body();
                                            if (!halal3.getStatus().equals("error")){
                                                data.addAll(halal3.getData());
                                                halalAdapter.notifyDataSetChanged();
                                                progressBar.setVisibility(View.GONE);
                                            } else {
                                                progressBar.setVisibility(View.GONE);
                                                TextView error, ket;
                                                tidak_ada.setVisibility(View.VISIBLE);
                                                error = findViewById(R.id.tv_belum_ada);
                                                ket = findViewById(R.id.tv_keterangan);
                                                error.setText(Html.fromHtml("Oppps.. Hasil pencarian untuk <u><i>"+ keyword.getText() +"</i></u> tidak ditemukan."));
                                                ket.setText(R.string.keterangan);
                                            }
    
                                        }
    
                                        @Override
                                        public void onFailure(Call<ProdukHalal> call, Throwable t) {
    
                                        }
                                    });
                                }
    
                            }
    
                            @Override
                            public void onFailure(Call<ProdukHalal> call, Throwable t) {
    
                            }
                        });
    
                    }
    
    
                }
    
                @Override
                public void onFailure(Call<ProdukHalal> call, Throwable t) {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this,"Gagal memuat hasil, perisak koneksi internet anda.",Toast.LENGTH_LONG).show();
    
                }
            });
    
        }
    }
  18. Buat class baru beri nama dengan API.java lalu ubah menjadi seperti berikut ini. Class ini berfungsi untuk memanggi url json.
  19. import retrofit2.Call;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;
    import retrofit2.http.GET;
    import retrofit2.http.Url;
    
    public class API {
    
        public static final String BASE_URL = "https://sites.google.com/";
        public static final String KEY = "macros/exec?service=AKfycbx_-gZbLP7Z2gGxehXhWMWDAAQsTp3e3bmpTBiaLuzSDQSbIFWD";
        public static final String MENU = "&menu=";
        public static final String KUERI = "&query=";
        public static final String NAMA_PRODUK = "nama_produk";
        public static final String NAMA_PRODUSEN = "nama_produsen";
        public static final String NOMOR_SERTIFIKAT = "nomor_sertifikat";
    
        public static PostService postService = null;
    
        public static PostService getService()
        {
            if(postService == null)
            {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
    
                postService = retrofit.create(PostService.class);
            }
            return postService;
        }
    
        public interface PostService {
            @GET
            Call<ProdukHalal> getProdukList(@Url String url);
        }
    
    }
  20. Buat class baru beri nama dengan ProdukHalal.java lalu ubah menjadi seperti berikut ini. Class ini berfungsi untuk mengambil data dari JSON API.
  21. import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    import java.util.List;
    
    public class ProdukHalal {
    
        @SerializedName("status")
        @Expose
        private String status;
    
        @SerializedName("data")
        @Expose
        private List<DataHalal> data = null;
    
        @SerializedName("next_page")
        @Expose
        private String next_page;
    
        public ProdukHalal(){}
    
        public ProdukHalal(String status, List<DataHalal> data, String next_page) {
            this.status = status;
            this.data = data;
            this.next_page = next_page;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        public List<DataHalal> getData() {
            return data;
        }
    
        public void setData(List<DataHalal> data) {
            this.data = data;
        }
    
        public String getNext_page() {
            return next_page;
        }
    
        public void setNext_page(String next_page) {
            this.next_page = next_page;
        }
    }
  22. Buat class baru beri nama dengan DataHalal.java lalu ubah menjadi seperti berikut ini. Class ini berfungsi untuk mengambil data dari JSON Array.
  23. import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class DataHalal {
    
        @SerializedName("title")
        @Expose
        private String title;
    
        @SerializedName("nomor_sertifikat")
        @Expose
        private String nomor_sertifikat;
    
        @SerializedName("produsen")
        @Expose
        private String produsen;
    
        @SerializedName("berlaku_hingga")
        @Expose
        private String berlaku_hingga;
    
        public DataHalal(){}
    
        public DataHalal(String title, String nomor_sertifikat, String produsen, String berlaku_hingga) {
            this.title = title;
            this.nomor_sertifikat = nomor_sertifikat;
            this.produsen = produsen;
            this.berlaku_hingga = berlaku_hingga;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getNomor_sertifikat() {
            return nomor_sertifikat;
        }
    
        public void setNomor_sertifikat(String nomor_sertifikat) {
            this.nomor_sertifikat = nomor_sertifikat;
        }
    
        public String getProdusen() {
            return produsen;
        }
    
        public void setProdusen(String produsen) {
            this.produsen = produsen;
        }
    
        public String getBerlaku_hingga() {
            return berlaku_hingga;
        }
    
        public void setBerlaku_hingga(String berlaku_hingga) {
            this.berlaku_hingga = berlaku_hingga;
        }
    }
  24. Buat class baru beri nama dengan HalalAdapter.java lalu ubah menjadi seperti berikut ini. Class ini berfungsi untuk menampilkan data yang diperoleh ke recyclerview.
  25. import android.content.Context;
    import android.content.Intent;
    import android.support.v7.widget.RecyclerView;
    import android.text.Html;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.List;
    
    public class HalalAdapter extends RecyclerView.Adapter<HalalAdapter.ViewHolder>{
    
        private Context context;
        private List<DataHalal> dataHalal;
    
        HalalAdapter(Context context, List<DataHalal> dataHalal) {
            this.context = context;
            this.dataHalal = dataHalal;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.produk_halal, parent, false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            final DataHalal halal = dataHalal.get(position);
            holder.nama_produk.setText(halal.getTitle());
            holder.nama_produsen.setText(halal.getProdusen());
            holder.nomor_sertifikat.setText(halal.getNomor_sertifikat());
            holder.berlaku.setText(halal.getBerlaku_hingga());
    
            holder.btn_bagikan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent s = new Intent(Intent.ACTION_SEND);
                    s.setType("text/plain");
                    String share = String.valueOf(Html.fromHtml("Nama Produk : <b>" + halal.getTitle() +
                            "</b><br> Nama Produsen : <b>" + halal.getProdusen() +
                            "</b><br> Nomor Sertifikat : <b>" + halal.getNomor_sertifikat() +
                            "</b><br> Berlaku : <b><i>" + halal.getBerlaku_hingga() +"</i></b>" ));
                    s.putExtra(Intent.EXTRA_TEXT, share);
                    context.startActivity(s);
                }
            });
            //Toast.makeText(context, halal.toString(),Toast.LENGTH_LONG).show();
    
        }
    
        @Override
        public int getItemCount() {
            return dataHalal.size();
        }
    
        class ViewHolder extends RecyclerView.ViewHolder {
    
            TextView nama_produk,nama_produsen,nomor_sertifikat,berlaku;
            Button btn_bagikan;
    
            ViewHolder(View itemView) {
                super(itemView);
                nama_produk = itemView.findViewById(R.id.tv_nama_produk);
                nama_produsen = itemView.findViewById(R.id.tv_nama_produsen);
                nomor_sertifikat = itemView.findViewById(R.id.tv_nomor_sertifikat);
                berlaku = itemView.findViewById(R.id.tv_berlaku);
                btn_bagikan = itemView.findViewById(R.id.btn_bagikan);
            }
        }
    }
  26. Jalankan aplikasi, hubungkan perangkat android kalian menggunakan kabel USB dan pastikan USB Debuggin sudah aktif. Lalu klik Run, kemudian pilih pilih perangkat dan klik OK.
Aplikasi Android Produk Halal MUI Menggunakan Android Studio
Demikian posting kali ini, jika teman-teman punya pertanyaan bisa disampaikan di kolom komentar di bawah. Atau teman-teman punya kritik dan saran bisa hubungi saya langsung melalui facebook, twitter atau instagram yang juga ada di bawah. 😃
your advertise here
Load comments

This post have 0 komentar


EmoticonEmoticon

This Is The Newest Post
Previous article Previous Post

Advertisement

Themeindie.com