Tutorial Membuat Menu Button Image dan Alert Exit pada Aplikasi Android

1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:background="@android:color/black" >

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:background="@android:color/white" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" Menu Wisata Bernaveld "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/black"
            android:textStyle="bold" />
    </RelativeLayout>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout2"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="35dp"
        android:onClick="bernavel"
        android:src="@drawable/button_bernavel" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_marginRight="22dp"
        android:onClick="profil"
        android:src="@drawable/button_profil" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton1"
        android:layout_below="@+id/imageButton1"
        android:text="DAFTAR SPBU"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_below="@+id/imageButton2"
        android:text="  Profil"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:onClick="About"
        android:src="@drawable/button_about" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton3"
        android:layout_below="@+id/imageButton3"
        android:layout_marginLeft="20dp"
        android:text="About"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView4"
        android:layout_alignLeft="@+id/textView2"
        android:onClick="Exit"
        android:src="@drawable/button_exit" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignRight="@+id/textView2"
        android:layout_marginRight="29dp"
        android:text="Exit"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>

2. MainActivity.java

package com.develup.spbu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public final class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

public static final int Exit = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
  findViewById(R.id.imageButton1).setOnClickListener(this);
  findViewById(R.id.imageButton2).setOnClickListener(this);
  findViewById(R.id.imageButton3).setOnClickListener(this);
  findViewById(R.id.imageButton4).setOnClickListener(this);

    }
    public void onClick(View v){
    switch(v.getId()) {
    case R.id.imageButton1:
    Intent f = new Intent(this, satu.class);
    startActivity(f);
    break;
    case R.id.imageButton2:
    Intent e = new Intent(this, dua.class);
    startActivity(e);
    break;
    case R.id.imageButton3:
    Intent z = new Intent(this, tiga.class);
    startActivity(z);
    break;
    case R.id.imageButton4:
    showDialog(Exit);
    break;
    }
    }
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch (id) {
case Exit:
}
return builder.setMessage("Anda Yakin Ingin Keluar?")
.setCancelable(false).setPositiveButton("Ya",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
}).setNegativeButton("Tidak",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}

}

Selanjutnya Buat Class Class Baru Seperti misal satu.class dua.class tiga.class

Berikut Output dari codingan diatas :







Previous
Next Post »

EmoticonEmoticon

Catatan: Hanya anggota dari blog ini yang dapat mengirim komentar.