0% found this document useful (0 votes)
19 views

Android Development Assignment - DHANANJAY SINGHAL

The document discusses solutions to four questions related to Android development. It includes code snippets for creating basic Android apps with buttons, text views, login functionality and using a spinner to change images. For each question, XML and Java code is provided to implement the given task.

Uploaded by

Shubh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Android Development Assignment - DHANANJAY SINGHAL

The document discusses solutions to four questions related to Android development. It includes code snippets for creating basic Android apps with buttons, text views, login functionality and using a spinner to change images. For each question, XML and Java code is provided to implement the given task.

Uploaded by

Shubh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

UNIVERSITY OF DELHI

HANSRAJ COLLEGE
BSC (HONS) COMPUTER SCIENCE
SEMESTER - 4

ANDROID DEVELOPMENT

DHANANJAY SINGHAL
21CS/13
Question 1 :

Create a “Hello World” application. That will display “Hello World” in the middle of the screen in the
emulator. Also display “Hello World” in the middle of the screen in the Android Phone.

Solution 1 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?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"
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>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical1;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

Emulator Android Phone

Question 2 :

Create an application with three buttons (increment, decrement and reset) and a textView aligned
vertically. On clicking, increment/decrement button, the value of the textview should
increment/decrement by 1 while selecting the reset button, the value of textview should become
zero.

Solution 2 :

activity_main.xml :

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?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"
android:background="#9C27B0"
android:backgroundTintMode="screen"
tools:context=".MainActivity">

<LinearLayout

android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
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.157">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:onClick="decrement"
android:padding="16dp"
android:text="-"
android:textSize="24sp"
android:textStyle="bold"

/>

<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-condensed-medium"
android:padding="16dp"
android:text="0"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="italic" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="increment"
android:padding="16dp"
android:text="+"
android:textSize="24sp"

/>

</LinearLayout>

<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="108dp"
android:onClick="reset"
android:text="Reset"
android:textSize="24sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="@+id/linearLayout"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="@+id/linearLayout"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.Practical_2;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

private TextView mytext;


private int number =0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytext = findViewById(R.id.quantity_text_view);
}

public void decrement(View view) {


if (number !=0)
{
number-=1;
}
DisplayOnText(number);

public void increment(View view) {

number+=1;
DisplayOnText(number);
}

public void reset(View view) {


number =0;
DisplayOnText(number);
}
public void DisplayOnText(int num)
{
mytext.setText(""+num);
}
}

Output 2 :
Question 3 :

Create an application with a login module. (Check username and password).

Solution 3 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:layout_marginTop="49dp"
android:text="User Name"
android:textSize="18sp" />

<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvName"
android:layout_alignBottom="@+id/tvName"
android:layout_alignParentEnd="true"
android:layout_marginEnd="23dp"
android:ems="10"
android:inputType="textPersonName" />

<TextView
android:id="@+id/tvPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/tvName"
android:layout_below="@+id/etUsername"
android:layout_marginTop="32dp"
android:text="Password"
android:textSize="18sp" />

<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvPass"
android:layout_alignBottom="@+id/tvPass"
android:layout_alignStart="@+id/etUsername"
android:ems="10"
android:inputType="textPassword" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="LOGIN"
/>
<TextView
android:id="@+id/tvLoginStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="100sp"
/>
</RelativeLayout>
Main_activity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical_3;
import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


EditText etUsername, etPassword;
Button btnStatus;
TextView tvLoginStatus;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
btnStatus = (Button) findViewById(R.id.button);
tvLoginStatus = (TextView) findViewById(R.id.tvLoginStatus);

btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
check();
}
});
}

public void check(){


if(etUsername.getText().toString().equals("login") &&
etPassword.getText().toString().equals("password")){
tvLoginStatus.setText("Login successful");
}else{
Toast.makeText(this, "Login failed", Toast.LENGTH_LONG).show();
}
}
}
Output 3 :

Question 4 :

Create spinner with strings taken from resource folder (res >> value folder) and on changing the
spinner value, Image will change.

Solution 4 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:orientation="vertical">

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="116dp"
android:layout_weight="1"
android:text="TextView" />

<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="116dp"
android:layout_weight="1"
android:text="TextView" />

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="116dp"
android:layout_weight="1"
android:text="TextView" />

