0% found this document useful (0 votes)
19 views31 pages

Android Lab File1

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

Android Lab File1

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

Program 1

Objective: Create a basic Hello World program.

Code:
/* MainActivity.java */
package com.example.helloworld;

import android.os.Bundle;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

TextView textViewJava;
@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;

});
textViewJava = findViewById(R.id.txt_xml);
textViewJava.setText(R.string.hello_word);
}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/txt_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_text"
android:textSize="32sp"

1
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

/* strings.xml */
<resources>
<string name="app_name">First App</string>
<string name="default_text">GEHU</string>
<string name="hello_word">Hello World !</string>
</resources>

Output:

2
Program 2
Objective: Create a simple application to change colors of background of any
textbox with the click of a button.

Code:
/* MainActivity.java */
package com.example.la_2;

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

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

public class MainActivity extends AppCompatActivity {

TextView textView;
@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;
});
}

public void changeTextBoxBackground(View view) {

textView = findViewById(R.id.txtBox_XML);

int color = (int) Math.floor(Math.random() * 16777215);


color = color | 0xFF000000;

textView.setText(Integer.toHexString(color).toUpperCase());
textView.setBackgroundColor(color);

}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"

3
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/txtBox_XML"
android:layout_width="192dp"
android:layout_height="78dp"
android:gravity="center"
android:text="@string/text_box"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.545" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="300dp"
android:onClick="changeTextBoxBackground"
android:text="@string/change_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output:

4
Program 3
Objective: Create a simple app to change background colour on button click.

Code:
/* MainActivity.java */
package com.example.la3;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

ConstraintLayout mainLayout;
TextView txtJava;

int[] colors = {R.color.light_pink, R.color.light_blue, R.color.light_yellow,


R.color.light_green, R.color.light_purple};
int colorIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main_XML), (v, insets) ->
{
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});
mainLayout = findViewById(R.id.main_XML);
txtJava = findViewById(R.id.txt_XML);
}

public void changeBG(View view){


int color = ContextCompat.getColor(this,colors[colorIndex]);
mainLayout.setBackgroundColor(color);
txtJava.setText("Color Changed!: " + Integer.toHexString(color).toUpperCase());
colorIndex = (colorIndex + 1) % colors.length;
}
}

/* 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"

5
android:id="@+id/main_XML"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/txt_XML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
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.219" />

<Button
android:id="@+id/btn_XML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeBG"
android:text="Change BG"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_XML"
app:layout_constraintVertical_bias="0.313" />

</androidx.constraintlayout.widget.ConstraintLayout>

/* colors.xml */
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="light_pink">#FFB6C1</color>
<color name="light_blue">#ADD8E6</color>
<color name="light_yellow">#FFFFE0</color>
<color name="light_green">#90EE90</color>
<color name="light_purple">#D8BFD8</color>
</resources>

6
Program 4
Objective: Create a simple app to add two numbers.

Code:
/* MainActivity.java */
package com.example.la4;

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

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

public class MainActivity extends AppCompatActivity {

Button addBtn;
EditText num1, num2;
TextView result;

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

addBtn = findViewById(R.id.addBtn);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
result = findViewById(R.id.result);

addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Float n1 = Float.parseFloat(num1.getText().toString());
Float n2 = Float.parseFloat(num2.getText().toString());
result.setText("Answer: " + (n1 + n2));
}
});
}
}

/* 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"
7
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="Add Two Numbers"
android:textSize="20sp"
android:textAlignment="center"
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.14" />

<EditText
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:ems="10"
android:hint="Enter First Number"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:hint="Enter Second Number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/num1"
app:layout_constraintVertical_bias="0.168" />

<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/num2" />

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer: 0"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/addBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.326"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/num2"
app:layout_constraintVertical_bias="0.496" />

</androidx.constraintlayout.widget.ConstraintLayout>

8
Output:

9
Program 5
Objective: Create a simple app to create a simple calculator in android.

Code:
/* MainActivity.java */
package com.example.la5;

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

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btnAdd, btnSubtract, btnMultiply, btnDivide;


EditText etNum1, etNum2, etResult;

@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;
});
etNum1 = findViewById(R.id.etNum1);
etNum2 = findViewById(R.id.etNum2);
etResult = findViewById(R.id.etResult);
assignId(btnAdd, R.id.btnAdd);
assignId(btnSubtract, R.id.btnSubtract);
assignId(btnMultiply, R.id.btnMultiply);
assignId(btnDivide, R.id.btnDivide);
}

