Pada Pertemuan Kali Ini saya akan membuat aplikasi Client-Server Pemesanan Lapangan Futsal, Tapi pada blog kali ini saya hanya akan fokus pada Clientnya saja.
Ok, langsung saja ...
- langkah pertama yang harus kita lakukan adalah membuat project baru pada Aplikasi ADT atau sejenisnya .
dengan cara klik pada menu file -> new -> Android Application Project
kemudian kita beri nama disni nama project saya adalah "akta" dengan nama file xml "login".
dengan format project seperti pada gambar di bawah ini:
-login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widg37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bg"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:layout_marginTop="120dp"
android:id="@+id/widget37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_marginLeft="15dp"
android:id="@+id/widget45"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget47"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Username :" />
<EditText
android:id="@+id/txtusername"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_marginLeft="15dp"
android:id="@+id/widget46"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget55"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Password :" />
<EditText
android:id="@+id/txtpassword"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text=""
android:password="True"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/w7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TableRow
android:layout_marginLeft="90dp"
android:id="@+id/widget65"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnlogin"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="L O G I N" />
<Button
android:id="@+id/btndaftar"
android:onClick="daftar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DAFTAR" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/txtstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
- daftar_user.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/widget124"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_marginTop="80dp"
android:id="@+id/widget124"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TableLayout
android:id="@+id/widget125"
android:layout_marginLeft="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:id="@+id/widget149"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget150"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:id="@+id/txtusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget152"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget153"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="@+id/txtpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:password="True"
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget129"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget130"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama" />
<EditText
android:id="@+id/txtnama"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget135"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget136"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail" />
<EditText
android:id="@+id/txtemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget135"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget136"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Telp" />
<EditText
android:id="@+id/txttelp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/widget143"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/widget144"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alamat" />
<EditText
android:id="@+id/txtalamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/widget125"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:layout_marginLeft="25dp"
android:id="@+id/widget155"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnsimpan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIMPAN" />
<Button
android:id="@+id/btnexit"
android:onClick="keluar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E X I T" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/txtstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</ScrollView>
- login.java
package com.wilis;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class login extends Activity {
/** Called when the activity is first created. */
EditText username,password;
TextView status;
Button login,daftar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username=(EditText) findViewById(R.id.txtusername);
password=(EditText) findViewById(R.id.txtpassword);
status=(TextView) findViewById (R.id.txtstatus);
login=(Button) findViewById (R.id.btnlogin);
daftar=(Button) findViewById (R.id.btndaftar);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", username
.getText().toString()));
postParameters.add(new BasicNameValuePair("password", password
.getText().toString()));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/myfutsal/check.php", postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll("\\s+","");
status.setText(res);
if (res.equals("1"))
{
status.setText("Anda Berhasil Login");
berhasil(v);
}
else {
// status.setText(res.toString());
Toast toast = Toast.makeText(getApplicationContext(), "username or password wrong", Toast.LENGTH_LONG);
toast.show();
}
}
catch (Exception e) {
status.setText(e.toString());
}
}
});
// penutup buka dari public void onCreate
}
public void daftar (View theButton)
{
Intent d = new Intent (this,daftar_user.class);
startActivity(d);
}
// apabila user berhasil login.
public void berhasil (View theButton)
{
Intent s = new Intent (this, Home.class);
startActivity(s);
}
}
- daftar_user.java
package com.wilis;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class daftar_user extends Activity {
EditText username,password,nama,email,no_telp,alamat;
TextView status;
Button simpan;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftar_user);
username=(EditText)findViewById(R.id.txtusername);
password=(EditText)findViewById(R.id.txtpassword);
nama=(EditText)findViewById(R.id.txtnama);
email=(EditText)findViewById(R.id.txtemail);
no_telp=(EditText)findViewById(R.id.txttelp);
alamat=(EditText)findViewById(R.id.txtalamat);
simpan=(Button)findViewById(R.id.btnsimpan);
// keluar=(Button)findViewById(R.id.btnexit);
status=(TextView)findViewById(R.id.txtstatus);
simpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//atur variabel utk menampung pilihan jenis kelamin
String type=null;
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", username.getText().toString()));
postParameters.add(new BasicNameValuePair("password", password.getText().toString()));
postParameters.add(new BasicNameValuePair("nama_lengkap", nama.getText().toString()));
postParameters.add(new BasicNameValuePair("email", email.getText().toString()));
postParameters.add(new BasicNameValuePair("no_telp", no_telp.getText().toString()));
postParameters.add(new BasicNameValuePair("alamat_lengkap", alamat.getText().toString()));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/myfutsal/aksi_daftar.php", postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll("\\s+","");
status.setText(res);
if (res.equals("1")) status.setText("Data tidak Tersimpan Ke server");
else status.setText("Data berhasil disimpan ke server");
}
catch (Exception e) {
username.setText(e.toString());
}
}
});
}
public void keluar (View theButton)
{
Intent a = new Intent (this,login.class);
startActivity(a);
}
}
hasil Running :