0% found this document useful (0 votes)
2 views28 pages

Android Assignment

This document is a certificate for a student completing practical work in Current Trends in IT as part of their BCA curriculum for the academic year 2023-2024. It includes a series of assignments that involve creating mobile applications using Android, covering functionalities like user registration, string manipulation, bill generation, and course-subject selection. Each assignment provides detailed XML layout and Java code examples for implementation.

Uploaded by

grade1.susukham
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)
2 views28 pages

Android Assignment

This document is a certificate for a student completing practical work in Current Trends in IT as part of their BCA curriculum for the academic year 2023-2024. It includes a series of assignments that involve creating mobile applications using Android, covering functionalities like user registration, string manipulation, bill generation, and course-subject selection. Each assignment provides detailed XML layout and Java code examples for implementation.

Uploaded by

grade1.susukham
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

Institute of Business Studies & Research®

Activators of Human Potential


CBD Belapur

Date: _________

Certificate
This is to certify that Mr. ___________________________ seat no ___________of BCA
Semester-VI has completed the practical work in the subject of Current Trends in IT
during the academic year 2023–2024 under the guidance of Prof. Kamalam Sundarrajan
being the partial requirement for the fulfillment of the curriculum of Degree of Bachelor
of Computer Application, University of Tilak Maharashtra Vidyapeeth.
.

Signature of Internal Guide Signature of HOD

College Seal
INDEX

Sr. Date Title Sign


no

1 Assignment 1

2 Assignment 2

3 Assignment 3

4 Assignment 4

5 Assignment 5

6 Assignment 6

7 Assignment 7

8 Assignment 8
Assignment 1
Create a mobile app containing 2 EditTexts for username and password, 1 CheckBox for
course selection, RadioButtons for gender and Register Button. On click of Register Button,
display username, password, course selection & selected gender using Toast?

activity_main.xml:-

<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#292F50"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:hint="Password"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/courseCheckBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BCA" />

<CheckBox
android:id="@+id/courseCheckBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MCA" />

<CheckBox
android:id="@+id/courseCheckBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="MBA" />

<RadioGroup
android:id="@+id/genderRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/maleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />

<RadioButton
android:id="@+id/femaleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>

<Button
android:id="@+id/registerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Register" />
</LinearLayout>

Content_main.xml:-

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


<LinearLayout xmlns:android="[Link]
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#491E1E"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="10"
android:text="This is a custom toast message"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>

[Link]:-

package [Link].apk1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


EditText usernameEditText, passwordEditText;
CheckBox courseCheckBox1, courseCheckBox2, courseCheckBox3;
RadioGroup genderRadioGroup;
Button registerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
usernameEditText = findViewById([Link]);
passwordEditText = findViewById([Link]);
courseCheckBox1 = findViewById([Link].courseCheckBox1);
courseCheckBox2 = findViewById([Link].courseCheckBox2);
courseCheckBox3 = findViewById([Link].courseCheckBox3);
genderRadioGroup = findViewById([Link]);
registerButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String username = [Link]().toString();
String password = [Link]().toString();
StringBuilder courseSelection = new StringBuilder();
if ([Link]()) {
[Link]("BCA, ");
}
if ([Link]()) {
[Link]("MCA, ");
}
if ([Link]()) {
[Link]("MBA");
}
int genderId = [Link]();
RadioButton selectedGender = findViewById(genderId);
String gender = selectedGender != null ?
[Link]().toString() : "Gender not selected";
String message = "Username: " + username + "\nPassword: " + password + "\nCourse
Selection: " + ([Link]() > 0 ? [Link]() : "No courses selected") +
"\nGender: " + gender;
View layout = getLayoutInflater().inflate([Link].content_main, null);
TextView text = [Link]([Link]);
[Link](message);
Toast toast = new Toast(getApplicationContext());
[Link](Toast.LENGTH_LONG);
[Link](layout);
[Link]();
}
});
}
}

Output :-
Assignment 2
Create a mobile app containing EditText (for accepting string from user), 2 RadioButtons
([Link]-CASE, [Link]-case) and Show Button. Perform selected operation on the string in
EditText & display result on the same page using Toast.

activity_main.xml:-

<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1E2130"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a string" />

<RadioGroup
android:id="@+id/caseRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/upperRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPPER-CASE" />

<RadioButton
android:id="@+id/lowerRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lower-case" />
</RadioGroup>

<Button
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show" />
</LinearLayout>

[Link]:-