public void assignId(Button btn, int id) {


btn = findViewById(id);
btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {

double result = 0;
try {
double num1 = Double.parseDouble(etNum1.getText().toString());
double num2 = Double.parseDouble(etNum2.getText().toString());

if (v.getId() == R.id.btnAdd) {
result = num1 + num2;
} else if (v.getId() == R.id.btnSubtract) {
result = num1 - num2;
} else if (v.getId() == R.id.btnMultiply) {
result = num1 * num2;
10
} else if (v.getId() == R.id.btnDivide) {
result = num1 / num2;
}
}catch (Exception e){
etResult.setError("Error");
}

String resulttext = String.valueOf(result);


if(resulttext.endsWith(".0")){
resulttext = resulttext.substring(0, resulttext.length() - 2);
etResult.setText(resulttext);
}else{
etResult.setText(resulttext);
}
}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/etResult"
android:layout_width="378dp"
android:layout_height="66dp"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:text="0"
android:textColor="@color/black"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.584" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Num 1: "
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.204"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.606" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Num 2: "
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

11
app:layout_constraintHorizontal_bias="0.204"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<EditText
android:id="@+id/etNum1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.647"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.209" />

<EditText
android:id="@+id/etNum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.395" />

<LinearLayout
android:layout_width="375dp"
android:layout_height="220dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etResult">

<Button
android:id="@+id/btnAdd"
android:layout_width="70dp"
android:layout_height="76dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="+" />

<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button
android:id="@+id/btnSubtract"
android:layout_width="70dp"
android:layout_height="76dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="-" />

<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button
android:id="@+id/btnMultiply"
android:layout_width="70dp"
android:layout_height="76dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="*" />

12
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button
android:id="@+id/btnDivide"
android:layout_width="70dp"
android:layout_height="76dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="/" />
</LinearLayout>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Calculator"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.108" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output:

13
Program 6
Objective: Create an app to set image on imageview.

Code:
/* MainActivity.java */
package com.example.la6;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

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

public class MainActivity extends AppCompatActivity {

Button btnSetImage;
ImageView imageView;
@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;
});

imageView = findViewById(R.id.imageView);
btnSetImage = findViewById(R.id.btnSetImage);

btnSetImage.setOnClickListener(v -> {
imageView.setImageResource(R.drawable.img);
});

}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnSetImage"
android:layout_width="wrap_content"
14
android:layout_height="wrap_content"
android:text="Set Image"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.679" />

<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@+id/btnSetImage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_gallery" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output:

15
Program 7
Objective: Create a program to change image on button click.

Code:
/* MainActivity.java */
package com.example.la7;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.DrawableRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

ImageView imageView;
Button btnPrev, btnNext;

@DrawableRes
int[] imageArray = new int[]{R.drawable.img_1, R.drawable.img_2, R.drawable.img_3,
R.drawable.img_4, R.drawable.img_5, R.drawable.img_6};
int imageIndex = 0;

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

imageView = findViewById(R.id.imageView);
btnPrev = findViewById(R.id.btnPrev);
btnNext = findViewById(R.id.btnNext);
imageView.setImageResource(imageArray[imageIndex]);
btnPrev.setEnabled(false);

btnNext.setOnClickListener(v -> {

btnPrev.setEnabled(true);

imageIndex += 1;
imageView.setImageResource(imageArray[imageIndex]);
if (imageIndex == imageArray.length - 1)
btnNext.setEnabled(false);
});

btnPrev.setOnClickListener(v -> {

btnNext.setEnabled(true);

imageIndex -= 1;
imageView.setImageResource(imageArray[imageIndex]);
16
if (imageIndex == 0)
btnPrev.setEnabled(false);

});

}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="550dp"
android:scaleType="centerCrop"
android:contentDescription="Image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.178"
app:srcCompat="@android:drawable/ic_menu_report_image" />

<LinearLayout
android:layout_width="350dp"
android:layout_height="57dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintVertical_bias="0.635">

<Button
android:id="@+id/btnPrev"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Previous"
android:textSize="20sp" />

<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button
android:id="@+id/btnNext"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Next"
android:textSize="20sp" />

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

17
Output:

18
Program 8
Objective: Create a program to depict activity lifecycle.

Code:
/* MainActivity.java */
package com.example.la8;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;

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

public class MainActivity extends AppCompatActivity {

String TAG = "MainActivity";


@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;

});
Log.d(TAG, "onCreate Called");
}

@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart Called");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume Called");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause Called");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop Called");
}@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart Called");
}

19
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy Called");
}
}

/* Log Cat output */

// App started
2024-09-22 17:00:05.176 15608-15608 MainActivity pid-15608 D onCreate Called
2024-09-22 17:00:05.287 15608-15608 MainActivity pid-15608 D onStart Called
2024-09-22 17:00:05.291 15608-15608 MainActivity pid-15608 D onResume Called

// App goes in the background


2024-09-22 17:00:11.905 15608-15608 MainActivity pid-15608 D onPause Called
2024-09-22 17:00:11.978 15608-15608 MainActivity pid-15608 D onStop Called

