0% found this document useful (0 votes)
4 views60 pages

Program 2-1 - Merged

The document outlines the creation of an Android application with multiple programs, including a signup form, a basic calculator, and a login form using XML layouts and Java code. Each program features user input fields and buttons, with functionality to display or process the entered data. The code examples provided demonstrate the implementation of user interactions and validations.

Uploaded by

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

Program 2-1 - Merged

The document outlines the creation of an Android application with multiple programs, including a signup form, a basic calculator, and a login form using XML layouts and Java code. Each program features user input fields and buttons, with functionality to display or process the entered data. The code examples provided demonstrate the implementation of user interactions and validations.

Uploaded by

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

Program 2:

Create a signup form that has input boxes for User


Name, Password, Address, Gender (radio buttons for
male and female), Age (numeric) and a submit button.
On clicking the submit button, print all the data below
the submit button (use any layout).

XML:

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

<androidx.appcompat.widget.LinearLayoutCompat
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:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:orientation="vertical"

android:padding="20dp">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Registration Form!"
app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

android:textSize="24sp"

android:layout_gravity="center"

android:layout_marginTop="30dp"/>

<EditText

android:id="@+id/username"

android:layout_width="match_parent"

android:layout_height="48dp"

android:ems="10"

android:inputType="text"

android:hint="Enter User Name"

android:padding="10dp"/>

<EditText

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="48dp"

android:ems="10"

android:inputType="textPassword"

android:hint="Enter Password"

android:padding="10dp"/>
<EditText

android:id="@+id/address"

android:layout_width="match_parent"

android:layout_height="100dp"

android:ems="10"

android:inputType="textMultiLine"

android:hint="Enter Postal Address"

android:maxLines="5"/>

<RadioGroup

android:id="@+id/gender"

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

<RadioButton

android:id="@+id/male"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Male" />

<RadioButton

android:id="@+id/female"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Female" />
</RadioGroup>

<EditText

android:id="@+id/age"

android:layout_width="wrap_content"

android:layout_height="48dp"

android:ems="10"

android:inputType="number"

android:hint="Enter Age"/>

<Button

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Submit" />

<TextView

android:id="@+id/toDisplayData"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="24sp"/>

</androidx.appcompat.widget.LinearLayoutCompat>
MainActivity.java

package com.example.labact2;

import android.os.Bundle;

import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText username, password, address, age;

RadioGroup gender;

TextView toDisplayData;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

username = findViewById(R.id.username);

password = findViewById(R.id.password);

address = findViewById(R.id.address);

age = findViewById(R.id.age);

gender = findViewById(R.id.gender);

