Membuat Aplikasi Menu Makanan dengan android Studio

Agustus 01, 2017 0 Comments

Buatlah project baru di android studio beri nama MenuMakanan kemudian pilih empty activity

Step pertama tambahkan dependecies di gradle (Modul App) 'com.android.support:cardview-v7:26.+' dan 'com.android.support:design:26.+' kemudian klik Sync Now.

Selanjutnya adalah copykan foto ke drawable (fotonya ada dalam folder di bawah), copykan juga string.xml, color.xml, dan styles.xml di folder view.

kemudian copykan kode layout activity_main.xml berikut ini :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="id.giviews.menumakanan.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:src="@drawable/logo_rm"
        android:background="@color/colorLogo"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listmakanan">

    </ListView>

</LinearLayout>

kemudian buat activity_detail_makanan lalu copyka code berikut :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout 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"
        android:orientation="vertical"
        tools:context="id.giviews.menumakanan.DetailMakanan">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/imgdetailmakanan"
            android:src="@drawable/bakso" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:fontFamily="monospace"
            android:textStyle="bold"
            android:gravity="center"
            android:text="Nama Makanan"
            android:id="@+id/txtdetailnamamakanan"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:fontFamily="monospace"
            android:textStyle="bold"
            android:gravity="center"
            android:text="Harga Makanan"
            android:id="@+id/txtdetailhargamakanan"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:textStyle="bold"
            android:fontFamily="monospace"
            android:text="Detail Makanan"
            android:id="@+id/txtdetailmakanan"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@drawable/ic_local_phone_black_24dp"
            android:id="@+id/imgcall"/>

    </LinearLayout>

</ScrollView>

dan buat tampilancustomlist.xml ketikan kode berikut:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:cardBackgroundColor="@color/colorAccent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/imgmakanan"
            android:scaleType="fitXY"
            android:src="@drawable/bakso"/>

         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:text="Nama Makanan"
                android:layout_gravity="center"
                android:fontFamily="cursive"
                android:textStyle="bold"
                android:layout_marginLeft="15dp"
                android:id="@+id/txtmakanan"
                android:textColor="#000000"/>

             <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:text="Harga Makanan"
                android:layout_gravity="center"
                android:fontFamily="cursive"
                android:textStyle="bold"
                android:layout_marginLeft="15dp"
                android:id="@+id/txtharga"
                android:textColor="#000000"/>

            </LinearLayout>
    </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

selanjutnya di java MainActivity.java ketikan kode seperti ini:
package id.giviews.menumakanan;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import id.giviews.menumakanan.adapter.CustomListAdapter;

public class MainActivity extends AppCompatActivity {

    //declarasi Variable
    ListView listmakanan;

    //membuat Array
    String nama_makanan[] = {"bakso","gado-gado","Gorengan","Gudeg","Opor-Ayam","Pempek",
            "Rawon","Rendang","Soto","Nasi Kuning","Otak-otak","Sate","Pecel Lele","Ketoprak"};

    String harga_makanan[] = {"Rp. 15.000","Rp. 10.000","Rp. 4.000","Rp. 30.000","Rp. 55.000","Rp. 30.000",
            "Rp. 20.000","Rp. 65.000","Rp. 18.000","Rp. 8.000","Rp. 7.000","Rp. 25.000","Rp. 15.000","Rp. 12.000"};

    int gambar_makanan[]={R.drawable.bakso,R.drawable.gadogado,R.drawable.gorengan,R.drawable.gudeg,
            R.drawable.oporyam,R.drawable.pempek,R.drawable.rawon,R.drawable.rendang,R.drawable.soto,R.drawable.nasikuning,
            R.drawable.otakotak,R.drawable.sate,R.drawable.pecellele,R.drawable.ketoprak};

    int detail_makanan[]={R.string.bakso,R.string.gadogado,R.string.gorengan,R.string.gudeg,
            R.string.oporyam,R.string.pempek,R.string.rawon,R.string.rendang,R.string.soto,R.string.nasikuning,
            R.string.otakotak,R.string.sate,R.string.pecellele,R.string.ketoprak};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //inisialisasi
        listmakanan = (ListView)findViewById(R.id.listmakanan);