package [Link].apk2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText inputEditText;
RadioGroup caseRadioGroup;
Button showButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
inputEditText = findViewById([Link]);
caseRadioGroup = findViewById([Link]);
showButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String inputString = [Link]().toString();
int checkedId = [Link]();
RadioButton selectedRadioButton = findViewById(checkedId);
if (selectedRadioButton != null) {
String caseOption = [Link]().toString();
if ([Link]("UPPER-CASE")) {
inputString = [Link]();
} else if ([Link]("lower-case")) {
inputString = [Link]();
}
[Link]([Link], "Result: " + inputString,
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Please select an option",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

Output :-
Assignment 3
Create a mobile app containing 3 checkboxes for different menus. When user selects any
menu or all menus, generate a bill with selected menus along with their quantity using
toast.

activity_main.xml:-

<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/menu1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible">

<CheckBox
android:id="@+id/checkBoxMenu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paneer with Roti" />

<EditText
android:id="@+id/quantityMenu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_weight="1"
android:hint="Quantity"
android:inputType="number" />
</LinearLayout>

<LinearLayout
android:id="@+id/menu2Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible">

<CheckBox
android:id="@+id/checkBoxMenu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gajar ka Halwa" />

<EditText
android:id="@+id/quantityMenu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_weight="1"
android:hint="Quantity"
android:inputType="number" />
</LinearLayout>

<LinearLayout
android:id="@+id/menu3Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible">

<CheckBox
android:id="@+id/checkBoxMenu3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Litti Chokha" />

<EditText
android:id="@+id/quantityMenu3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_weight="1"
android:hint="Quantity"
android:inputType="number" />
</LinearLayout>

<Button
android:id="@+id/btnGenerateBill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Generate Bill" />

</LinearLayout>

content_main.xml:-

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


<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#491E1E"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="This is a custom toast message"
android:textColor="#FFFFFF"
android:textSize="20sp" />

</LinearLayout>

[Link]:-

package [Link].apk3;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

CheckBox menu1CheckBox, menu2CheckBox, menu3CheckBox;


Button generateBillButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

menu1CheckBox = findViewById([Link].checkBoxMenu1);
menu2CheckBox = findViewById([Link].checkBoxMenu2);
menu3CheckBox = findViewById([Link].checkBoxMenu3);
generateBillButton = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
StringBuilder bill = new StringBuilder();
int totalAmount = 0;

if ([Link]()) {
[Link]("Paneer with Roti: ");
int quantity =
[Link](((TextView)findViewById([Link].quantityMenu1)).getText().toString());
[Link](quantity + " x 300rs = " + (quantity * 300) + "rs\n");
totalAmount += quantity * 300;
}

if ([Link]()) {
[Link]("Gajar ka Halwa : ");
int quantity =
[Link](((TextView)findViewById([Link].quantityMenu2)).getText().toString());
[Link](quantity + " x 150rs = " + (quantity * 150) + "rs\n");
totalAmount += quantity * 150;
}

if ([Link]()) {
[Link]("Litti Chokha: ");
int quantity =
[Link](((TextView)findViewById([Link].quantityMenu3)).getText().toString());
[Link](quantity + " x 450rs = " + (quantity * 450) + "rs\n");
totalAmount += quantity * 450;
}

if ([Link]() > 0) {
View layout = getLayoutInflater().inflate([Link].content_main, null);
TextView text = [Link]([Link]);
[Link]("--------------------------");
[Link]("\nTotal Amount: Rs " + totalAmount);
[Link](bill);
[Link](bill);
Toast toast = new Toast(getApplicationContext());
[Link](Toast.LENGTH_LONG);
[Link](layout);
[Link]();

} else {
[Link]([Link], "Please select at least one menu",
Toast.LENGTH_SHORT).show();
}

}
});
}
}

Output :-
Assignment 4
Create a mobile app containing ListView having a list of courses. On click of any course,
subjects related to that course should display in spinner. For eg. For BCA course- C,C++,
Java, For BCom course- Accounts, PPM, Economics. When user selects BCA from
listview-subjects related to BCA should appear in spinner.

activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/coursesListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

content_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<TextView
android:id="@+id/courseTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textSize="18sp"/>

<Spinner
android:id="@+id/subjectsSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>

</LinearLayout>

[Link]:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private ListView coursesListView;


private HashMap<String, List<String>> subjectsMap;
private CourseAdapter courseAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

coursesListView = findViewById([Link]);

initializeSubjectsMap();
populateCoursesListView();
}