toDisplayData = findViewById(R.id.toDisplayData);
findViewById(R.id.button).setOnClickListener(v -> {

if (anyEmpty(username, password, address, age) ||


gender.getCheckedRadioButtonId() == -1) {

Toast.makeText(this, "All fields are required", Toast.LENGTH_SHORT).show();

return;

String g = ((RadioButton)
findViewById(gender.getCheckedRadioButtonId())).getText().toString();

toDisplayData.setText("Username: " + username.getText() +

"\nPassword: " + password.getText() +

"\nAddress: " + address.getText() +

"\nGender: " + g +

"\nAge: " + age.getText());

});

private boolean anyEmpty(EditText... fields) {

for (EditText f : fields)

if (f.getText().toString().trim().isEmpty()) return true;

return false;

}
A very compact version of the same java code written above. But this very complex to
understand. (Not recommended for exams, Use it wisely)

package com.example.labact2;

import android.os.Bundle;

import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

public void onCreate(Bundle s) {

super.onCreate(s); setContentView(R.layout.activity_main);

EditText
u=findViewById(R.id.username),p=findViewById(R.id.password),a=findViewById(R.id.addr
ess),g=findViewById(R.id.age);

RadioGroup r=findViewById(R.id.gender); TextView


d=findViewById(R.id.toDisplayData);

findViewById(R.id.button).setOnClickListener(v->{

if(u.getText().toString().isEmpty()||p.getText().toString().isEmpty()||a.getText().toString()
.isEmpty()||g.getText().toString().isEmpty()||r.getCheckedRadioButtonId()==-1)

{Toast.makeText(this,"All fields are required",Toast.LENGTH_SHORT).show(); return;}

//Toast.makeText(this, "All fields are required", Toast.LENGTH_SHORT).show();

d.setText("Username: "+u.getText()+"\nPassword: "+p.getText()+"\nAddress:


"+a.getText()+"\nGender:
"+((RadioButton)findViewById(r.getCheckedRadioButtonId())).getText()+"\nAge:
"+g.getText());

});

}
Program 3:
Develop standard calculator application to perform
basic calculations like addition, subtraction,
multiplication and division using 2 EditText and 1
TextView for displaying result.
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:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="15dp"

tools:context=".MainActivity">

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:autofillHints="Calc"

android:text="@string/the_great_calculator_app"

android:textSize="34sp"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.023" />

<EditText

android:id="@+id/num1"

android:layout_width="match_parent"

android:layout_height="48dp"

android:layout_marginBottom="484dp"

android:ems="10"

android:hint="@string/enter_1st_number"

android:inputType="numberDecimal"

android:textAlignment="center"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/result" />

<EditText

android:id="@+id/num2"

android:layout_width="match_parent"

android:layout_height="48dp"

android:layout_marginBottom="444dp"

android:ems="10"
android:hint="@string/enter_2nd_number"

android:inputType="numberDecimal"

android:textAlignment="center"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent" />

<TextView

android:id="@+id/result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/result"

android:textAlignment="center"

android:textSize="34sp"

app:layout_constraintBottom_toTopOf="@+id/num2"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/textView2"

app:layout_constraintVertical_bias="0.315" />

<Button

android:id="@+id/btnSub"

android:layout_width="92dp"

android:layout_height="90dp"
android:layout_marginTop="31dp"

android:layout_marginBottom="300dp"

android:text="@string/sub"

android:textSize="40sp"

app:cornerRadius="5dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toStartOf="@+id/btnMul"

app:layout_constraintStart_toEndOf="@+id/btnAdd"

app:layout_constraintTop_toBottomOf="@+id/num1"

app:layout_constraintVertical_bias="1.0" />

<Button

android:id="@+id/btnDiv"

android:layout_width="92dp"

android:layout_height="90dp"

android:layout_marginTop="31dp"

android:layout_marginBottom="300dp"

android:text="@string/div"

android:textSize="40sp"

app:cornerRadius="5dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@+id/btnMul"

app:layout_constraintTop_toBottomOf="@+id/num1"

app:layout_constraintVertical_bias="1.0" />
<Button

android:id="@+id/btnAdd"

android:layout_width="92dp"

android:layout_height="90dp"

android:layout_marginTop="30dp"

android:layout_marginBottom="300dp"

android:text="@string/add"

android:textSize="40sp"

app:cornerRadius="5dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toStartOf="@+id/btnSub"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/num1"

app:layout_constraintVertical_bias="1.0" />

<Button

android:id="@+id/btnMul"

android:layout_width="92dp"

android:layout_height="90dp"

android:layout_marginTop="31dp"

android:layout_marginBottom="300dp"

android:text="@string/x"

android:textSize="40sp"

app:cornerRadius="5dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toStartOf="@+id/btnDiv"
app:layout_constraintStart_toEndOf="@+id/btnSub"

app:layout_constraintTop_toBottomOf="@+id/num1"

app:layout_constraintVertical_bias="1.0" />

<TextView

android:id="@+id/textView"

android:layout_width="105dp"

android:layout_height="96dp"

android:layout_marginStart="162dp"

android:layout_marginTop="88dp"

android:layout_marginEnd="176dp"

android:layout_marginBottom="208dp"

android:text="TextView"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/btnSub" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java

package com.example.lab3;

import android.os.Bundle;

import android.view.WindowManager;

import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText num1, num2;

TextView result;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

num1 = findViewById(R.id.num1);

num2 = findViewById(R.id.num2);

result = findViewById(R.id.result);

num1.requestFocus();

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_A
LWAYS_VISIBLE);
findViewById(R.id.btnAdd).setOnClickListener(v -> calculate('+'));

findViewById(R.id.btnSub).setOnClickListener(v -> calculate('-'));

findViewById(R.id.btnMul).setOnClickListener(v -> calculate('*'));

findViewById(R.id.btnDiv).setOnClickListener(v -> calculate('/'));

private void calculate(char op) {

String s1 = num1.getText().toString().trim();

String s2 = num2.getText().toString().trim();

if (s1.isEmpty() || s2.isEmpty()) {

Toast.makeText(this, "Please enter both numbers", Toast.LENGTH_SHORT).show();

return;

double a = Double.parseDouble(s1);

double b = Double.parseDouble(s2);

double res = 0;

switch (op) {

case '+': res = a + b; break;

case '-': res = a - b; break;

case '*': res = a * b; break;

case '/':

if (b == 0) {

Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();


return;

res = a / b;

break;

result.setText(String.valueOf(res));

}
A compact version of the same java code written above, But its complex to
understand. (Not recommended for exams)

package com.example.lab3;

import android.os.Bundle;

import android.view.WindowManager;

import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText n1,n2; TextView r;

public void onCreate(Bundle b) {

super.onCreate(b); setContentView(R.layout.activity_main);

n1 = findViewById(R.id.num1); n2 = findViewById(R.id.num2); r =
findViewById(R.id.result);

n1.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALW
AYS_VISIBLE);

findViewById(R.id.btnAdd).setOnClickListener(v -> c('+'));

findViewById(R.id.btnSub).setOnClickListener(v -> c('-'));

findViewById(R.id.btnMul).setOnClickListener(v -> c('*'));

findViewById(R.id.btnDiv).setOnClickListener(v -> c('/'));

void c(char o){


String a=n1.getText().toString(),b=n2.getText().toString();

if(a.isEmpty()||b.isEmpty()){Toast.makeText(this,"Please enter both


numbers",Toast.LENGTH_SHORT).show();return;}

double x=Double.parseDouble(a),y=Double.parseDouble(b),z=0;

if(o=='+')z=x+y;else if(o=='-')z=x-y;else if(o=='*')z=x*y;

else if(o=='/'){if(y==0){Toast.makeText(this,"Cannot divide by


zero",Toast.LENGTH_SHORT).show();return;}z=x/y;}

r.setText(""+z);

}
Program 4:
Design an android application for creating login form
using table layout.
XML:

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

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:stretchColumns="1"

android:paddingTop="100dp"

>

<TableRow>

<TextView

android:text="Username:"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="8dp"/>

<EditText

android:id="@+id/editTextUsername"

android:layout_width="match_parent"

android:layout_height="48dp"

android:hint="Enter Username"

android:inputType="textPersonName"/>

</TableRow>
<TableRow>

<TextView

android:text="Password:"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="8dp"/>

<EditText

android:id="@+id/editTextPassword"

android:layout_width="match_parent"

android:layout_height="48dp"

android:hint="Enter Password"

android:inputType="textPassword"/>

</TableRow>

<TableRow>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<Button

android:id="@+id/buttonLogin"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Login"/>

</TableRow>
</TableLayout>

MainActivity.Java

package com.example.prog4;

import android.os.Bundle;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText usernameEditText, passwordEditText;

Button loginButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

usernameEditText = findViewById(R.id.editTextUsername);

passwordEditText = findViewById(R.id.editTextPassword);

loginButton = findViewById(R.id.buttonLogin);

loginButton.setOnClickListener(view -> {
String username = usernameEditText.getText().toString();

String password = passwordEditText.getText().toString();

if (username.equals("admin") && password.equals("admin")) {

Toast.makeText(MainActivity.this, "Login Successful",


Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(MainActivity.this, "Invalid Credentials",


Toast.LENGTH_SHORT).show();

});

}
Program 5:
Develop an android application to register a form in
first activity and display the registered information in
second activity using intents.
Activity_main.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:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:id="@+id/registeration_form"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="28dp"

android:layout_marginBottom="68dp"

android:text="Registration Form"

android:textSize="20dp"

android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.19" />

<EditText

android:id="@+id/etName"

android:layout_width="0dp"

android:layout_height="48dp"

android:hint="Enter Name"

android:padding="10dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@id/registeration_form"

app:layout_constraintVertical_bias="0.050000012"

android:inputType="text"

/>

<EditText

android:id="@+id/etEmail"

android:layout_width="0dp"

android:layout_height="48dp"

android:hint="Enter Email"

android:inputType="textEmailAddress"
app:layout_constraintTop_toBottomOf="@id/etName"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:padding="10dp"/>

<EditText

android:id="@+id/etPhone"

android:layout_width="0dp"

android:layout_height="48dp"

android:hint="Enter Phone"

android:inputType="phone"

app:layout_constraintTop_toBottomOf="@id/etEmail"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:padding="10dp" />

<EditText

android:id="@+id/etAge"

android:layout_width="0dp"

android:layout_height="48dp"

android:hint="Age"

android:inputType="number"

app:layout_constraintTop_toBottomOf="@id/etPhone"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:padding="10dp"/>
<EditText

android:id="@+id/etGender"

android:layout_width="0dp"

android:layout_height="48dp"

android:hint="Enter Gender"

app:layout_constraintTop_toBottomOf="@id/etAge"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:padding="10dp" />

<Button

android:id="@+id/btnSubmit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintTop_toBottomOf="@id/etGender"

app:layout_constraintVertical_bias="0.120000005"

android:text="Button" />

</androidx.constraintlayout.widget.ConstraintLayout>
Activity_main2.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:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity2">

<TextView

android:id="@+id/tvDisplay"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:textSize="24sp"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:padding="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:

package com.example.pro5;

import android.content.Intent;

import android.os.Bundle;

import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText name, email, phone, age, gender;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

name = findViewById(R.id.etName);

email = findViewById(R.id.etEmail);

phone = findViewById(R.id.etPhone);

age = findViewById(R.id.etAge);

gender = findViewById(R.id.etGender);

Button submit = findViewById(R.id.btnSubmit);

if (submit != null) {
submit.setOnClickListener(v -> {

Intent i = new Intent(this, MainActivity2.class);

i.putExtra("NAME", name.getText().toString());

i.putExtra("EMAIL", email.getText().toString());

i.putExtra("PHONE", phone.getText().toString());

i.putExtra("AGE", age.getText().toString());

i.putExtra("GENDER", gender.getText().toString());

startActivity(i);

});

}
MainActivity2.java:
package com.example.pro5;

import android.os.Bundle;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

@Override

protected void onCreate(Bundle b) {

super.onCreate(b);

setContentView(R.layout.activity_main2);

TextView tv = findViewById(R.id.tvDisplay);

Bundle i = getIntent().getExtras();

if (i != null) {

String info = "Name: " + i.getString("NAME") +

"\nEmail: " + i.getString("EMAIL") +

"\nPhone: " + i.getString("PHONE") +

"\nAge: " + i.getString("AGE") +

"\nGender: " + i.getString("GENDER");

tv.setText(info);

}
}
Program 6 :
Design an android application to send SMS using
Intent
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:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!"

app:layout_constraintBottom_toTopOf="@+id/phoneNumber"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.65999997" />
<EditText

android:id="@+id/phoneNumber"

android:layout_width="match_parent"

android:layout_height="48dp"

android:layout_marginTop="56dp"

android:hint="Enter Phone Number"

android:inputType="phone"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText

android:id="@+id/messageText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="35dp"

android:layout_marginBottom="486dp"

android:gravity="top"

android:hint="Enter Message"

android:inputType="textMultiLine"

android:lines="3"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/phoneNumber" />
<Button

android:id="@+id/sendSmsButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="16dp"

android:layout_marginTop="50dp"

android:layout_marginEnd="282dp"

android:layout_marginBottom="388dp"

android:text="Send SMS"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/messageText" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:

package com.example.lab61;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle b) {

super.onCreate(b);

setContentView(R.layout.activity_main);

EditText number = findViewById(R.id.phoneNumber);

EditText message = findViewById(R.id.messageText);

findViewById(R.id.sendSmsButton).setOnClickListener(v -> {

String num = number.getText().toString().trim();

String msg = message.getText().toString().trim();

if (num.isEmpty() || msg.isEmpty()) {

Toast.makeText(this, "Enter number and message", Toast.LENGTH_SHORT).show();

return;

try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:" +
num)).putExtra("sms_body", msg));

} catch (Exception e) {

Toast.makeText(this, "No SMS app found", Toast.LENGTH_SHORT).show();

});

}
Program 7:
Create an android application using fragments.

Activity_main.XML :

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

<LinearLayout 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:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:orientation="vertical">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:padding="5dp">

<Button

android:id="@+id/btnFragment1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"
android:padding="5dp"

android:text="Frag ONE"

app:cornerRadius="5dp" />

<Button

android:id="@+id/btnFragment2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:padding="5dp"

android:text="Frag TWO"

app:cornerRadius="5dp" />

<Button

android:id="@+id/btnFragment3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:padding="5dp"

android:text="Frag THREE"

app:cornerRadius="5dp" />

</LinearLayout>

<FrameLayout

android:id="@+id/fragmentContainer"

android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
Fragment_one.xml :

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

<FrameLayout 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"

android:layout_gravity="center"

android:background="#EF0808"

tools:context=".FragmentOne">

<!-- TODO: Update blank fragment layout -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:gravity="center"

android:text="Welcome to Fragment ONE!"

android:textColor="#F2EDED"

android:textSize="34sp"

android:textStyle="bold" />

</FrameLayout>
Fragment_two.xml :

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

<FrameLayout 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"

android:layout_gravity="center"

android:background="#2BEF08"

tools:context=".FragmentTwo">

<!-- TODO: Update blank fragment layout -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:gravity="center"

android:text="Welcome to Fragment TWO!"

android:textColor="#0D0C0C"

android:textSize="34sp"

android:textStyle="bold" />

</FrameLayout>
Fragment_three.xml :

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

<FrameLayout 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"

android:layout_gravity="center"

android:background="#0832EF"

tools:context=".FragmentThree">

<!-- TODO: Update blank fragment layout -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:gravity="center"

android:text="Welcome to Fragment THREE!"

android:textColor="#0D0C0C"

android:textSize="34sp"

android:textStyle="bold" />

</FrameLayout>
mainActivity.java :

package com.example.fragments;

import android.os.Bundle;

import android.widget.Button;

import androidx.activity.EdgeToEdge;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

EdgeToEdge.enable(this);

setContentView(R.layout.activity_main);

findViewById(R.id.btnFragment1).setOnClickListener(v -> loadFragment(new


FragmentOne()));

findViewById(R.id.btnFragment2).setOnClickListener(v -> loadFragment(new


FragmentTwo()));

findViewById(R.id.btnFragment3).setOnClickListener(v -> loadFragment(new


FragmentThree()));

loadFragment(new FragmentOne());

private void loadFragment(Fragment f) {


getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,
f).commit();

}
fragmentOne.java :

package com.example.fragments;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class FragmentOne extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_one, container, false);

view.setBackgroundColor(0xFFEF0808); //Red

return view;

}
FragmentTwo.java :

package com.example.fragments;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class FragmentTwo extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_two, container, false);

view.setBackgroundColor(0xFF4CAF50); //Green

return view;

}
FragmentThree.java :

package com.example.fragments;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class FragmentThree extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_three, container, false);

view.setBackgroundColor(0xFF2196F3); //Blue

return view;

}
Program 8 :
Design an application that draws basic graphical
shapes (rectangle, circle)
No XML for this project :

