Program 2-1 - Merged
Program 2-1 - Merged
XML:
<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: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: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;
RadioGroup gender;
TextView toDisplayData;
@Override
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 -> {
return;
String g = ((RadioButton)
findViewById(gender.getCheckedRadioButtonId())).getText().toString();
"\nGender: " + g +
});
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;
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);
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)
});
}
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:
<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;
TextView result;
@Override
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('+'));
String s1 = num1.getText().toString().trim();
String s2 = num2.getText().toString().trim();
if (s1.isEmpty() || s2.isEmpty()) {
return;
double a = Double.parseDouble(s1);
double b = Double.parseDouble(s2);
double res = 0;
switch (op) {
case '/':
if (b == 0) {
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;
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);
double x=Double.parseDouble(a),y=Double.parseDouble(b),z=0;
r.setText(""+z);
}
Program 4:
Design an android application for creating login form
using table layout.
XML:
<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;
Button loginButton;
@Override
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();
} else {
});
}
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:
<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:
<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;
@Override
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);
if (submit != null) {
submit.setOnClickListener(v -> {
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;
@Override
super.onCreate(b);
setContentView(R.layout.activity_main2);
TextView tv = findViewById(R.id.tvDisplay);
Bundle i = getIntent().getExtras();
if (i != null) {
tv.setText(info);
}
}
Program 6 :
Design an android application to send SMS using
Intent
XML:
<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: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;
super.onCreate(b);
setContentView(R.layout.activity_main);
findViewById(R.id.sendSmsButton).setOnClickListener(v -> {
if (num.isEmpty() || msg.isEmpty()) {
return;
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:" +
num)).putExtra("sms_body", msg));
} catch (Exception e) {
});
}
Program 7:
Create an android application using fragments.
Activity_main.XML :
<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 :
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#F2EDED"
android:textSize="34sp"
android:textStyle="bold" />
</FrameLayout>
Fragment_two.xml :
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#0D0C0C"
android:textSize="34sp"
android:textStyle="bold" />
</FrameLayout>
Fragment_three.xml :
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
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;
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
loadFragment(new FragmentOne());
}
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;
@Override
Bundle savedInstanceState) {
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;
@Override
Bundle savedInstanceState) {
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;
@Override
Bundle savedInstanceState) {
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 ShapeView(Context c) {
super(c);
p.setColor(0xFF2196F3); p.setStyle(Paint.Style.FILL);
switch(i){
}
Program 9:
Develop a mobile application that create, save, update
and delete data in a database.
Activity_main.xml :
<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.*;
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);
int[] btns={R.id.addBtn,R.id.viewBtn,R.id.updateBtn,R.id.deleteBtn};
for(int j=0;j<btns.length;j++) {
findViewById(btns[k]).setOnClickListener(v->{
});
void v() {
}
Program 10 :
Create a user registration application that stores the
user details in a database table.
Xml :
<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);
}
}
}