0% found this document useful (0 votes)
21 views58 pages

MAD Manual

The document outlines practical assignments for a B.E. 4th year 7th semester course, focusing on designing a login activity using various layouts in Android. It includes XML code for different layouts such as Linear, Relative, and Table layouts, as well as the AndroidManifest.xml configuration. Additionally, it introduces the implementation of implicit and explicit intents in Android development.
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)
21 views58 pages

MAD Manual

The document outlines practical assignments for a B.E. 4th year 7th semester course, focusing on designing a login activity using various layouts in Android. It includes XML code for different layouts such as Linear, Relative, and Table layouts, as well as the AndroidManifest.xml configuration. Additionally, it introduces the implementation of implicit and explicit intents in Android development.
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/ 58

B.E.

4th YEAR 7th SEM EN: 210330107015

Practical 1

Aim: Design Login activity and implement control events:


Use Edit Text, Checkbox, and Buttons.

Input

A. 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"
android:background="@drawable/background"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:textSize="34sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/Username"
android:layout_marginStart="16dp" />

<EditText
android:id="@+id/Username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Your UserName"
android:inputType="textPersonName"
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />

<EditText
android:id="@+id/Password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"

MGITER / CE / MAD / 2024 pg. 1


B.E. 4th YEAR 7th SEM EN: 210330107015

android:hint="Enter Your Password"


android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@id/Username"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me"
app:layout_constraintTop_toBottomOf="@id/Password"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp" />

<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintTop_toBottomOf="@id/checkBox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

B. androidmanifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

MGITER / CE / MAD / 2024 pg. 2


B.E. 4th YEAR 7th SEM EN: 210330107015

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Output

MGITER / CE / MAD / 2024 pg. 3


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 2

Aim: Implement Practical 1 Using layouts:


1. Linear Layout, 2. Relative Layout, 3. Table Layout.

1.Linear Layout:

Input

A. 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"
android:background="@drawable/background"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login / Sign up"
android:textSize="34sp"
android:textColor="@color/white"
android:textStyle="bold"/>

<EditText
android:id="@+id/Username"

MGITER / CE / MAD / 2024 pg. 4


B.E. 4th YEAR 7th SEM EN: 210330107015

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Your UserName"
android:textColorHint="@color/white"
android:inputType="textPersonName" />

<EditText
android:id="@+id/Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Your Password"
android:textColorHint="@color/white"
android:inputType="textPassword" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me"
android:textColor="@color/white"/>

<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"/>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

B. AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"

MGITER / CE / MAD / 2024 pg. 5


B.E. 4th YEAR 7th SEM EN: 210330107015

android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Output

MGITER / CE / MAD / 2024 pg. 6


B.E. 4th YEAR 7th SEM EN: 210330107015

2. Relative Layout:

Input

A. 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"
android:background="@drawable/background"
tools:context=".MainActivity">

<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20sp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login / Sign up"
android:textColor="@color/white"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="34sp" />

<EditText
android:id="@+id/Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:ems="10"
android:hint="Enter Your UserName"
android:textColorHint="@color/white"
MGITER / CE / MAD / 2024 pg. 7
B.E. 4th YEAR 7th SEM EN: 210330107015

android:inputType="textPersonName" />

<EditText
android:id="@+id/Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Username"
android:ems="10"
android:hint="Enter Your Password"
android:textColorHint="@color/white"
android:inputType="textPassword" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Password"
android:text="Remember Me"
android:textColor="@color/white"/>

<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/checkBox"
android:text="Login"
android:textSize="20sp"/>

</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MGITER / CE / MAD / 2024 pg. 8


B.E. 4th YEAR 7th SEM EN: 210330107015

Output

3. Table Layout:
Input

A. 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"
android:background="@drawable/background"
tools:context=".MainActivity">

<TextView

MGITER / CE / MAD / 2024 pg. 9


B.E. 4th YEAR 7th SEM EN: 210330107015