MainActivity.java :

package com.example.lab8drawing;

import android.content.Context;

import android.graphics.*;

import android.os.Bundle;

import android.view.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle b) { super.onCreate(b); setContentView(new


ShapeView(this)); }

static class ShapeView extends View {

Paint p = new Paint(Paint.ANTI_ALIAS_FLAG), t = new Paint(Paint.ANTI_ALIAS_FLAG), h


= new Paint(Paint.ANTI_ALIAS_FLAG);

int i = 0; String[] s = {"Circle", "Square", "Rectangle", "Triangle"};

public ShapeView(Context c) {

super(c);
p.setColor(0xFF2196F3); p.setStyle(Paint.Style.FILL);

t.setColor(Color.WHITE); t.setTextSize(100); t.setTextAlign(Paint.Align.CENTER);

h.setColor(Color.WHITE); h.setTextSize(150); h.setTextAlign(Paint.Align.CENTER);


h.setFakeBoldText(true);

@Override protected void onDraw(Canvas c) {

int w = getWidth(), hgt = getHeight(), sz = Math.min(w,hgt)/3, cx=w/2, cy=hgt/2;

c.drawText("- S H A P E S -", cx, 400, h);

switch(i){

case 0: c.drawCircle(cx, cy, sz, p); break;

case 1: c.drawRect(cx-sz, cy-sz, cx+sz, cy+sz, p); break;

case 2: c.drawRect(w/4,hgt/3,3*w/4,2*hgt/3,p); break;

default: Path path = new Path(); path.moveTo(cx,hgt/3); path.lineTo(w/3,2*hgt/3);


path.lineTo(2*w/3,2*hgt/3); path.close(); c.drawPath(path,p);

c.drawText(s[i], cx, hgt - 600, t);

@Override public boolean onTouchEvent(MotionEvent e) {


if(e.getAction()==MotionEvent.ACTION_DOWN){i=(i+1)%4; invalidate();} return true;}

}
Program 9:
Develop a mobile application that create, save, update
and delete data in a database.
Activity_main.xml :

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="8dp">

<ScrollView

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1">

<TextView

android:id="@+id/taskList"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Tasks"

android:padding="4dp" />

</ScrollView>
<EditText

android:id="@+id/idInput"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="ID"

android:inputType="number" />

<EditText

android:id="@+id/taskInput"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Task" />

<EditText

android:id="@+id/notesInput"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Notes" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<ImageButton

android:id="@+id/addBtn"
android:layout_width="48dp"

android:layout_height="48dp"

android:src="@android:drawable/ic_input_add" />

<ImageButton

android:id="@+id/viewBtn"

android:layout_width="48dp"

android:layout_height="48dp"

android:src="@android:drawable/ic_menu_view" />

<ImageButton

android:id="@+id/updateBtn"

android:layout_width="48dp"

android:layout_height="48dp"

android:src="@android:drawable/ic_menu_edit" />

<ImageButton

android:id="@+id/deleteBtn"

android:layout_width="48dp"

android:layout_height="48dp"

android:src="@android:drawable/ic_menu_delete" />

</LinearLayout>

</LinearLayout>
MainActivity.java :

package com.example.ninetodoapp;

import android.content.*;

import android.database.*;

import android.database.sqlite.*;

import android.os.*;

import android.widget.*;

import androidx.appcompat.app.*;

public class MainActivity extends AppCompatActivity {

SQLiteDatabase d; EditText i,t,n; TextView o;

protected void onCreate(Bundle b) {

super.onCreate(b); setContentView(R.layout.activity_main);

i=findViewById(R.id.idInput); t=findViewById(R.id.taskInput);
n=findViewById(R.id.notesInput); o=findViewById(R.id.taskList);

d=openOrCreateDatabase("DB",MODE_PRIVATE,null); d.execSQL("CREATE TABLE IF


NOT EXISTS t(id INTEGER PRIMARY KEY,t TEXT,n TEXT)"); v();

int[] btns={R.id.addBtn,R.id.viewBtn,R.id.updateBtn,R.id.deleteBtn};

for(int j=0;j<btns.length;j++) {

final int k=j;

findViewById(btns[k]).setOnClickListener(v->{

ContentValues c=new ContentValues();

if(k==0){ c.put("t",t.getText().toString()); c.put("n",n.getText().toString());


d.insert("t",null,c); s("Added"); }

else if(k==1) v();


else if(k==2){ c.put("t",t.getText().toString()); c.put("n",n.getText().toString());
d.update("t",c,"id=?",new String[]{i.getText().toString()}); s("Updated"); }

else { d.delete("t","id=?",new String[]{i.getText().toString()}); s("Deleted"); }

if(k!=1) { i.setText(""); t.setText(""); n.setText(""); v(); }

});

void v() {

Cursor c=d.rawQuery("SELECT * FROM t",null);

StringBuilder s=new StringBuilder();

if(c.moveToFirst()) do s.append("ID: ").append(c.getInt(0)).append(", Task:


").append(c.getString(1)).append(", Notes: ").append(c.getString(2)).append("\n");
while(c.moveToNext());

else s.append("No tasks"); c.close(); o.setText(s);

void s(String m) { Toast.makeText(this,"Note "+m,Toast.LENGTH_SHORT).show(); }

}
Program 10 :
Create a user registration application that stores the
user details in a database table.
Xml :

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />

<EditText
android:id="@+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />

<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />

</LinearLayout>
MainActivity.java :

package com.example.tenprog;
import android.content.*;
import android.database.sqlite.*;
import android.os.*;
import android.widget.*;
import androidx.appcompat.app.*;
public class MainActivity extends AppCompatActivity {
EditText n,e,p;SQLiteDatabase d;
protected void onCreate(Bundle
b){super.onCreate(b);setContentView(R.layout.activity_main);
n=findViewById(R.id.editName); e=findViewById(R.id.editEmail);
p=findViewById(R.id.editPassword);
d=new DbHelper(this).getWritableDatabase();
findViewById(R.id.btnRegister).setOnClickListener(v->r());
}
void r(){
String N=n.getText().toString(),E=e.getText().toString(),P=p.getText().toString();
if(N.isEmpty()||E.isEmpty()||P.isEmpty()){Toast.makeText(this,"Fill all
fields",Toast.LENGTH_SHORT).show();return;}
ContentValues v=new
ContentValues();v.put("name",N);v.put("email",E);v.put("password",P);
if(d.insert("users",null,v)!=-
1){Toast.makeText(this,"Success",Toast.LENGTH_SHORT).show();n.setText("");e.setText("");
p.setText("");}
else Toast.makeText(this,"Failed",Toast.LENGTH_SHORT).show();
}
class DbHelper extends SQLiteOpenHelper{
DbHelper(Context c){super(c,"userdb",null,1);
}
public void onCreate(SQLiteDatabase d){
d.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY,name TEXT,email
TEXT,password TEXT)");
}
public void onUpgrade(SQLiteDatabase d,int o,int n){
d.execSQL("DROP TABLE IF EXISTS users");onCreate(d);
}
}
}

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