Create Android Application:
- File >> New >> Android Application
- Enter Project name: AlertDialogBoxValidationApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish

1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Button login, clear;
EditText uN, pW;
AlertDialog aDialog;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login =
(Button) findViewById(R.id.login);
aDialog = new AlertDialog.Builder(this).create();
aDialog.setTitle("Error Message");
login.setOnClickListener(this);
clear =
(Button) findViewById(R.id.clear);
clear.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
if (v == this.login) {
uN =
(EditText) findViewById(R.id.unm);
pW =
(EditText) findViewById(R.id.pwd);
String userN = uN.getText().toString();
String passW = pW.getText().toString();
if (userN.equals("") ||
passW.equals("")) {
aDialog.setMessage("Enter
UserName & Password");
aDialog.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface
dialog, int which)
{
return;
}
});
}
if (userN.equals("sunil")
&& passW.equals("android")) {
aDialog.setMessage("Success...!");
//
Intent i = new Intent(Demo.this, MenuControl.class);
//
startActivity(i);
} else
if (userN.equals("sunil")
&& passW.equals("")) {
aDialog.setMessage("Enter
Password");
} else {
if (userN.equals("")
&& passW.equals("android")) {
aDialog.setMessage("Enter
UserName");
}
else {
aDialog.setMessage("Enter
UserName & Password");
}
}
}
aDialog.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface
dialog, int which)
{
return;
}
});
}
aDialog.show();
}
}
2.Simple Copy and Past the below code: actvitiy_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<ImageView
android:id="@+id/main_logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_marginTop="-10dip"
/>
<TextView
android:id="@+id/tunm"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginTop="75dip"
android:text="User Name :"
android:textColor="#aa00aa"
/>
<EditText
android:id="@+id/unm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginRight="5dip"
android:layout_marginTop="65dip"
/>
<TextView
android:id="@+id/tpwd"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginTop="125dip"
android:text=" Password :"
android:textColor="#aa00aa"
/>
<EditText
android:id="@+id/pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginRight="5dip"
android:layout_marginTop="120dip"
android:password="true"
/>
<Button
android:id="@+id/clear"
android:text=" Clear "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dip"
android:layout_marginTop="168dip"
/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/clear"
android:layout_alignBottom="@+id/clear"
android:layout_toRightOf="@+id/tpwd"
android:text=" Login " />
</RelativeLayout>
3.Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When our application launch, without enter data in the fields if you click on submit button you get a Alert Box with Enter UserName and Password, If you enter UserName and clik on Login Then a Alert Box like Enter Password will get,If you enter Password and clik on Login Then a Alert Box like Enter UserName will get.
If You Enter Both Correct The a Alert With Success...! will Fired.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... Android(OS) vs iPhone(iOS) In Android:
No comments:
Post a Comment