0% found this document useful (0 votes)
38 views3 pages

Activity XML

The document contains code for an Android login application. It includes an XML layout file with EditText fields for a username and password and a login button. The MainActivity class handles validating the login by comparing the input to hardcoded credentials and launching a new activity on success. The second activity's layout contains a text view to display a message.

Uploaded by

takla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Activity XML

The document contains code for an Android login application. It includes an XML layout file with EditText fields for a username and password and a login button. The MainActivity class handles validating the login by comparing the input to hardcoded credentials and launching a new activity on success. The second activity's layout contains a text view to display a message.

Uploaded by

takla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Activity xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context=".MainActivity">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:textSize="24sp" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:hint="Password"
android:textSize="24sp" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:lineSpacingExtra="36sp"
android:layout_centerHorizontal="true"
android:text="Sign in" />

</RelativeLayout>

Mainactivity.java

package com.example.bishal_2.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText Username;


private EditText Password;
private Button Login;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Username = (EditText)findViewById(R.id.name);
Password = (EditText)findViewById(R.id.password);
Login = (Button)findViewById(R.id.btn);

Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validate(Username.getText().toString(), Password.getText().toString());
}
});

public void validate(String username, String uspassword){


if((username.equals("Alpesh"))){
if(uspassword.equals("Alpesh")){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
else{
Toast.makeText(MainActivity.this,"Default User name and password is
Alpesh",Toast.LENGTH_LONG).show();
}
}

Main2activity

package com.example.bishal_2.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

Activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is second activity"
android:textSize="30sp" />

</RelativeLayout>

You might also like