android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login / Sign up"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textSize="34sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/Username"
android:layout_marginBottom="32dp" />

<!-- Username EditText -->


<EditText
android:id="@+id/Username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter Your UserName"
android:inputType="textPersonName"
android:textColorHint="@android:color/white"
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="16dp"/>

<!-- Password EditText -->


<EditText
android:id="@+id/Password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter Your Password"
android:textColorHint="@android:color/white"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@id/Username"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="16dp"/>

<!-- Remember Me CheckBox -->


<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"

MGITER / CE / MAD / 2024 pg. 10


B.E. 4th YEAR 7th SEM EN: 210330107015

android:layout_height="wrap_content"
android:text="Remember Me"
android:textColor="@android:color/white"
app:layout_constraintTop_toBottomOf="@id/Password"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"/>

<!-- Login Button -->


<Button
android:id="@+id/login_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/checkBox"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="16dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Output

MGITER / CE / MAD / 2024 pg. 11


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 3

Aim: Create Activities & Implement following


1. Implicit Intent, 2. Explicit Intent 3. Start Activity for Result.

1. Implicit Intent

Input

A. 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"
android:background="@drawable/background_phone"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="https://www.exmaple.com"
android:textAlignment="center"
android:textColorHint="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn"
android:text="Search"
android:onClick="search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

MGITER / CE / MAD / 2024 pg. 12


B.E. 4th YEAR 7th SEM EN: 210330107015

B. MainActivity.java

package com.example.myapp2;

import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

EditText editText;
Button button;

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

button = findViewById(R.id.btn);

editText = (EditText) findViewById(R.id.editText);


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=editText.getText().toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}

MGITER / CE / MAD / 2024 pg. 13


B.E. 4th YEAR 7th SEM EN: 210330107015

Output

MGITER / CE / MAD / 2024 pg. 14


B.E. 4th YEAR 7th SEM EN: 210330107015

2. Explicit Intent

Input

A. 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"
android:background="@drawable/background_phone"
tools:context=".MainActivity">

<TextView
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Home Screen"
android:textAlignment="center"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn1"
android:text="Go to News Screen"
android:onClick="newsScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

MGITER / CE / MAD / 2024 pg. 15


B.E. 4th YEAR 7th SEM EN: 210330107015

B. MainActivity.java

package com.example.myapp2;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void newsScreen(View view) {


Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
}
}

C. 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/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to News Screen"
android:textAlignment="center"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"

MGITER / CE / MAD / 2024 pg. 16


B.E. 4th YEAR 7th SEM EN: 210330107015

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn2"
android:text="Go to Home Screen"
android:onClick="homeScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

D. MainActivity2.java

package com.example.myapp2;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void homeScreen(View view) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}

MGITER / CE / MAD / 2024 pg. 17


B.E. 4th YEAR 7th SEM EN: 210330107015

Output

MGITER / CE / MAD / 2024 pg. 18


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 4

Aim: Implement Activity Lifecycle and State Callbacks.

Input

A. 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"
android:background="@drawable/background"
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>

B. MainActivity.java

package com.example.myapp4;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MGITER / CE / MAD / 2024 pg. 19
B.E. 4th YEAR 7th SEM EN: 210330107015

setContentView(R.layout.activity_main);
Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called",
Toast.LENGTH_LONG);
toast.show();
}

protected void onStart() {


super.onStart();
Toast toast = Toast.makeText(getApplicationContext(), "onStart Called",
Toast.LENGTH_LONG);
toast.show();
}

@Override
protected void onRestart() {
super.onRestart();
Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called",
Toast.LENGTH_LONG);
toast.show();
}

protected void onPause() {


super.onPause();
Toast toast = Toast.makeText(getApplicationContext(), "onPause Called",
Toast.LENGTH_LONG);
toast.show();
}