private void initializeSubjectsMap() {


subjectsMap = new HashMap<>();
[Link]("BCA", new ArrayList<String>() {{
add("C");
add("C++");
add("Java");
add("Python");
add("HTML");
add("CSS");
}});
[Link]("BCom", new ArrayList<String>() {{
add("Account”);
add("PPM");
add("Economics");
add("Business Law");
add("Marketing");
add("Financial Management");
}});
[Link]("BSc", new ArrayList<String>() {{
add("Physics");
add("Chemistry");
add("Biology");
add("Mathematics");
add("Computer Science");
add("Environmental Science");
}});
}

private void populateCoursesListView() {


List<String> coursesList = new ArrayList<>([Link]());
courseAdapter = new CourseAdapter(this, coursesList);
[Link](courseAdapter);
}

public class CourseAdapter extends ArrayAdapter<String> {

public CourseAdapter(@NonNull MainActivity context, @NonNull List<String> courses) {


super(context, 0, courses);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = [Link](getContext()).inflate([Link].content_main, parent, false);
}

final String course = getItem(position);


TextView courseTextView = [Link]([Link]);
final Spinner subjectsSpinner = [Link]([Link]);

if (course != null) {
[Link](course);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if ([Link]() == [Link]) {
[Link]([Link]);
} else {
[Link]([Link]);

}
}
});

List<String> subjects = [Link](course);


if (subjects != null) {
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(getContext(),
[Link].simple_spinner_item, subjects);

[Link]([Link].simple_spinner_dropdown_item);
[Link](spinnerAdapter);

[Link](new [Link]()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedSubject = (String) [Link](position);
[Link]([Link], "Selected subject for " + course + ": " +
selectedSubject, Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing
}
});
}
}

return itemView;
}
}
}

Output :-
Assignment 5
Create a mobile app containing ListView having a list of countries. On click of any country in
the listview, country's flag should display in ImageView.

activity_main.xml:-
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/countriesListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>

<ImageView
android:id="@+id/flagImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/india_flag"
android:visibility="gone"/>

</RelativeLayout>

[Link]:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private ListView countriesListView;


private ImageView flagImageView;
private String[] countries = {"USA", "UK", "Germany", "France", "Italy", "INDIA"};
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
countriesListView = findViewById([Link]);
flagImageView = findViewById([Link]);
ArrayAdapter<String> adapter = new ArrayAdapter<> (this
,[Link].simple_list_item_1 , countries);
[Link](adapter);
[Link](new [Link]() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedCountry = countries[position];
int flagResource = getFlagResource(selectedCountry);
if (flagResource != 0) {
[Link](flagResource);
[Link]([Link]);
} else {
[Link]([Link]);
}
[Link]([Link], "Clicked: " + selectedCountry,Toast.LENGTH_SHORT).show();
}});}

private int getFlagResource(String country) {


switch (country) {
case "USA": return [Link].usa_flag;
case "UK": return [Link].uk_flag;
case "Germany": return [Link].germany_flag;
case "France": return [Link].france_flag;
case "Italy": return [Link].italy_flag;
case "INDIA": return [Link].india_flag;
default: return [Link].india_flag;
}}}

Output :-
Assignment 6
Create a mobile app containing 2 EditTexts for username and password. When user clicks on
submit show it to the next Activity.

Activity_main.xml:-

<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:hint="Username" />

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

<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="Submit" />

</LinearLayout>

Content_main.xml:-

<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<TextView
android:id="@+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/passwordTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>

[Link]:-

package [Link].apk4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private EditText editTextUsername, editTextPassword;


private Button buttonSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextUsername = findViewById([Link]);
editTextPassword = findViewById([Link]);
buttonSubmit = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String username = [Link]().toString().trim();
String password = [Link]().toString().trim();
Intent intent = new Intent([Link], [Link]);
[Link]("USERNAME", username);
[Link]("PASSWORD", password);
startActivity(intent);
}
});
}
}

[Link] :-

package [Link].apk4;
import [Link];
import [Link];
import [Link];
import [Link];

public class SecondActivity extends AppCompatActivity {

private TextView textViewUsername, textViewPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].content_main);
textViewUsername = findViewById([Link]);
textViewPassword = findViewById([Link]);
Intent intent = getIntent();
if (intent != null) {
String username = [Link]("USERNAME");
String password = [Link]("PASSWORD");
[Link]("Username: " + username);
[Link]("Password: " + password);
}
}
}

Output :-
Assignment 7
Designn a mobile application for music player using android services.

Activity_main.xml:-

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


<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvSongTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Song Title"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp">

<Button
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous"
android:layout_below="@id/tvSongTitle"
android:layout_alignParentStart="true"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"/>

<Button
android:id="@+id/btnPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_below="@id/tvSongTitle"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"/>

<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_below="@id/tvSongTitle"
android:layout_alignParentEnd="true"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"/>
</RelativeLayout>
[Link]:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private List<Integer> songsList = new ArrayList<>();


private MediaPlayer mediaPlayer;
private int currentSongIndex = 0;
private TextView tvSongTitle; @Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tvSongTitle = findViewById([Link]);

Button btnPlayPause = findViewById([Link]);


