How To Make Layout Register and Login-Input Database Mysql - HULONTHALANGI

Breaking

Home Top Ad

Responsive Ads Here

Minggu, 06 Januari 2019

How To Make Layout Register and Login-Input Database Mysql

Assalammualaikum wr. wb.       Pada kesempatan kali ini saya akan menjelakan cara Membuat Layout Register dan Login di android Eclipse yang terhubung dengan web dan Database MySQL. langsung saja.
langkah pertama kita buka android eclipse, kemudian kita membuat project baru dengan cara New>Android Aplication Porject>Buat project baru
setelah itu kita membuat layout register atau activity_registrasi.xml
kodingannya:
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:background=”@drawable/ab”
tools:context=”.Registrasi” >
<TableLayout
android:id=”@+id/layar1″
android:layout_height=”40dp”
android:layout_width=”match_parent”
>
<TableRow>
<TextView
android:id=”@+id/regis”
android:layout_width=”match_parent”
android:layout_height=”50dp”
android:layout_marginLeft=”20dp”
android:layout_marginTop=”5dp”
android:text=”Registrasi”
android:textColor=”#ffffff”
android:textSize=”25dp”
android:textStyle=”bold” />
</TableRow>
</TableLayout>
<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”40dp”
android:textColor=”#ffffff”
android:text=”Nama” />
<EditText
android:id=”@+id/nama”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView2″
android:hint=”nama”
android:layout_below=”@+id/textView2″ />
<TextView
android:id=”@+id/textView3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/nama”
android:layout_below=”@+id/nama”
android:layout_marginTop=”10dp”
android:textColor=”#ffffff”
android:text=”Username” />
<EditText
android:id=”@+id/username”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView3″
android:hint=”username”
android:layout_below=”@+id/textView3″ />
<TextView
android:id=”@+id/textView4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/username”
android:layout_below=”@+id/username”
android:layout_marginTop=”10dp”
android:textColor=”#ffffff”
android:text=”Password” />
<EditText
android:id=”@+id/password”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView4″
android:hint=”password”
android:layout_below=”@+id/textView4″ />
<TextView
android:id=”@+id/textView5″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/password”
android:layout_below=”@+id/password”
android:layout_marginTop=”10dp”
android:textColor=”#ffffff”
android:text=”Keluarga” />
<EditText
android:id=”@+id/keluarga”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView5″
android:hint=”keluarga”
android:layout_below=”@+id/textView5″ />
<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/keluarga”
android:layout_below=”@+id/keluarga”
android:layout_marginTop=”28dp”
android:text=”Simpan”
android:textColor=”#000000″ />
<TextView
android:layout_below=”@+id/button1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:textColor=”#ffffff”
android:id=”@+id/status”
/>
</RelativeLayout>
kemudian kita membuat class registrasi atau registrasi.java
kodingannya:
package android.tracer_family;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Registrasi extends Activity {
EditText nama,keluarga,username,password;
TextView status;
Button simpan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registrasi);
nama = (EditText) findViewById(R.id.nama);
keluarga = (EditText) findViewById(R.id.keluarga);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
simpan = (Button) findViewById(R.id.button1);
status=(TextView)findViewById(R.id.status);
simpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair(“nama”, nama.getText().toString()));
postParameters.add(new BasicNameValuePair(“email”, email.getText().toString()));
postParameters.add(new BasicNameValuePair(“username”, username.getText().toString()));
postParameters.add(new BasicNameValuePair(“password”, password.getText().toString()));
String response = null;
try {
response = CustomHttpClient.executeHttpPost(“http://10.0.2.2/tracer_family/daftar_user.php&#8221;, 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) {
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.registrasi, menu);
return true;
}
}
setelah itu kita membuat class CustomHttpClient.java
kodingannya:
package android.tracer_family;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
public class CustomHttpClient {
/** The time it takes for our client to timeout */
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
/** Single instance of our HttpClient */
private static HttpClient mHttpClient;
/**
* Get our single instance of our HttpClient object.
*
* @return an HttpClient object with connection parameters set
*/
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
/**
* Performs an HTTP Post request to the specified url with the
* specified parameters.
*
* @param url The web address to post the request to
* @param postParameters The parameters to send via the request
* @return The result of the request
* @throws Exception
*/
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer(“”);
String line = “”;
String NL = System.getProperty(“line.separator”);
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Performs an HTTP GET request to the specified url.
*
* @param url The web address to post the request to
* @return The result of the request
* @throws Exception
*/
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer(“”);
String line = “”;
String NL = System.getProperty(“line.separator”);
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
kemudian kita membuat layout masuk atau activity_masuk.xml
kodingan:
<TableLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”@drawable/ab”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.Tanya” >
<TableRow
android:layout_width=”400dp”
android:layout_height=”400dp”
>
<ImageView
android:layout_marginLeft=”-20dp”
android:src=”@drawable/pohon1″
android:layout_gravity=”center”
android:layout_width=”100dp”
android:layout_height=”100dp”/>
</TableRow>
<TableRow android:layout_width=”fill_parent”
android:layout_height=”400dp”>
<TextView
android:layout_width=”200dp”
android:layout_height=”fill_parent”
android:layout_marginLeft=”-29dp”
android:gravity=”center”
android:text=”Penjualan”
android:textColor=”#fff”
android:textSize=”24sp”
android:textStyle=”bold” />
</TableRow>
<TableRow>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:text=”Username :”
android:textColor=”#fff”
android:textStyle=”bold” />
</TableRow>
<TableLayout>
<TableRow
android:background=”#ffffff”
>
<ImageView
android:src=”@drawable/r1″
android:layout_width=”30dp”
android:layout_height=”40dp”/>
<EditText
android:id=”@+id/editText2″
android:layout_width=”254dp”
android:hint=”user”
android:layout_height=”wrap_content”
android:ems=”10″ >
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
<TableRow>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:text=”Password :”
android:textColor=”#fff”
android:textStyle=”bold”
/>
</TableRow>
<TableLayout>
<TableRow
android:background=”#ffffff”
>
<ImageView
android:id=”@+id/r2″
android:src=”@drawable/r2″
android:layout_width=”36dp”
android:layout_height=”50dp”
android:layout_marginLeft=”-5dp”/>
<EditText
android:id=”@+id/editText3″
android:layout_width=”254dp”
android:layout_height=”wrap_content”
android:password=”true”
android:hint=”pass”
android:inputType=”textPassword”
android:ems=”10″ >
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
<TableRow
android:layout_width=”200dp”
android:gravity=”center” >
<Button
android:id=”@+id/btnlogin”
android:layout_width=”270dp”
android:layout_height=”35dp”
android:gravity=”center”
android:layout_marginBottom=”2dp”
android:layout_marginLeft=”10dp”
android:layout_marginTop=”10dp”
android:background=”#ccff00″
android:text=”Login”
android:textColor=”#000000″
android:textStyle=”bold” />
</TableRow>
<TableRow
android:layout_width=”fill_parent”
android:gravity=”center” >
<Button
android:id=”@+id/buat”
android:layout_width=”320dp”
android:layout_height=”35dp”
android:gravity=”center”
android:layout_marginLeft=”10dp”
android:layout_marginRight=”10dp”
android:layout_marginTop=”6dp”
android:background=”#ccffff”
android:text=”Buat Akun”
android:textColor=”#000000″
android:textStyle=”bold” />
</TableRow>
<TextView
android:layout_marginTop=”30dp”
android:textColor=”#000000″
android:text=”Belum punya Akun? Silahkan Buat Akun” />
<TextView
android:id=”@+id/info”
android:layout_marginBottom=”30dp”
android:layout_marginTop=”1dp”
android:layout_marginLeft=”3dp”
android:layout_width=”10dp”
android:textColor=”#ccff00″
android:textStyle=”bold”
android:text=”Info Aplikasi” />
</TableLayout>
kemudian class masuk atau masuk.java
kodingannya:
package android.penjualan;
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;
public class Masuk extends Activity {
/** Called when the activity is first created. */
EditText username,password;
Button login,daftar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_masuk);
username=(EditText) findViewById(R.id.editText2);
password=(EditText) findViewById(R.id.editText3);
login=(Button) findViewById (R.id.btnlogin);
daftar=(Button) findViewById (R.id.buat);
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/penjualan/check.php&#8221;, postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll(“\\s+”,””);
if (res.equals(“1”))
{
Intent s = new Intent (getApplicationContext(),MainActivity.class);
startActivity(s);
}
else {
}
}
catch (Exception e) {
}
}
});
// penutup buka dari public void onCreate
}
public void daftar (View theButton)
{
Intent d = new Intent (this,Registrasi.class);
startActivity(d);
}
// apabila user berhasil login.
}
untuk menghubungkan ke database, kita harus mengaktifkan xampp.
setelah itu, kita membuat koneksi.php untuk terhubung ke database.
kodingannya:
package android.penjualan;
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;
public class Masuk extends Activity {
/** Called when the activity is first created. */
EditText username,password;
Button login,daftar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_masuk);
username=(EditText) findViewById(R.id.editText2);
password=(EditText) findViewById(R.id.editText3);
login=(Button) findViewById (R.id.btnlogin);
daftar=(Button) findViewById (R.id.buat);
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/penjualan/check.php&#8221;, postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll(“\\s+”,””);
if (res.equals(“1”))
{
Intent s = new Intent (getApplicationContext(),MainActivity.class);
startActivity(s);
}
else {
}
}
catch (Exception e) {
}
}
});
// penutup buka dari public void onCreate
}
public void daftar (View theButton)
{
Intent d = new Intent (this,Registrasi.class);
startActivity(d);
}
// apabila user berhasil login.
}

Tidak ada komentar:

Posting Komentar