        CustomListAdapter adapter = new CustomListAdapter(this,nama_makanan,gambar_makanan,harga_makanan);

        //untuk mengisi data ke widget list view
        listmakanan.setAdapter(adapter);

        //aksi atau event listener dari widget list view

        listmakanan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent kirimdata = new Intent(MainActivity.this,DetailMakanan.class);
                kirimdata.putExtra("gbrM",gambar_makanan[i]);
                kirimdata.putExtra("namaM",nama_makanan[i]);
                kirimdata.putExtra("hargaM",harga_makanan[i]);
                kirimdata.putExtra("detailM",detail_makanan[i]);

                startActivity(kirimdata);
            }
        });

    }
}

lalu untuk DetailMakanan.java sesuikan kodenya seperti ini :
package id.giviews.menumakanan;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailMakanan extends AppCompatActivity {

    ImageView imgdetailmakanan;
    TextView txtdetailnamamakanan;
    TextView txtDetalmakanan;
    TextView txthargamakanan;
    ImageView imgcall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_makanan);

        imgdetailmakanan = (ImageView) findViewById(R.id.imgdetailmakanan);
        imgcall = (ImageView) findViewById(R.id.imgcall);

        imgcall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:089689696474"));
                if (ActivityCompat.checkSelfPermission(DetailMakanan.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
//                        ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
//                       public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                                              int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.

//                    return;

                    String toDial="tel:089689696474";

                    startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(toDial)));

                }
                startActivity(intent);
            }
        });

        txtdetailnamamakanan = (TextView)findViewById(R.id.txtdetailnamamakanan);
        txtDetalmakanan = (TextView)findViewById(R.id.txtdetailmakanan);
        txthargamakanan = (TextView)findViewById(R.id.txtdetailhargamakanan);


        Intent terimadata = getIntent();

        txtdetailnamamakanan.setText(terimadata.getStringExtra("namaM"));
        txthargamakanan.setText(terimadata.getStringExtra("hargaM"));
        txtDetalmakanan.setText(terimadata.getIntExtra("detailM",0));
        imgdetailmakanan.setImageResource (terimadata.getIntExtra("gbrM",0));

    }
}

kemudian buat pakage di java dengan nama adapter. didalam package adapter buat file CustomListAdapter.java dengan kode seperti ini :
package id.giviews.menumakanan;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailMakanan extends AppCompatActivity {

    ImageView imgdetailmakanan;
    TextView txtdetailnamamakanan;
    TextView txtDetalmakanan;
    TextView txthargamakanan;
    ImageView imgcall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_makanan);

        imgdetailmakanan = (ImageView) findViewById(R.id.imgdetailmakanan);
        imgcall = (ImageView) findViewById(R.id.imgcall);

        imgcall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:089689696474"));
                if (ActivityCompat.checkSelfPermission(DetailMakanan.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
//                        ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
//                       public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                                              int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.

//                    return;

                    String toDial="tel:089689696474";

                    startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(toDial)));

                }
                startActivity(intent);
            }
        });

        txtdetailnamamakanan = (TextView)findViewById(R.id.txtdetailnamamakanan);
        txtDetalmakanan = (TextView)findViewById(R.id.txtdetailmakanan);
        txthargamakanan = (TextView)findViewById(R.id.txtdetailhargamakanan);


        Intent terimadata = getIntent();

        txtdetailnamamakanan.setText(terimadata.getStringExtra("namaM"));
        txthargamakanan.setText(terimadata.getStringExtra("hargaM"));
        txtDetalmakanan.setText(terimadata.getIntExtra("detailM",0));
        imgdetailmakanan.setImageResource (terimadata.getIntExtra("gbrM",0));

    }
}

dan terakhir sesuikan AndroidManifest.xml seperti ini:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="id.giviews.menumakanan">

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <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>
        <activity android:name=".DetailMakanan" />
    </application>

</manifest>

Lalu coba jalankan aplikasi anda, jika anda ingin melihat cara penempatanya bisa anda lihat di vidio berikut ini



dan untuk source kodenya bisa anda download di sini

Selamat mencoba jika ada yang error silakan ditanyakan pada kolom komentar di bawah

Juanas Smith Shared

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 komentar :