0% found this document useful (0 votes)
7 views

androidoutput22

GJM has been in my head

Uploaded by

fathima
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)
7 views

androidoutput22

GJM has been in my head

Uploaded by

fathima
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/ 14

COURSE OUTCOME 1(CO1)

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;

public class MainActivity extends AppCompatActivity {


EditText EditUser,EditPass;
Button BtnLogin;

@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

AIM: Write a program that demonstrates 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

AIM: Implementing basic arithmetic operations of a 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;

public class MainActivity extends AppCompatActivity {


EditText ed1;
EditText ed2;
Button b1;
Button b2;
Button b3;
Button b4;
Integer i1;
Integer i2;
Integer RES = 0;
TextView n4;

@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("/");
}
});
}

public void performOperation(String operation) {


// Error handling for empty input fields
if (ed1.getText().toString().isEmpty() || ed2.getText().toString().isEmpty()) {
n4.setText("Error: Please enter both numbers");
return;
}

// Parse the numbers


try {
i1 = Integer.parseInt(ed1.getText().toString());
i2 = Integer.parseInt(ed2.getText().toString());
} catch (NumberFormatException e) {
n4.setText("Error: Invalid number format");
return;
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy