Question -4
Create an Login application as above . On successful login , open browser with any URL.Step 1: Create New Project in Eclipse.
Step 2: Edit Main_Activity layout xml file from res/layout folder in your project
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/loginform"
android:textSize="40dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="41dp"
android:text="@string/username" />
<EditText
android:id="@+id/uname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:ems="10"
android:hint="@string/enter_username" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/uname"
android:layout_alignRight="@+id/uname"
android:layout_below="@+id/uname"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="@string/enter_password"
android:inputType="textPassword" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/password"
android:layout_alignBottom="@+id/password"
android:layout_alignParentLeft="true"
android:text="@string/password" />
<Button
android:id="@+id/btnlogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:enabled="false"
android:text="@string/login" />
</RelativeLayout>
Step 3: Edit MainActivity.java file from src/package/ folder in your project
package com.example.loginuri;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
a public class MainActivity extends Activity implements OnClickListener,TextWatcher
{
Button login;
EditText uname;
EditText pwd;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button)findViewById(R.id.btnlogin);
uname = (EditText)findViewById(R.id.uname);
pwd = (EditText)findViewById(R.id.password);
login.setOnClickListener(this);
uname.addTextChangedListener(this);
pwd.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void afterTextChanged(Editable arg0)
{
if(uname.getText().toString().equalsIgnoreCase("sahin") && pwd.getText().toString().equalsIgnoreCase("raj"))
{
login.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View arg0)
{
Intent i = new Intent("android.intent.action.VIEW",Uri.parse("http://licamca.blogspot.com"));
startActivity(i);
}
}
No comments:
Post a Comment