protected void onResume() {


super.onResume();
Toast toast = Toast.makeText(getApplicationContext(), "onResume Called",
Toast.LENGTH_LONG);
toast.show();
}

protected void onStop() {


super.onStop();
Toast toast = Toast.makeText(getApplicationContext(), "onStop Called",
Toast.LENGTH_LONG);
toast.show();
}

protected void onDestroy() {


super.onDestroy();

MGITER / CE / MAD / 2024 pg. 20


B.E. 4th YEAR 7th SEM EN: 210330107015

Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called",


Toast.LENGTH_LONG);
toast.show();
}
}

Output

MGITER / CE / MAD / 2024 pg. 21


B.E. 4th YEAR 7th SEM EN: 210330107015

MGITER / CE / MAD / 2024 pg. 22


B.E. 4th YEAR 7th SEM EN: 210330107015

MGITER / CE / MAD / 2024 pg. 23


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 5

Aim: Use an Options Menu.

Input

A. activity_main.xml

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


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

<item
android:id="@+id/itemOne"
android:title="Item One"/>

<item
android:id="@+id/itemtwo"
android:title="Item Two"/>

<item
android:id="@+id/itemthree"
android:title="More Options">

<menu>
<group>
<item
android:id="@+id/subMenuOne"
android:title="Sub Menu Item One"/>

<item
android:id="@+id/subMenuTwo"
android:title="Sub Menu Item Two"/>
</group>
</menu>
</item>
</menu>

B. MainActivity.kt

package com.example.myapp5

import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

MGITER / CE / MAD / 2024 pg. 24


B.E. 4th YEAR 7th SEM EN: 210330107015

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {


val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.menu, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {


return when(item.itemId){
R.id.itemOne -> {
Toast.makeText( this,"Item One has been selected",
Toast.LENGTH_SHORT).show()
true
}

R.id.itemtwo -> {
Toast.makeText( this,"Item Two has been selected",
Toast.LENGTH_SHORT).show()
true
}

R.id.subMenuOne -> {
Toast.makeText( this,"Sub Menu One has been selected",
Toast.LENGTH_SHORT).show()
true
}

R.id.subMenuTwo -> {
Toast.makeText( this,"Sub Menu Two has been selected",
Toast.LENGTH_SHORT).show()
true
}
else -> super.onOptionsItemSelected(item)
}
}
}

MGITER / CE / MAD / 2024 pg. 25


B.E. 4th YEAR 7th SEM EN: 210330107015

Output

MGITER / CE / MAD / 2024 pg. 26


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 6

Aim: Use Firebase to perform CRUID operation.

Input

A. 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"
android:background="@color/blue_A700"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to our app!"
android:textColor="@color/white"
android:textStyle="bold|italic"
android:textSize="36dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

B. MainActivity.java

package com.example.signup;

import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

MGITER / CE / MAD / 2024 pg. 27


B.E. 4th YEAR 7th SEM EN: 210330107015

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) ->
{
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}

C. activity_main2.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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity2">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
app:cardCornerRadius="30dp"
app:cardElevation="20dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:padding="24dp"
android:background="@drawable/lavender_border">

MGITER / CE / MAD / 2024 pg. 28


B.E. 4th YEAR 7th SEM EN: 210330107015

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign Up"
android:textSize="36sp"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@color/lavender"/>

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/signup_name"
android:background="@drawable/lavender_border"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Name"
android:drawableLeft="@drawable/ic_baseline_person_24"
android:drawablePadding="8dp"
android:textColor="@color/black"/>

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/signup_email"
android:background="@drawable/lavender_border"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Email-Id"
android:drawableLeft="@drawable/ic_baseline_email_24"
android:drawablePadding="8dp"
android:textColor="@color/black"/>

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/signup_username"
android:background="@drawable/lavender_border"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Username"
android:drawableLeft="@drawable/ic_baseline_person_pin_24"
android:drawablePadding="8dp"
android:textColor="@color/black"/>

MGITER / CE / MAD / 2024 pg. 29


B.E. 4th YEAR 7th SEM EN: 210330107015

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/signup_password"
android:background="@drawable/lavender_border"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Password"
android:inputType="textPassword"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:drawablePadding="8dp"
android:textColor="@color/black"/>

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="SIGN UP"
android:id="@+id/signup_button"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:shadowRadius="20dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loginRedirectText"
android:text="Already an user? Login"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:textColor="@color/lavender"/>

</LinearLayout>
</LinearLayout>

D. MainActivity2.java

package com.example.signup;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

MGITER / CE / MAD / 2024 pg. 30


B.E. 4th YEAR 7th SEM EN: 210330107015

import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity2 extends AppCompatActivity {

EditText signupName, signupEmail, signupUsername, signupPassword;


TextView loginRedirectText;
Button signupButton;
FirebaseDatabase database;
DatabaseReference reference;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main2);

signupName = findViewById(R.id.signup_name);
signupEmail = findViewById(R.id.signup_email);
signupUsername = findViewById(R.id.signup_username);
signupPassword = findViewById(R.id.signup_password);
signupButton = findViewById(R.id.signup_button);
loginRedirectText = findViewById(R.id.loginRedirectText);

signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

database = FirebaseDatabase.getInstance();
reference = database.getReference("users");

String name = signupName.getText().toString();


String email = signupEmail.getText().toString();
String username = signupUsername.getText().toString();
String password = signupPassword.getText().toString();

HelperClass helperClass = new HelperClass(name, email, username, password);


reference.child(name).setValue(helperClass);

MGITER / CE / MAD / 2024 pg. 31


B.E. 4th YEAR 7th SEM EN: 210330107015

Toast.makeText(MainActivity2.this, "You have signed up successfully!",


Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity2.this, LoginActivity.class);
startActivity(intent);
}
});

loginRedirectText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity2.this, LoginActivity.class);
startActivity(intent);
}
});
}
}

E. activity_login.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"
android:orientation="vertical"
android:gravity="center"
android:background="blue"
tools:context=".LoginActivity">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
app:cardCornerRadius="30dp"
app:cardElevation="20dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"

MGITER / CE / MAD / 2024 pg. 32


B.E. 4th YEAR 7th SEM EN: 210330107015

android:padding="24dp"
android:background="@drawable/lavender_border">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="36sp"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@color/lavender"/>

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/login_username"
android:background="@drawable/lavender_border"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Username"
android:drawableLeft="@drawable/ic_baseline_person_pin_24"
android:drawablePadding="8dp"
android:textColor="@color/black"/>

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/login_password"
android:background="@drawable/lavender_border"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Password"
android:inputType="textPassword"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:drawablePadding="8dp"
android:textColor="@color/black"/>

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="LOGIN"
android:id="@+id/login_button"
android:textSize="18sp"
android:layout_marginTop="30dp"

MGITER / CE / MAD / 2024 pg. 33


B.E. 4th YEAR 7th SEM EN: 210330107015

android:shadowRadius="20dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/signupRedirectText"
android:text="Not yet registerd? sign Up"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:textColor="@color/lavender"/>

</LinearLayout>
</LinearLayout>

F. LoginActivity.java

package com.example.signup;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import androidx.activity.EdgeToEdge;

import java.util.Objects;

public class LoginActivity extends AppCompatActivity {

EditText loginUsername, loginPassword;


Button loginButton;
TextView signupRedirectText;

MGITER / CE / MAD / 2024 pg. 34


B.E. 4th YEAR 7th SEM EN: 210330107015

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_login);

loginUsername = findViewById(R.id.login_username);
loginPassword = findViewById(R.id.login_password);
signupRedirectText = findViewById(R.id.signupRedirectText);
loginButton = findViewById(R.id.login_button);

loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!validateUsername() | !validatePassword()) {
// Handle validation failure
} else {
checkUser();
}
}
});

signupRedirectText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}

public Boolean validateUsername() {


String val = loginUsername.getText().toString();
if (val.isEmpty()) {
loginUsername.setError("Username cannot be empty");
return false;
} else {
loginUsername.setError(null);
return true;
}
}

MGITER / CE / MAD / 2024 pg. 35


B.E. 4th YEAR 7th SEM EN: 210330107015

public Boolean validatePassword() {


String val = loginPassword.getText().toString();
if (val.isEmpty()) {
loginPassword.setError("Password cannot be empty");
return false;
} else {
loginPassword.setError(null);
return true;
}
}

public void checkUser() {


String userUsername = loginUsername.getText().toString().trim();
String userPassword = loginPassword.getText().toString().trim();

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");


Query checkUserDatabase =
reference.orderByChild("username").equalTo(userUsername);

checkUserDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
loginUsername.setError(null);
String passwordFromDB =
snapshot.child(userUsername).child("password").getValue(String.class);

if (Objects.equals(passwordFromDB, userPassword)) {
loginUsername.setError(null);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
} else {
loginPassword.setError("Invalid Credentials");
loginPassword.requestFocus();
}
} else {
loginUsername.setError("User does not exist");
loginUsername.requestFocus();
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// Handle database error

MGITER / CE / MAD / 2024 pg. 36


B.E. 4th YEAR 7th SEM EN: 210330107015

}
});
}
}

G. HelperClass.java

package com.example.signup;

public class HelperClass {

String name, email, username, password;

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getPassword() {


return password;
}

public void setPassword(String password) {


this.password = password;
}

public String getUsername() {


return username;
}

public void setUsername(String username) {


this.username = username;
}

MGITER / CE / MAD / 2024 pg. 37


B.E. 4th YEAR 7th SEM EN: 210330107015

public HelperClass(String email, String name, String password, String username) {


this.email = email;
this.name = name;
this.password = password;
this.username = username;
}
public HelperClass(){
}
}

Output:

MGITER / CE / MAD / 2024 pg. 38


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 7

Aim: Broadcast Receiver.

Input

A. activity_main.xml

MGITER / CE / MAD / 2024 pg. 39


B.E. 4th YEAR 7th SEM EN: 210330107015

<?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:background="@drawable/background"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! "
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

B. MainActivity.java

package com.example.myapp7;

import android.os.Bundle;
import android.content.Intent;
import android.content.IntentFilter;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

AirplaneModeChangeReceiver airplaneModeChangeReceiver = new


AirplaneModeChangeReceiver();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

MGITER / CE / MAD / 2024 pg. 40


B.E. 4th YEAR 7th SEM EN: 210330107015

@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeChangeReceiver, filter);
}

@Override
protected void onStop() {
super.onStop();
unregisterReceiver(airplaneModeChangeReceiver);
}
}

C. AirplaneModeChangeReceiver.java

package com.example.myapp7;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.widget.Toast;

public class AirplaneModeChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

if (isAirplaneModeOn(context.getApplicationContext())) {
Toast.makeText(context, "AirPlane mode is on", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "AirPlane mode is off", Toast.LENGTH_SHORT).show();
}
}

private static boolean isAirplaneModeOn(Context context) {


return Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}

Output

MGITER / CE / MAD / 2024 pg. 41


B.E. 4th YEAR 7th SEM EN: 210330107015

MGITER / CE / MAD / 2024 pg. 42


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 8

Aim: Get and Save User Preferences.

Input

A. 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"
android:background="@drawable/background"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="26dp"
android:ems="10"
android:inputType="text"
android:textColor="@color/black"
android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="73dp"
android:ems="10"
android:inputType="text"
android:textColor="@color/black"

MGITER / CE / MAD / 2024 pg. 43


B.E. 4th YEAR 7th SEM EN: 210330107015

