Lab Cycle: 01 Date:
EXPERIMENT NO: 01
AIM: Design a Login Form with username and password using Linear Layout and toast valid
credentials.
Procedure:
[Link]
package [Link].s3mca55;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private static final String VALID_USR_NAME = "user";
private static final String VALID_PWD = "pass";
private EditText username;
private EditText password;
private Button loginbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
username = findViewById([Link]);
password = findViewById([Link]);
loginbtn = findViewById([Link]);
[Link](view -> {
String enteredUname = [Link]().toString().trim();
1
String enteredPwd = [Link]().toString().trim();
if ([Link]() || [Link]()) {
showToast("Please enter both username and password");
} else if (isValid(enteredUname, enteredPwd)) {
showToast("Login Success");
} else {
showToast("Invalid credentials");
}
});
}
public boolean isValid(String euname, String epwd) {
return VALID_USR_NAME.equals(euname) && VALID_PWD.equals(epwd);
}
private void showToast(String msg) {
[Link](this, msg, Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
2
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
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.0">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN FORM"
android:textAlignment="center"
android:textSize="24sp"
android:padding="16dp" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:padding="16dp" />
<EditText
android:id="@+id/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter username"
android:inputType="text"
android:padding="16dp" />
3
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password"
android:padding="16dp" />
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter password"
android:padding="16dp" />
<Button
android:id="@+id/lbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:padding="16dp" />
</LinearLayout>
</[Link]>
4
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
5
Lab Cycle: 01 Date:
EXPERIMENT NO: 02
AIM: Implementing basic arithmetic operations of a simple calculator.
Procedure:
[Link]
package [Link].s3mca55;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private TextView textView1;
private Button button1, button2, button3, button4, button5, button6, button7, button8, button9,
button0;
private Button buttonAdd, buttonSub, buttonMul, buttonDiv, buttonDot, buttonEqual;
private String currentInput = "";
private double operand1 = 0;
private String operator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
textView1 = findViewById([Link].text_View1);
}
6
public void onDigitClick(View view) {
Button button = (Button) view;
currentInput += [Link]().toString();
updateDisplay();
}
public void onOperatorClick(View view) {
if (![Link]()) {
operand1 = [Link](currentInput);
operator = ((Button) view).getText().toString();
currentInput = "";
}
}
public void onEqualsClick(View view) {
if (![Link]()) {
double operand2 = [Link](currentInput);
double result = performOperation(operand1, operand2, operator);
currentInput = [Link](result);
updateDisplay();
}
}
public void onClearClick(View view) {
currentInput = "";
operand1 = 0;
operator = "";
updateDisplay();
}
private double performOperation(double operand1, double operand2, String operator) {
7
switch (operator) {
case "+":
return operand1 + operand2;
case "-":
return operand1 - operand2;
case "*":
return operand1 * operand2;
case "/":
if (operand2 != 0)
return operand1 / operand2;
else
return [Link];
default:
return 0;
}
}
public void updateDisplay() {
[Link](currentInput);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
8
<LinearLayout
android:layout_width="409dp"
android:layout_height="601dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/text_View"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SIMPLE CALCULATOR"
android:textSize="24sp" />
<TextView
android:id="@+id/text_View1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:columnCount="4"
android:rowCount="4">
<Button
9
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="3" />
<Button
android:id="@+id/buttonDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onOperatorClick"
android:text="/" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
10
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="6" />
<Button
android:id="@+id/buttonMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onOperatorClick"
android:text="*" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="7" />
<Button
android:id="@+id/button8"
11
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="9" />
<Button
android:id="@+id/buttonSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onOperatorClick"
android:text="-" />
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDigitClick"
android:text="0" />
<Button
android:id="@+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClearClick"
android:text="C" />
12
<Button
android:id="@+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onEqualsClick"
android:text="=" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onOperatorClick"
android:text="+" />
</GridLayout>
</LinearLayout>
</[Link]>
13
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
14
Lab Cycle: 01 Date:
EXPERIMENT NO: 03
AIM: Write a program that demonstrates Activity Lifecycle
Procedure:
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
showToast("Activity Created");
// Set up onClick listeners for each button
Button onCreateButton = findViewById([Link]);
Button onStartButton = findViewById([Link]);
Button onPauseButton = findViewById([Link]);
Button onStopButton = findViewById([Link]);
Button onRestartButton = findViewById([Link]);
Button onDestroyButton = findViewById([Link]);
[Link](v -> showToast("onCreate() Clicked"));
15
[Link](v -> showToast("onStart() Clicked"));
[Link](v -> showToast("onPause() Clicked"));
[Link](v -> showToast("onStop() Clicked"));
[Link](v -> showToast("onRestart() Clicked"));
[Link](v -> showToast("onDestroy() Clicked"));
}
@Override
protected void onStart() {
[Link]();
showToast("Activity Started");
}
@Override
protected void onResume() {
[Link]();
showToast("Activity Resumed");
}
@Override
protected void onPause() {
[Link]();
showToast("Activity Paused");
}
@Override
protected void onStop() {
[Link]();
showToast("Activity Stopped");
}
@Override
16
protected void onRestart() {
[Link]();
showToast("Activity Restarted");
}
@Override
protected void onDestroy() {
[Link]();
showToast("Activity Destroyed");
}
void showToast(String message) {
[Link](this, message, Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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="@string/activity_lifecycle"
android:textAlignment="center"
android:textSize="30sp"
android:layout_marginTop="50dp"
17
android:id="@+id/activityLifecycleText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/onCreateButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/onCreate"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/activityLifecycleText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/onStartButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/onStart"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/onCreateButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/onPauseButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/onPause"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/onStartButton"
18
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/onStopButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/onStop"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/onPauseButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/onRestartButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/onRestart"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/onStopButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/onDestroyButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/onDestroy"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/onRestartButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
19
/>
</[Link]>
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
20
Lab Cycle: 01 Date:
EXPERIMENT NO: 04
AIM: Implement validations on various UI controls
Procedure:
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button constraintButton = findViewById([Link]);
Button linearButton = findViewById([Link]);
Button gridButton = findViewById([Link]);
Button relativeButton = findViewById([Link]);
Button frameButton = findViewById([Link]);
Button tableButton = findViewById([Link]);
[Link] buttonClickListener = new [Link]() {
@Override
public void onClick(View v) {
String layoutName = ((Button) v).getText().toString();
21
displayToken(layoutName);
}
};
[Link](buttonClickListener);
[Link](buttonClickListener);
[Link](buttonClickListener);
[Link](buttonClickListener);
[Link](buttonClickListener);
[Link](buttonClickListener);
}
private void displayToken(String layoutName) {
[Link](this, "Token from " + layoutName, Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/constraintButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ConstraintLayout" />
22
<Button
android:id="@+id/linearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinearLayout" />
<Button
android:id="@+id/gridButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GridLayout" />
<Button
android:id="@+id/relativeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RelativeLayout" />
<Button
android:id="@+id/frameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FrameLayout" />
<Button
android:id="@+id/tableButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TableLayout" />
</LinearLayout>
23
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
24