Wednesday, November 7, 2012

Android Simple Login Dialogue Box

3:53 AM

package com.semicolondev.baghchal;

import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public abstract class test extends Activity
{

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.login, null);
final EditText userText = (EditText) textEntryView.findViewById(R.id.login_userID);
final EditText passText = (EditText) textEntryView.findViewById(R.id.login_password);
AlertDialog.Builder builder = new AlertDialog.Builder(this);  
AlertDialog alert;
builder.setMessage("Enter Username and Password")
.setCancelable(true)
.setView(textEntryView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.i("OB","Logging in ..");
try{
String url = "http://192.168.1.16/api/login?userID="+userText.getText().toString().trim()+"&password="+passText.getText().toString().trim();
Log.i("OB","url= "+url);
String response = OBHttpClient.executeHttpGet(url);
Log.i("OB","Response="+response);              // if valid login response ={"res":"success"}
JSONObject resObj = new JSONObject(response);
String resServer = resObj.getString("res");
if(resServer.equals("success")){
Toast.makeText(this,"Login Success", Toast.LENGTH_LONG).show();
Log.i("OB","Success");
}
else{
Toast.makeText(this,"Login Failed", Toast.LENGTH_LONG).show();
Log.i("OB","Invalid");
}
}catch(Exception e){Toast.makeText(this,"Sorry", Toast.LENGTH_LONG).show();}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {                                                 
}
});
alert = builder.create();
alert.show();

}
}

.... login.xml ....
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<!--  Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Username"/>
<EditText android:id="@+id/login_userID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="20dip"
android:singleLine="true"/>
<!--  Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Password"/>
<EditText android:id="@+id/login_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:password="true"/>
</LinearLayout>

Written by

We are passionate programmers. Enjoying the rich platforms of Semicolon Family of Programming languages we are proud to call ourselves Semicolon Developers.

0 comments:

Post a Comment

 

© 2013 Echo "Semicolon Developers"; Kathmandu. All rights resevered @ Semicolon Developers Network Pvt. Ltd.. Designed by Templateism

Back To Top