</LinearLayout>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text1 = (TextView) findViewById(R.id.text1);
ImageView img1 = (ImageView) findViewById(R.id.img1);
String[] city = {"delhi","mumbai","chennai","kolkata"};
Spinner spin = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> arrayadpt = new
ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
spin.setAdapter(arrayadpt);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int
position,long id){
text1.setText("Your selected city : "+ city[position]);
if(position==0) {img1.setImageResource(R.drawable.img2); }
else if(position==1) {img1.setImageResource(R.drawable.img2); }
else if(position==2) {img1.setImageResource(R.drawable.img3); }
else if(position==3) {img1.setImageResource(R.drawable.img4); }
}
public void onNothingSelected(AdapterView<?> parent){
text1.setText("");
}
});
}
}

Output 4 :
Question 5 :

Create a menu with some options and selected options should appear in the text box.

Solution 5 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?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"
android:background="#4593B5"
tools:context=".MainActivity">

<TextView
android:id="@+id/mytv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Menus.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/id1"
android:title="Option1" />
<item
android:id="@+id/id2"
android:title="Option2" />
<item
android:id="@+id/id3"
android:title="Option3" />
<item
android:id="@+id/id4"
android:title="Option4" />
<item
android:id="@+id/id5"
android:title="Option5" />
</menu>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical_5;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.mytv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater m = getMenuInflater();
m.inflate(R.menu.menus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected( MenuItem item) {

switch (item.getItemId())
{
case R.id.id1:
Toast.makeText(this, "Option1", Toast.LENGTH_SHORT).show();
tv.setText("Option1");
break;
case R.id.id2:
Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
tv.setText("Option2");
break;
case R.id.id3:
Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
tv.setText("Option3");

break;
case R.id.id4:
Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();

tv.setText("Option4");
case R.id.id5:
Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
tv.setText("Option5");
break;
}
return true;
}
}
Output 5 :
Question 6 :

Create a list of all courses in your college and on selecting a particular course teacher-in- charge
of that course should appear at the bottom of the screen.

Solution 6 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView)findViewById(R.id.listView);
TextView textView=(TextView)findViewById(R.id.txtView);
String[] listItem = {"BSc","BCom","BA"};
final ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
listItem);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long l) {
// TODO Auto-generated method stub
String value=adapter.getItem(position);
Log.v("value",value);
String teacher="";
if(value.equals("BSc")) { teacher="abc"; }
else if(value.equals("BCom")){ teacher="pqr"; }
else if(value.equals("BA")) teacher="ghj";

Toast.makeText(getApplicationContext(),teacher,Toast.LENGTH_SHORT).show();
}
});
}
}
Output 6 :
Question 7 :

Create an application with three option buttons, on selecting a button color of the screen will
change.

Solution 7 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlVar1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvVar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="240dp"
android:text="What would you like?"
android:textSize="25dp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvVar1"
android:layout_centerInParent="true"
android:layout_marginTop="60dp"
android:orientation="horizontal"
android:padding="10dp">

<Button
android:id="@+id/btVar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0EA1E3"
android:padding="20dp"
android:text="Cool"
android:textColor="#303F9F"
android:textSize="20dp" />
<Button
android:id="@+id/btVar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F6F6F6"
android:padding="20dp"
android:text="neutral"
android:textColor="#1A237E"
android:textSize="20dp" />

<Button
android:id="@+id/btVar3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#FD0505"
android:padding="20dp"
android:text="Hot"
android:textColor="#311B92"
android:textSize="20dp" />

</LinearLayout>

</RelativeLayout>

Main_Activity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical_7;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

Button button1, button2 , button3;


final RelativeLayout relativeLayout;
// set button 1 with its id
button1 = findViewById(R.id.btVar1);

// set button 2 with its id


button2 = findViewById(R.id.btVar2);

// set button 2 with its id


button3 = findViewById(R.id.btVar3);

// set relative layout with its id


relativeLayout = findViewById(R.id.rlVar1);

// onClick function for button 1


button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// set the color to relative layout
relativeLayout.setBackgroundResource(R.color.cool);
}
});

// onClick function for button 2