// App in focus
2024-09-22 17:00:24.860 15608-15608 MainActivity pid-15608 D onRestart Called
2024-09-22 17:00:24.861 15608-15608 MainActivity pid-15608 D onStart Called
2024-09-22 17:00:24.863 15608-15608 MainActivity pid-15608 D onResume Called

// App closed
2024-09-22 17:01:12.948 15608-15608 MainActivity pid-15608 D onPause Called
2024-09-22 17:01:12.974 15608-15608 MainActivity pid-15608 D onStop Called
2024-09-22 17:01:12.978 15608-15608 MainActivity pid-15608 D onDestroy Called

20
Program 9
Objective: Create an App to depict the Started Service.

Code:
/* MainActivity.java */
package com.example.l9;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

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

public class MainActivity extends AppCompatActivity {

Button btnStart, btnStop;

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

btnStart = findViewById(R.id.btnStart);
btnStop = findViewById(R.id.btnStop);

btnStart.setOnClickListener(view -> {
Intent intent = new Intent(this, MyService.class);
startService(intent);
});

btnStop.setOnClickListener(view -> {
Intent intent = new Intent(this, MyService.class);
stopService(intent);
});
}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

21
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
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.245" />

<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnStart"
app:layout_constraintVertical_bias="0.473" />
</androidx.constraintlayout.widget.ConstraintLayout>

/* MyService.java */
package com.example.l9;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class MyService extends Service {

MediaPlayer mediaPlayer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

mediaPlayer = MediaPlayer.create(this,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
mediaPlayer.setLooping(true);
mediaPlayer.start();
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
}

22
/* 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.L9"
tools:targetApi="31">

<service android:name=".MyService" />


<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:

23
Program 10
Objective: Show Bounded Services in android app.

Code:
/* MainActivity.java */
package com.example.l10;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

BoundedService myBService = new BoundedService();


Intent myIntent;
boolean bound = false;

@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;
});
}
public ServiceConnection sConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
BoundedService.LocalBinder activityBinder =(BoundedService.LocalBinder)iBinder;
myBService = activityBinder.getService();
bound=true;
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
bound=false;
}
};

public void getNumber(View view) {


if (bound) {
int i = myBService.getRandom();
Toast.makeText(MainActivity.this, "" + i, Toast.LENGTH_SHORT).show();
}
}

24
public void start(View view){
myIntent = new Intent(MainActivity.this, BoundedService.class);
bindService(myIntent, sConnection, BIND_AUTO_CREATE);
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
}

public void stop(View view){


unbindService(sConnection);
bound=false;
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="Start"
android:textSize="24sp"
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.2" />

<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:onClick="stop"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_Start"
app:layout_constraintVertical_bias="0.24" />

<Button
android:id="@+id/btnGetNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate Number"
android:onClick="getNumber"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnStop"
app:layout_constraintVertical_bias="0.343" />
</androidx.constraintlayout.widget.ConstraintLayout>

/* BoundedService.java */
25
package com.example.l10;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import androidx.annotation.Nullable;

import java.util.Random;

public class BoundedService extends Service {

private Random noGenerator= new Random();


private IBinder myBinder = new LocalBinder();

public class LocalBinder extends Binder {


BoundedService getService(){
return BoundedService.this;
}

}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}

public int getRandom(){


return noGenerator.nextInt(100);
}
}

/* 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.L10"
tools:targetApi="31">
<service android:name=".BoundedService"/>
<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>

26
Output:

27
Program 11
Objective: Create an app to show array adapter.

Code:
/* MainActivity.java */
package com.example.l11;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

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

public class MainActivity extends AppCompatActivity {

ListView listView;
String[] myArr = {"GEHU-BTL", "GEHU-HLD", "GEU-DDN"};

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

listView = (ListView) findViewById(R.id.listView);

ArrayAdapter myad = new ArrayAdapter(this, R.layout.my_template,R.id.textView, myArr);

listView.setAdapter(myad);

}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="315dp"
android:layout_height="490dp"
28
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.497" />

</androidx.constraintlayout.widget.ConstraintLayout>

/* my_template.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/purple"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Output:

29
Program 12
Objective: Create an app to show broadcast receiver.

Code:
/* MainActivity.java */
package com.example.l12;

import android.bluetooth.BluetoothAdapter;
import android.content.IntentFilter;
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;

public class MainActivity extends AppCompatActivity {

MyBroadcast myBroadcast = new MyBroadcast();


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

@Override
protected void onStart() {
super.onStart();

IntentFilter myIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);

registerReceiver(myBroadcast, myIntent);

}
}

/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
30
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

/* MyBroadcast.java */
package com.example.l12;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcast extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);

switch (state){
case BluetoothAdapter.STATE_ON:
Toast.makeText(context, "Bluetooth Turned On", Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_OFF:
Toast.makeText(context, "Bluetooth Turned Off",
Toast.LENGTH_SHORT).show();
break;
}
}
}
}

Output:

31

You might also like