Pages

Diberdayakan oleh Blogger.

Sabtu, 18 Oktober 2014

Membuat form login sederhana di android


Saya kali ini membuat aplikasi sederhana di android yakni form login. form login yang saya buat di sini hanya form login yang tidak membutuhkan koneksi ke data base manapun, sebab di sini saya menentukan manual apa yang harus diinput untuk melakuka login.

langkah - langkah nya adalah sebagai berikut :


  • Buat duluh sebuah project di eclipse gengan Nama/judul yang sesuai aplikasi nya, yang disini adalah "form login".
  • Setelah projectnya jadi, silahkan buka file main.xml yang berada di folder /res/layout, pada main.xml kita tambahkan beberapa komponen yang dibutuhkan untuk form login dan atur posisi serta jenis dan type yang diterima sebagai inputan. berikut script pada main.xml :


<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” android:background=”#ffffaa”><TextView
android:layout_height=”wrap_content”
android:textColor=”#101086″
android:layout_width=”fill_parent”
android:soundEffectsEnabled=”true”
android:cursorVisible=”true”
android:textSize=”25px”
android:text=”~{ Data Private Apps }~”
android:paddingLeft=”30px”/>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”User Name :”
android:id=”@+id/Txtusername”
android:layout_below=”@+id/jdl”></TextView>
<EditText
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:id=”@+id/username”
android:singleLine=”true”
android:layout_below=”@+id/Txtusername”></EditText>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/passwd”
android:text=”Password :”></TextView>
<EditText
android:layout_height=”wrap_content”
android:id=”@+id/passwd_input”
android:password=”true” android:layout_width=”fill_parent”></EditText>
<Button
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:id=”@+id/masuk”
android:text=”Masuk &gt;&gt;”></Button>
<TextView
android:id=”@+id/TextView01″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”CopyRight by Ardhy | 2011″></TextView>
</LinearLayout>




Berikut tampilan dari main.xml :


Gambar Form Login

    1. buat file dengan nama admin.xml,isi nya sesuai yang di inginkan,  file ini yang akan ditampilkan ketika proses login tadi berhasil.
    2. edit file java yang berada di folder /src/nama_paket, isinya seperti di bawah ini :

package app.input.data;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class formInput extends Activity implements OnClickListener {

// deklarasi variable
Button masuk;
AlertDialog alert;
EditText password;
EditText user;
String isi_passwd, username;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// inisialisasi variabel
masuk = (Button) this.findViewById(R.id.masuk);
masuk.setOnClickListener(this);
// edit text untuk username
user = (EditText) findViewById(R.id.username);
user.getText();
// edit text untuk password
password = (EditText) findViewById(R.id.passwd_input);
password.getText();
username=”owner”;
isi_passwd = “loginlah”;
}
@Override
// method untuk override tombol masuk
public void onClick(View tombol_act) {
// TODO Auto-generated method stub
if (tombol_act == masuk) {
if (password.getText().toString().equals(isi_passwd) && user.getText().toString().equals(username)) {
AlertDialog.Builder pesan = new AlertDialog.Builder(this);
pesan.setMessage(“UserName dan Password anda benar”)
.setCancelable(false).setPositiveButton(“OK”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
setContentView(R.layout.admin);
}
});
alert = pesan.create();
alert.show();
} else {
AlertDialog.Builder pesan = new AlertDialog.Builder(this);
pesan.setMessage(“User Name :” + user.getText() + ” dan Password :” + password.getText() + ” anda masih SALAH”)
.setCancelable(false).setPositiveButton(“OK”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
user.setText(“”);
password.setText(“”);
}
});
alert = pesan.create();
alert.show();
}
}
}
}
 


setelah itu jalankan programnya tampilannya akan seperti pada gambar-gambar di bawah ini :

Gambar 1
Gambar 2

Gambar 3