android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="101dp"
android:layout_marginBottom="16dp"
android:text="Name"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/editTextText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:layout_marginBottom="17dp"
android:text="Email"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/editTextText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextText" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginBottom="105dp"
android:text="Save"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"

MGITER / CE / MAD / 2024 pg. 44


B.E. 4th YEAR 7th SEM EN: 210330107015

app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextText2" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:text="Next Page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>

B. MainActivity.java

package com.example.sharedpreferencetutorial;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText name, email;


Button button, button2;
SharedPreferences sp;
String nameStr, emailStr;

@Override
protected void onCreate(Bundle savedInstanceState) {

MGITER / CE / MAD / 2024 pg. 45


B.E. 4th YEAR 7th SEM EN: 210330107015

super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

name = findViewById(R.id.editTextText);
email = findViewById(R.id.editTextText2);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
sp = getSharedPreferences("MyUserPreference", Context.MODE_PRIVATE);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { // Corrected method name
nameStr = name.getText().toString();
emailStr = email.getText().toString();

SharedPreferences.Editor editor = sp.edit();


editor.putString("name", nameStr);
editor.putString("email", emailStr);
editor.commit();
Toast.makeText(MainActivity.this, "Information Saved",
Toast.LENGTH_LONG).show();
}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { // Corrected method name
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
}

MGITER / CE / MAD / 2024 pg. 46


B.E. 4th YEAR 7th SEM EN: 210330107015

C. 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"
android:background="@drawable/background"
tools:context=".MainActivity2">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="103dp"
android:layout_marginBottom="609dp"
android:text=" "
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="306dp"
android:layout_marginBottom="406dp"
android:text=" "
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MGITER / CE / MAD / 2024 pg. 47


B.E. 4th YEAR 7th SEM EN: 210330107015

D. MainActivity2.java

package com.example.sharedpreferencetutorial;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main2);

TextView t1, t2;


t1 = findViewById(R.id.textView3);
t2 = findViewById(R.id.textView4);

SharedPreferences sp =
getApplicationContext().getSharedPreferences("MyUserPreference",
Context.MODE_PRIVATE);
String name = sp.getString("name", "");
String email = sp.getString("email", "");

t1.setText(name);
t2.setText(email);

}
}

MGITER / CE / MAD / 2024 pg. 48


B.E. 4th YEAR 7th SEM EN: 210330107015

Output :

MGITER / CE / MAD / 2024 pg. 49


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 9

Aim: Write the code to display the sum of two numbers.

Input

A. 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"
android:background="@drawable/background"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="150dp"
android:layout_height="48dp"
android:layout_marginStart="10dp"
android:layout_marginTop="52dp"
android:gravity="center_vertical"
android:text="@string/first_textView"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintEnd_toStartOf="@+id/first_number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/first_number"
android:layout_width="150dp"
android:layout_height="48dp"
android:layout_marginTop="52dp"
android:ems="10"
android:hint="@string/first_number_hint"
android:textColorHint="@color/white"
android:inputType="number"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"

MGITER / CE / MAD / 2024 pg. 50


B.E. 4th YEAR 7th SEM EN: 210330107015

app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="150dp"
android:layout_height="48dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:text="@string/second_textView"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintEnd_toStartOf="@+id/second_number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1" />

<EditText
android:id="@+id/second_number"
android:layout_width="175dp"
android:layout_height="48dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="@string/second_number_hint"
android:textColorHint="@color/white"
android:inputType="number"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/first_number" />

<Button
android:id="@+id/button"
android:layout_width="139dp"
android:layout_height="57dp"
android:layout_marginTop="84dp"
android:text="@string/Button_value"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView

MGITER / CE / MAD / 2024 pg. 51


B.E. 4th YEAR 7th SEM EN: 210330107015

android:id="@+id/result"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginTop="88dp"
android:gravity="center"
android:text="@string/result"
android:textColor="@color/white"
android:textSize="16sp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/result_value"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.006" />