Button btnPrevious = findViewById([Link]);
Button btnNext = findViewById([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
mediaPlayer = [Link](this, [Link](currentSongIndex));
updateSongTitle();

[Link](new [Link]() {
@Override
public void onClick(View v) {
playPauseSong();
}});
[Link](new [Link]() {
@Override
public void onClick(View v) {
playPreviousSong();
}});
[Link](new [Link]() {
@Override
public void onClick(View v) {
playNextSong();
}});}
private void playPauseSong() {
if ([Link]()) {
[Link]();
} else {
[Link]();
}}
private void playNextSong() {
if (currentSongIndex < [Link]() - 1) {
currentSongIndex++;
[Link]();
mediaPlayer = [Link](this, [Link](currentSongIndex));
[Link]();
updateSongTitle();
} else {
[Link](this, "No more songs", Toast.LENGTH_SHORT).show();
}}

private void playPreviousSong() {


if (currentSongIndex > 0) {
currentSongIndex--;
[Link]();
mediaPlayer = [Link](this, [Link](currentSongIndex));
[Link]();
updateSongTitle();
} else {
[Link](this, "No previous songs", Toast.LENGTH_SHORT).show();
}}
private void updateSongTitle() {
String songName = getResources().getResourceEntryName([Link](currentSongIndex));
[Link](songName);
}

@Override
protected void onDestroy() {
[Link]();
if (mediaPlayer != null) {
[Link]();
mediaPlayer = null;
}}}

Output :-
Assignment 8
Create a mobile app for storing username and phone number using database. And display all
records using toast.
Activity_main.xml:-

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


<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:hint="Enter Name" />
<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone" />

<Button
android:id="@+id/btnAddUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addUser"
android:text="+ Add User" />
<Button
android:id="@+id/btnShowUsers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:onClick="showUsers"
android:text="Show Users" />
</LinearLayout>

Custom_toast.xml:-

<TextView xmlns:android="[Link]
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AA000000"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold"
android:padding="16dp" />
[Link]:-

package [Link].apk7;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
public class MainActivity extends AppCompatActivity {
private EditText editTextName, editTextPhone;
private DBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextName = findViewById([Link]);
editTextPhone = findViewById([Link]);
dbHandler = new DBHandler(this);
}
public void addUser(View view) {
String name = [Link]().toString().trim();
String phone = [Link]().toString().trim();
if (![Link]() && ![Link]()) {
int id = generateUserId();
User user = new User(id, name, phone);
[Link](user);
[Link](this, "User added to database",
Toast.LENGTH_SHORT).show();
[Link]("");
[Link]("");
} else {
[Link](this, "Please enter name and phone number",
Toast.LENGTH_SHORT).show();
}}
private int userIdCounter = 1;
private int generateUserId() {
return userIdCounter++;
}
private int getNextUserIdFromDatabase() {
return userIdCounter;
}
public void showUsers(View view) {
Cursor cursor = [Link]();
if (cursor != null && [Link]()) {
StringBuilder usersText = new StringBuilder();
int nameIndex = [Link](DBHandler.NAME_COL);
int phoneIndex = [Link](DBHandler.PHONE_COL);
do {
String name = [Link](nameIndex);
String phone = [Link](phoneIndex);
[Link]("Name: ").append(name).append("\n Phone:
").append(phone).append("\n\n\n");
} while ([Link]());
[Link]();
showToast([Link]());
} else {
showToast("No users found");
} }
private void showToast(String message) {
View toastView =
getLayoutInflater().inflate([Link].custom_toast, null);
TextView textViewMessage = [Link]([Link]);
[Link](message);
Toast toast = new Toast(this);
[Link](toastView);
[Link](Toast.LENGTH_LONG);
[Link]();
}}

[Link]:-

package [Link].apk7;
public class User {
private int id;
private String name;
private String phone;
public User(int id, String name, String phone) {
[Link] = id;
[Link] = name;
[Link] = phone;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}}

[Link]

package [Link].apk7;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DBHandler extends SQLiteOpenHelper {
private static final String DB_NAME = "userdb";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String ID_COL = "id";
public static final String NAME_COL = "name";
public static final String PHONE_COL = "phone";
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + " (" + ID_COL + " INTEGER
PRIMARY KEY AUTOINCREMENT, " + NAME_COL + " TEXT, " + PHONE_COL + " TEXT)";
[Link](query);
}
public void addUser( User user) {
SQLiteDatabase db = [Link]();
ContentValues values = new ContentValues();
[Link](NAME_COL, [Link]());
[Link](PHONE_COL, [Link]());
[Link](TABLE_NAME, null, values);
[Link]();
}
public Cursor getAllUsers() {
SQLiteDatabase db = [Link]();
String[] projection = {ID_COL, NAME_COL, PHONE_COL};
return [Link](TABLE_NAME, projection, null, null, null, null,
null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
[Link]("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}}

Output :-

You might also like