button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// set the color to relative layout
relativeLayout.setBackgroundResource(R.color.neutral);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
// set the color to relative layout
relativeLayout.setBackgroundResource(R.color.hot);
}

}
);
}
}
colors.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="blue">#2196F3</color>
<color name="cool">#188FCF</color>
<color name="neutral">#F0EFED</color>
<color name="hot">#EC0909</color>
</resources>
Output 7 :
Question 8 :

Create an application to display various activity life cycle and fragment lifecycle methods.

Solution 8 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:text="@string/activity_lifecycle"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/result1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="60dp"
android:text=" "
android:textSize="20sp"
android:textStyle="bold" />

<fragment
android:id="@+id/frag"
android:name="com.example.practical8.BlankFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_toEndOf="@+id/tv1" />

</RelativeLayout>
fragment_blank.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">

<!-- TODO: Update blank fragment layout -->


<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:text="@string/fragment_lifecycle"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/result1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv"
android:layout_marginTop="0dp"
android:text=" "
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = findViewById(R.id.result1);
result.setText("onCreate");
}

@Override
protected void onStop() {
super.onStop();
String res = result.getText().toString();
result.setText(res + "\n" + "onStart");
}

@Override
protected void onStart() {
super.onStart();
String res = result.getText().toString();
result.setText(res + "\n" + "onStart");
}

@Override
protected void onPause() {
super.onPause();
String res = result.getText().toString();
result.setText(res + "\n" + "onPause");
}

protected void onResume() {


super.onResume();
String res = result.getText().toString();
result.setText(res + "\n" + "onResume");
}

@Override
protected void onDestroy() {
super.onDestroy();
String res = result.getText().toString();
result.setText(res + "\n" + "onDestroy");

}
}
BlankFragment.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical8;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment extends Fragment {
TextView result;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank, container,
false);
result = (TextView) view.findViewById(R.id.result1);
return view;
}

@Override
public void onStop() {
super.onStop();
String res = result.getText().toString();
result.setText(res + "\n" + "onStop");
}

@Override
public void onResume() {
super.onResume();
String res = result.getText().toString();
result.setText(res + "\n" + "onResume");
}

@Override
public void onPause() {
super.onPause();
String res = result.getText().toString();
result.setText(res + "\n" + "onPause");
}

@Override
public void onStart() {
super.onStart();
String res = result.getText().toString();
result.setText(res + "\n" + "onStart");
}
}

strings.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<resources>
<string name="app_name">practical8</string>
<string name="activity_lifecycle">"Activity Lifecycle</string>
<string name="fragment_lifecycle">"Fragment Lifecycle</string>
</resources>

Output 8 :
Question 9 :

Create an application with 2 fragments, one to set the background and other to set the fore-color
of the text.

Solution 9 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="10dp"
android:text="FRAGMENT EXAMPLE APP"
android:textColor="@color/black" />

<fragment
android:id="@+id/frag1"
android:name="com.example.practical9.BGColor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />

<fragment
android:id="@+id/frag2"
android:name="com.example.practical9.FGColor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />

</LinearLayout>
MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical9;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

TextView tv1;
TextView tv2;
TextView tv3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textview);
tv2 = (TextView) findViewById(R.id.textview2);
tv3 = (TextView) findViewById(R.id.textview3);
LinearLayout ll = (LinearLayout) findViewById(R.id.main_layout);
RadioGroup rg = (RadioGroup) findViewById(R.id.bg_radiogroup);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
RadioButton checkedRadioButton = (RadioButton)
radioGroup.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked) {
switch (checkedRadioButton.getId()) {
case R.id.radioButton1:

ll.setBackgroundColor(getResources().getColor(R.color.purple_500));
break;
case R.id.radioButton2:

ll.setBackgroundColor(getResources().getColor(R.color.teal_200));
break;
case R.id.radioButton3:

ll.setBackgroundColor(getResources().getColor(R.color.yellow));
break;
case R.id.radioButton4:

ll.setBackgroundColor(getResources().getColor(R.color.green));
break;
case R.id.radioButton5:

ll.setBackgroundColor(getResources().getColor(R.color.Red));
break;
}
}
}
});

RadioGroup rg2 = (RadioGroup) findViewById(R.id.fg_radiogroup);


rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
RadioButton checkedRadioButton = (RadioButton)
radioGroup.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked) {
switch (checkedRadioButton.getId()) {
case R.id.rb1:

tv1.setTextColor(getResources().getColor(R.color.green));

tv2.setTextColor(getResources().getColor(R.color.green));

tv3.setTextColor(getResources().getColor(R.color.green));
break;
case R.id.rb2:

tv1.setTextColor(getResources().getColor(R.color.purple_700));

tv2.setTextColor(getResources().getColor(R.color.purple_700));

tv3.setTextColor(getResources().getColor(R.color.purple_700));
break;
case R.id.rb3:

tv1.setTextColor(getResources().getColor(R.color.yellow));
tv2.setTextColor(getResources().getColor(R.color.yellow));

tv3.setTextColor(getResources().getColor(R.color.yellow));
break;
case R.id.rb4:

tv1.setTextColor(getResources().getColor(R.color.white));

tv2.setTextColor(getResources().getColor(R.color.white));

tv3.setTextColor(getResources().getColor(R.color.white));
break;
case R.id.rb5:

tv1.setTextColor(getResources().getColor(R.color.Red));
tv2.setTextColor(getResources().getColor(R.color.Red));
tv3.setTextColor(getResources().getColor(R.color.Red));
break;
}
}
}
});
}
}

fragment_bg_color.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:text="SELECT BACKGROUND COLOUR"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />

<RadioGroup
android:id="@+id/bg_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Purple" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Teal" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Yellow" />

<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Green" />

<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Red" />
</RadioGroup>
</LinearLayout>
BGColor.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical9;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BGColor extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_bg_color,
container, false);

fragment_fg_color.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview3"
android:layout_width="397dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:text="CHANGE TEXT COLOUR"
android:textAlignment="center"
android:textColor="@color/black"
android:textStyle="bold" />

<RadioGroup
android:id="@+id/fg_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Green" />

<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Purple" />

<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Yellow" />

<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="White" />

<RadioButton
android:id="@+id/rb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Red" />
</RadioGroup>
</LinearLayout>

FGColor.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical9;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FGColor extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fg_color,
container, false);

colors.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="yellow">#FFFFFF00</color>
<color name="magenta">#FFFF00FF</color>
<color name="Red"> #ff0000</color>
<color name="green">#7fff00 </color>
<color name="blue">#ADD8E6</color>
</resources>

strings.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<resources>
<string name="app_name">Question_9</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
Output 9 :
Question 10 :

Create an application with an activity having EditText and a button (with name “Send”). On clicking
the Send button, make use of implicit intent that uses a Send Action and let the user select the
app from app chooser and navigate to that application.

Solution 10 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="80dp"
android:layout_marginRight="80dp"
android:layout_marginLeft="25dp"
android:ems="10">
<requestFocus />
</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginLeft="0dp"
android:layout_marginTop="20dp"
android:ems="10">

<requestFocus />
</EditText>

<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textMultiLine" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="15dp"
android:text="To :" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="15dp"
android:text="Subject :" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentLeft="true"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="15dp"
android:text="Message:" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="15dp"
android:text="Send" />
</RelativeLayout>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical10;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText editTextTo,editTextSubject,editTextMessage;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTo=(EditText)findViewById(R.id.editText1);
editTextSubject=(EditText)findViewById(R.id.editText2);
editTextMessage=(EditText)findViewById(R.id.editText3);

send=(Button)findViewById(R.id.button1);

