androidoutput22
androidoutput22
1
PROGRAM NO. 01
Date: 08/08/2024
LOGIN FORM
AIM: Design a Login Form with username and password using LinearLayout and toast
valid
credentials.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN"
android:textAlignment="center"
android:textSize="34sp" />
<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
2
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="login_user"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.login;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditUser=findViewById(R.id.editTextText2);
EditPass=findViewById(R.id.editTextTextPassword);
BtnLogin=findViewById(R.id.button);
}
public void login_user(View view){
String s1= EditUser.getText().toString();
String s2= EditPass.getText().toString();
if(s1.equals("Naaz") && s2.equals("12345")) {
Toast.makeText(this, "Login Successfully", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
}
3
OUTPUT
4
PROGRAM NO. 02
Date: 22/08/2024
ACTIVITY LIFECYCLE
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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=".MainActivity"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "create", Toast.LENGTH_LONG).show();
}
protected void onStart(){
super.onStart();
Toast.makeText(this, "start", Toast.LENGTH_LONG).show();
}
protected void onPause(){
5
super.onPause();
Toast.makeText(this, "pause", Toast.LENGTH_SHORT).show();
}
protected void onResume(){
super.onResume();
Toast.makeText(this, "Resume", Toast.LENGTH_SHORT).show();
}
protected void onRestart(){
super.onRestart();
Toast.makeText(this, "Restart", Toast.LENGTH_SHORT).show()}}protected void
onStop(){
super.onStop();
Toast.makeText(this, "Stop", Toast.LENGTH_SHORT).show()}}
OUTPUT
6
7
PROGRAM NO. 03
Date: 29/08/2024
SIMPLE CALCULATOR
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/f_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="First Number" />
<EditText
android:id="@+id/f_edi1"
android:layout_width="0dp"
android:layout_height="wrap_content"
8
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/l_num"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Last Number" />
<EditText
android:id="@+id/ed_num"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
9
android:text="+" /><Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/re_s"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Result" />
<TextView
android:id="@+id/res_view"
10
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout></LinearLayout>
MainActivity.java
package com.example.assignment2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize EditTexts
ed1 = findViewById(R.id.ed_text1);
ed2 = findViewById(R.id.ed_text2);
// Initialize Buttons
b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
// Initialize TextView
n4 = findViewById(R.id.textView4);
11
// Set onClickListeners for buttons
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("+");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("-");
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("*");
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("/");
}
});
}
switch (operation) {
case "+":
RES = i1 + i2;
break;
case "-":
12
RES = i1 - i2;
break;
case "*":
RES = i1 * i2;
break;
case "/":
if (i2 == 0) {
n4.setText("Error: Divide by zero");
return;
}
RES = i1 / i2;
break;
default:
n4.setText("Error: Unknown operation");
return;
}
n4.setText("Result: " + RES);
}
}
OUTPUT
13
14