<TextView
android:id="@+id/result_value"
android:layout_width="150dp"
android:layout_height="40dp"
android:gravity="center"
android:textSize="16sp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/result"
app:layout_constraintTop_toTopOf="@+id/result" />

</androidx.constraintlayout.widget.ConstraintLayout>

B. String.xml

<resources>
<string name="app_name">My App9</string>
<string name="first_textView">First Number:</string>
<string name="first_number_hint">Enter first number</string>
<string name="second_textView">Second Number:</string>
<string name="second_number_hint">Enter second number</string>
<string name="Button_value">Add</string>
<string name="result">Result:</string>
</resources>

MGITER / CE / MAD / 2024 pg. 52


B.E. 4th YEAR 7th SEM EN: 210330107015

C. MainActivity.java

package com.example.myapp9;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

// Variables
EditText number1,number2;
Button Add_button;
TextView temp,result;
int ans=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Assiging the values to variables


number1=(EditText) findViewById(R.id.first_number);
number2=(EditText) findViewById(R.id.second_number);
Add_button=(Button) findViewById(R.id.button);
result=(TextView) findViewById(R.id.result_value);
temp=(TextView) findViewById(R.id.result);

Add_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
// add both number and store it to sum
double sum = num1 + num2;
// set it ot result textviewe
result.setText(Double.toString(sum));

temp.setVisibility(View.VISIBLE);
result.setVisibility(View.VISIBLE);
}

MGITER / CE / MAD / 2024 pg. 53


B.E. 4th YEAR 7th SEM EN: 210330107015

});
}
}

Output

MGITER / CE / MAD / 2024 pg. 54


B.E. 4th YEAR 7th SEM EN: 210330107015

Practical 10

Aim: Write the code to check which toggle button is ON / OFF.

Input

A. 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"
android:background="@drawable/background"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:text="Choose your hobbies:"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Painting"
android:layout_marginTop="16dp"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"

MGITER / CE / MAD / 2024 pg. 55


B.E. 4th YEAR 7th SEM EN: 210330107015

android:text="Reading"
android:layout_marginTop="16dp"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Singing"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="@+id/textView"
tools:layout_editor_absoluteX="382dp" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cooking"
android:layout_marginTop="16dp"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
tools:layout_editor_absoluteX="386dp" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="Check"
android:text="submit" />
</LinearLayout>

MGITER / CE / MAD / 2024 pg. 56


B.E. 4th YEAR 7th SEM EN: 210330107015

B. MainActivity.java

package com.example.myapp10;

import android.os.Bundle;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends AppCompatActivity {

CheckBox ch, ch1, ch2, ch3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Binding MainActivity.java with activity_main.xml file
setContentView(R.layout.activity_main);

// Finding CheckBox by its unique ID


ch=(CheckBox)findViewById(R.id.checkBox);
ch1=(CheckBox)findViewById(R.id.checkBox2);
ch2=(CheckBox)findViewById(R.id.checkBox3);
ch3=(CheckBox)findViewById(R.id.checkBox4);
}

// This function is invoked when the button is pressed.


public void Check(View v)
{
String msg="";

// Concatenation of the checked options in if

// isChecked() is used to check whether


// the CheckBox is in true state or not.

if(ch.isChecked())
msg = msg + " Painting ";
if(ch1.isChecked())
msg = msg + " Reading ";
if(ch2.isChecked())
msg = msg + " Singing ";
if(ch3.isChecked())

MGITER / CE / MAD / 2024 pg. 57


B.E. 4th YEAR 7th SEM EN: 210330107015

msg = msg + " Cooking ";

// Toast is created to display the


// message using show() method.
Toast.makeText(this, msg + "are selected",
Toast.LENGTH_LONG).show();
}
}

Output

MGITER / CE / MAD / 2024 pg. 58

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