send.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View arg0) {
String to=editTextTo.getText().toString();
String subject=editTextSubject.getText().toString();
String message=editTextMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client
:"));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menus_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

dimens.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="activity_horizontal_margin">10dp</dimen>
<dimen name="activity_vertical_margin">10dp</dimen>
</resources>
menus_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<menu 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"
tools:context="net.XXX.YYY.ZZZZ">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>

strings.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<resources>
<string name="app_name">practical10</string>
<string name="action_settings">"Action settings</string>
</resources>

Output 10 :
Question 11 :

Create a Login application. On successful login, use explicit intent to second activity displaying
welcome message (Welcome Username) to the user and a logout button. When user presses the
logout button, a dialog box with a message (“Are you sure you want to exit?”) and two buttons
(“Yes” and “No”) should appear to confirm logout. On “Yes” button click, go to login activity and on
“No”, stay on the same activity

Solution 11 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="251dp"
android:layout_height="567dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/textViewCounter"
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/login" />
<EditText
android:id="@+id/editLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
/>
<TextView
android:id="@+id/textViewCounter2"
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/password"
/>
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
/>
<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/submit" />
</LinearLayout>

Main_activity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical11;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.widget.Toast;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) this.findViewById(R.id.buttonSubmit);
EditText e1 = (EditText) findViewById(R.id.editLogin);
EditText e2 = (EditText) findViewById(R.id.editPassword);
Intent i = new Intent(this, Second_activity.class);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String text1 = e1.getText().toString();
String text2 = e2.getText().toString();
Log.v(text1, "login");
Log.v(text2, "password");
if (text1.equals("login") && text2.equals("password")) {

Toast.makeText(MainActivity.this, "Successful",
Toast.LENGTH_SHORT).show();
startActivity(i);
} else {

Toast.makeText(MainActivity.this, "Unsuccessful",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

activity_second.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="251dp"
android:layout_height="567dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/textViewCounter"
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/WelcomeUser" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/logout" />
</LinearLayout>
AndroidManifest.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical11">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Practical11">
<activity
android:name=".AndroidManifest"
android:exported="false" />
<activity
android:name=".Second_activity"
android:exported="false" />
<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>

Second_activity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical11;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class Second_activity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button b11 = (Button) this.findViewById(R.id.button2);
Intent k = new Intent(this, MainActivity.class);
AlertDialog.Builder ad = new AlertDialog.Builder(this);
b11.setOnClickListener(v -> {
ad.setTitle("Login App");
ad.setMessage("Do you want to logout");
ad.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface Dialog, int buttonID) {
startActivity(k);
return;
}
});
ad.setPositiveButton("No", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface Dialog, int buttonID) {
return;
}
});
ad.show();
});
}
}
Output 11 :
Question 12 :

Create an application for Broadcast sender and receivers.

Solution 12 :

BroadcastSender activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Broadcast Message"
android:onClick="onBroadcastSendBtnClicked"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
BroadcastSender MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical12;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
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 onBroadcastSendBtnClicked(View v){


Intent intent = new Intent();
intent.setAction("com.practical12.myBroadcastMessage");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
}
}

BroadcastReceiver activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Broadcasting custom intents"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
BroadcastReceiver MyBroadcastReceiver.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical12_broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.i("BroadcastReceiver", "Broadcast message is received");
Toast.makeText(context,"Broadcast message is received",
Toast.LENGTH_LONG).show();
}
}

BroadcastReceiver AndroidManifest.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?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:supportsRtl="true"
android:theme="@style/Theme.Practical12_BroadcastReceiver"
tools:targetApi="31">
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name = "com.practical12.myBroadcastMessage"></action>
</intent-filter>

</receiver>

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

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical12_broadcastreceiver;
import androidx.appcompat.app.AppCompatActivity;

import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

IntentFilter intentFilter = new


IntentFilter("com.practical12.myBroadcastMessage");
MyBroadcastReceiver objReceiver = new MyBroadcastReceiver();
registerReceiver(objReceiver,intentFilter);
}
}
Output 12 :
Question 13 :

Create an application to create a notification having icon, text and title.

Solution 13 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


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

<Button
android:id="@+id/notifyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get notification"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>l̥

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical13;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button notifyBtn;

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

notifyBtn = findViewById(R.id.notifyBtn);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


NotificationChannel channel = new NotificationChannel("NyNotification",
"NyNotification", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager =
getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}

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

NotificationCompat.Builder builder = new


NotificationCompat.Builder(MainActivity.this, "NyNotification");
builder.setContentTitle("Sample title");
builder.setContentText("This is sample text for Practical 13");
builder.setSmallIcon(R.drawable.notification_icon);
builder.setAutoCancel(true);

NotificationManagerCompat managerCompat =
NotificationManagerCompat.from(MainActivity.this);
if (ActivityCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.POST_NOTIFICATIONS) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
managerCompat.notify(1, builder.build());

}
});
}
}
Output 13 :

Question 14 :

Create an application to create services.

Solution 14 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">

<Button
android:id="@+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:text="Start Service" />

<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Stop Service" />

<Button
android:id="@+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:text="Next Page" />
</RelativeLayout>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical14;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
Button buttonStart, buttonStop,buttonNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buttonStart = findViewById(R.id.buttonStart);
buttonStop = findViewById(R.id.buttonStop);
buttonNext = findViewById(R.id.buttonNext);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonNext.setOnClickListener(this);

}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:

startService(new Intent(this, MyService.class));


break;
case R.id.buttonStop:
stopService(new Intent(this, MyService.class));
break;
case R.id.buttonNext:
Intent intent=new Intent(this,NextPage.class);
startActivity(intent);
break;
}
}
}

NextPage.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical14;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class NextPage extends AppCompatActivity {

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

activity_next.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
tools:context=".NextPage">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="200dp"
android:text="Next Page"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

MyService.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical14;

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

import androidx.annotation.Nullable;

public class MyService extends Service {


MediaPlayer myPlayer;

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

@Override
public void onCreate() {
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

myPlayer = MediaPlayer.create(this, R.raw.earth);


myPlayer.setLooping(false); // Set looping
}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
myPlayer.start();
}

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

AndroidManifest.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical14">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light">
<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>
<activity android:name=".NextPage"></activity>

<service
android:name=".MyService"
android:enabled="true" />
</application>
</manifest>
Output 14 :
Question 15 :

Create an application to Create, Insert, update, Delete and retrieve operations on the database.

Solution 15 :

activity_main.xml

<!--Created by - DHANANJAY SINGHAL 21CS/13 -->


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter id to update or delete"
android:id="@+id/id"
android:onClick="buttonAction"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter task to insert or update"
android:id="@+id/task" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/insert"
android:text="Insert"
android:onClick="buttonAction"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/view"
android:text="View"
android:onClick="buttonAction" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update"
android:onClick="buttonAction"
android:id="@+id/update" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete"
android:onClick="buttonAction"
android:id="@+id/delete" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"/>
</LinearLayout>

MainActivity.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical15;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText id,task;
Button insert,view,update,delete;
TextView text;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
id=(EditText)findViewById(R.id.id);
task=(EditText)findViewById((R.id.task));
insert=(Button)findViewById(R.id.insert);
view=(Button)findViewById(R.id.view);
update=(Button)findViewById(R.id.update);
delete=(Button)findViewById(R.id.delete);
text=(TextView)findViewById(R.id.text);
db=new DatabaseHelper(getApplicationContext());
}
public void buttonAction(View view){
switch (view.getId()){
case R.id.insert:
db.insertRecord(task.getText().toString());
Toast.makeText(getApplicationContext(),"record
inserted",Toast.LENGTH_LONG).show();
break;
case R.id.view:
text.setText(db.getRecords());
break;
case R.id.update:

db.updateRecord(id.getText().toString(),task.getText().toString());
Toast.makeText(getApplicationContext(),"record
updated",Toast.LENGTH_LONG).show();
break;
case R.id.delete:
db.deleteRecord(id.getText().toString());
Toast.makeText(getApplicationContext(),"record
deleted",Toast.LENGTH_LONG).show();
break;
}
}
}

DatabaseHelper.java

//Created by - DHANANJAY SINGHAL 21CS/13


package com.example.practical15;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
private static final String DB_NAME="demodb";
private static final int DB_VERSION=1;
private static final String TABLE_NAME="record";
private static final String ID_COL="id";
private static final String TASK_COL="task";
public DatabaseHelper(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,"+TASK_COL+" TEXT)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create table again
onCreate(db);
}
public void insertRecord(String name){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(TASK_COL,name);
db.insert(TABLE_NAME,null,values);
db.close();
}
public String getRecords(){
String query="SELECT * FROM "+TABLE_NAME;
String result="";
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery(query,null);
cursor.moveToFirst();
while(cursor.isAfterLast()==false){
result+=cursor.getString(0)+" "+cursor.getString(1)+"\n";
cursor.moveToNext();
}
db.close();
return result;
}
public void updateRecord(String id,String task){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(TASK_COL,task);
db.update(TABLE_NAME,values,"id=?",new String[]{id});
db.close();
}
public void deleteRecord(String id){
SQLiteDatabase db=this.getWritableDatabase();
db.delete(TABLE_NAME,"id=?",new String[]{id});
db.close();
}
}
Output 15 :
Name - DHANANJAY SINGHAL Roll No - 21CS/13

END OF ASSIGNMENT

You might also like