0% found this document useful (0 votes)
100 views102 pages

Certificate: J.N.T.U.H. University College of Engineering, Science & Technology Hyderabad

Uploaded by

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

Certificate: J.N.T.U.H. University College of Engineering, Science & Technology Hyderabad

Uploaded by

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

J.N.T.U.H.

UNIVERSITY COLLEGE OF ENGINEERING, SCIENCE &


TECHNOLOGY HYDERABAD

KUKATPALLY, HYDERABAD – 500 085

Certificate
Certified that this is the bonafide record of the practical work done during

the academic year by

Name

Roll Number Class

in the Laboratory of _

of the Department of

Signature of the Staff Member Signature of the Head of the Department

Date of Examination_

Signature of the Examiner/s

Internal Examiner External Examiner


J.N.T.U.H. UNIVERSITY COLLEGE OF ENGINEERING, SCIENCE &
TECHNOLOGY HYDERABAD

KUKATPALLY, HYDERABAD – 500 085

Name Roll Number

Class Year _ Laboratory

List of Experiments
S.No. Name of the Experiment Date of Page Marks Remarks
Experiment Number
1. a) Create an Android application that shows Hello + name
of theuser and run it on an emulator.
MainActivity.java

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

import
androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends


AppCompatActivity { @Override

protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Assuming you have a TextView with the id 'textView' in


your layout String username = "Sampath";

TextView textView = findViewById(R.id.textView);


textView.setText("Hello, " + username + "!");

}
activity_main.xml

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

android:id="@+id/textView"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Hello,
World!"

android:textSize="24sp"
android:layout_centerInParent="t
rue"/>

</RelativeLayout>
1b) Create an application that takes the name from a text box and shows
hello message along with the name entered in text box, when the user
clicks the OK button.

activity_main.xml
<?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_p
arent"
tools:context=".MainActivity">
<EditText

android:id="@+id/
editTextName"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Enter your
name"
android:layout_marginTop="16d
p"
android:layout_marginLeft="16d
p"
android:layout_marginRight="16
dp"/>

<Button

android:id="@+id/butto

nOK"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="OK"

android:layout_below="@id/
editTextName"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:onClick="showHelloMessage
"/>
<TextView

android:id="@+id/
textViewMessage"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text=""

android:layout_below="@id/
buttonOK"
android:layout_marginTop="16dp
"
android:layout_marginLeft="16d
p"/>

</RelativeLayout>

MainActivity.java

package
com.example.hellonameapp;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;

import android.widget.EditText;
import android.widget.TextView;
import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity { private EditText
editTextName;

private TextView
textViewMessage; @Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editTextName =

findViewById(R.id.editTextName);

textViewMessage = findViewById(R.id.textViewMessage);

}
public void showHelloMessage(View view)
{ String name =
editTextName.getText().toString(); if (!
name.isEmpty()) {

String helloMessage = "Hello, " +


name + "!";
textViewMessage.setText(helloMessag
e);

}}
2. Create a screen that has input boxes for User Name, Password,
Address, Gender (radio buttons for male and female), Age
(numeric), Date of Birth (Date Picket), State (Spinner) and a
Submit button. On clicking the submit button, print all the data
below the Submit Button. Use (a) Linear Layout (b) Relative
Layout and (c) Grid Layout or Table Layout.(any one of the
layouts).
Linear layout:

MainActivity.java

package
com.example.userinputlayouts;
import android.os.Bundle;
import android.view.View;

import
android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import
android.widget.LinearLayout;
import
android.widget.RadioButton;
import
android.widget.RadioGroup;
import android.widget.Spinner;

import android.widget.TextView;

import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {

private EditText editTextUserName, editTextPassword, editTextAddress,


editTextAge;
private RadioButton radioButtonMale,
radioButtonFemale; private DatePicker datePicker;

private Spinner spinnerState;

private TextView
textViewResult; @Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_linear);

editTextUserName =
findViewById(R.id.editTextUserName);
editTextPassword =
findViewById(R.id.editTextPassword);
editTextAddress =
findViewById(R.id.editTextAddress); editTextAge =
findViewById(R.id.editTextAge); radioButtonMale =
findViewById(R.id.radioButtonMale);

radioButtonFemale = findViewById(R.id.radioButtonFemale); datePicker


= findViewById(R.id.datePicker);
spinnerState = findViewById(R.id.spinnerState);
textViewResult =
findViewById(R.id.textViewResult); setupSpinner();
}
private void setupSpinner() {

ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource( this,
R.array.states_array,

android.R.layout.simple_spinner_

item

);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropd

o wn_ item);

spinnerState.setAdapter(adapter);

}
public void onSubmitClick(View view) {
String userName =
editTextUserName.getText().toString(); String
password = editTextPassword.getText().toString();
String address =
editTextAddress.getText().toString(); String age =
editTextAge.getText().toString();

String gender = radioButtonMale.isChecked() ? "Male" :


"Female"; int day = datePicker.getDayOfMonth();

int month = datePicker.getMonth() + 1; // Months are 0-


indexed int year = datePicker.getYear();

String dateOfBirth = String.format("%02d-%02d-%04d", day, month, year);

String state =
spinnerState.getSelectedItem().toString(); String
result = "User Name: " + userName + "\
nPassword: " + password +

"\nAddress: " +
address + "\nAge: " +
age + "\nGender: " +
gender +

"\nDate of Birth: " +


dateOfBirth + "\nState: " +
state;

textViewResult.setText(result);

}
}
res/values/strings.xml

<resources>

<string-array name="states_array">

<item>State 1</item>

<item>State 2</item>

<item>State 3</item>
<!-- Add more states as needed -->
</string-array>

</resources>

res/layout/activity_main_linea

r.xml

<?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_p
arent"
android:orientation="vertical"

android:padding="16dp">

<EditText

android:id="@+id/
editTextUserName"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="User
Name"/>

<EditText

android:id="@+id/
editTextPassword"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Password"
android:inputType="textPasswor
d"/>
<EditText

android:id="@+id/
editTextAddress"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Address"/>

<EditText
android:id="@+id/editTextAge"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Age"
android:inputType="number"/>

<RadioGroup

android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"

android:orientation="horizontal">

<RadioButton

android:id="@+id/
radioButtonMale"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Male"/>

<RadioButton

android:id="@+id/
radioButtonFemale"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Female"/>

</RadioGroup>

<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_par
ent"
android:layout_height="wrap_cont
ent"/>

<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_par
ent"
android:layout_height="wrap_cont
ent"/>

<Button

android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Submit"
android:onClick="onSubmitClick"
android:layout_gravity="center"/
>

<TextView

android:id="@+id/
textViewResult"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:layout_marginTop="16d
p"/>

</LinearLayout>

b) Relative layout:

MainActivity.java

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

import android.view.View;
import
android.widget.ArrayAdapter;
import android.widget.Button;

import android.widget.DatePicker;
import android.widget.EditText;

import android.widget.RadioButton;
import
android.widget.RelativeLayout;
import android.widget.Spinner;

import android.widget.TextView;

import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {
private EditText editTextUserName, editTextPassword, editTextAddress,
editTextAge;

private RadioButton radioButtonMale,


radioButtonFemale; private DatePicker datePicker;

private Spinner spinnerState;

private TextView
textViewResult; @Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_relative);

editTextUserName =
findViewById(R.id.editTextUserName); editTextPassword
= findViewById(R.id.editTextPassword); editTextAddress
= findViewById(R.id.editTextAddress); editTextAge =
findViewById(R.id.editTextAge); radioButtonMale =
findViewById(R.id.radioButtonMale); radioButtonFemale =
findViewById(R.id.radioButtonFemale); datePicker =
findViewById(R.id.datePicker);

spinnerState = findViewById(R.id.spinnerState);
textViewResult =
findViewById(R.id.textViewResult); setupSpinner();
}

private void setupSpinner() {


ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource( this,
R.array.states_array,

android.R.layout.simple_spinner_

item

);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropd

o wn_ item);

spinnerState.setAdapter(adapter);

public void onSubmitClick(View view) {

String userName =
editTextUserName.getText().toString(); String
password = editTextPassword.getText().toString();
String address =
editTextAddress.getText().toString(); String age =
editTextAge.getText().toString();

String gender = radioButtonMale.isChecked() ? "Male" :


"Female"; int day = datePicker.getDayOfMonth();

int month = datePicker.getMonth() + 1; // Months are 0-


indexed int year = datePicker.getYear();

String dateOfBirth = String.format("%02d-%02d-%04d", day, month,


year); String state = spinnerState.getSelectedItem().toString();

String result = "User Name: " +


userName + "\nPassword: " +
password +

"\nAddress: " +
address + "\nAge: " +
age + "\nGender: " +
gender +
"\nDate of Birth: " +
dateOfBirth + "\nState: " +
state;

textViewResult.setText(result);

}
}
res/layout/activity_main_relative.xml

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

android:padding="16dp">

<EditText

android:id="@+id/
editTextUserName"
android:layout_width="match_par
ent"
android:layout_height="wrap_con
tent" android:hint="User Name"
android:layout_alignParentTop="tr
ue"/>

<EditText

android:id="@+id/
editTextPassword"
android:layout_width="match_p
arent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_below="@id/editTextUser
Name"
android:layout_marginTop="16dp"
android:inputType="textPassword"/>
<EditText

android:id="@+id/
editTextAddress"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Address"

android:layout_below="@id/
editTextPassword"
android:layout_marginTop="16dp"/>

<EditText

android:id="@+id/editText

Age"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:layout_below="@id/editTextAd
dress"
android:layout_marginTop="16dp"
android:inputType="number"/>

<RadioGroup

android:id="@+id/
radioGroupGender"
android:layout_width="match_pare
nt"
android:layout_height="wrap_cont
ent"
android:orientation="horizontal"
android:layout_below="@id/editTex
tAge"
android:layout_marginTop="16dp"
>

<RadioButton
android:id="@+id/
radioButtonMale"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Male"/>

<RadioButton

android:id="@+id/
radioButtonFemale"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Female"/>

</RadioGroup>

<DatePicker

android:id="@+id/datePi

cker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroupG
ender"
android:layout_marginTop="16dp"/>

<Spinner

android:id="@+id/spinnerState"
android:layout_width="match_par
ent"
android:layout_height="wrap_cont
ent"
android:layout_below="@id/datePi
cker"
android:layout_marginTop="16dp"
/>

<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Submit"

android:onClick="onSubmitClick"
android:layout_below="@id/spinner
State"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="tr
ue"/>

<TextView

android:id="@+id/
textViewResult"
android:layout_width="match_p
arent"

android:layout_height="wrap_conten
t"
android:layout_below="@id/buttonS
ubmit"
android:layout_marginTop="16dp"/>

</RelativeLayout>

c) Grid Layout:

MainActivity.java

package
com.example.userinputlayouts;
import android.os.Bundle;
import android.view.View;

import
android.widget.ArrayAdapter;
import android.widget.Button;

import android.widget.DatePicker;
import android.widget.EditText;

import android.widget.GridLayout;
import
android.widget.RadioButton;
import
android.widget.Spinner;

import android.widget.TextView;

import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {
private EditText editTextUserName, editTextPassword, editTextAddress,
editTextAge;

private RadioButton radioButtonMale,


radioButtonFemale; private DatePicker datePicker;
private Spinner spinnerState;

private TextView
textViewResult; @Override

protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_grid);

editTextUserName =
findViewById(R.id.editTextUserName); editTextPassword
= findViewById(R.id.editTextPassword); editTextAddress
= findViewById(R.id.editTextAddress); editTextAge =
findViewById(R.id.editTextAge); radioButtonMale =
findViewById(R.id.radioButtonMale); radioButtonFemale =
findViewById(R.id.radioButtonFemale); datePicker =
findViewById(R.id.datePicker);

spinnerState = findViewById(R.id.spinnerState);
textViewResult =
findViewById(R.id.textViewResult); setupSpinner();

}
private void setupSpinner() {
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource( this,
R.array.states_array,

android.R.layout.simple_spinner_

item

);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropd

o wn_ item);

spinnerState.setAdapter(adapter);

}
public void onSubmitClick(View view) {

String userName = editTextUserName.getText().toString();

String password =
editTextPassword.getText().toString(); String
address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();

String gender = radioButtonMale.isChecked() ? "Male" :


"Female"; int day = datePicker.getDayOfMonth();

int month = datePicker.getMonth() + 1; // Months are 0-


indexed int year = datePicker.getYear();

String dateOfBirth = String.format("%02d-%02d-%04d", day, month,


year); String state = spinnerState.getSelectedItem().toString();

String result = "User Name: " +


userName + "\nPassword: " +
password +

"\nAddress: " +
address + "\nAge: " +
age + "\nGender: " +
gender +
"\nDate of Birth: " +
dateOfBirth + "\nState: " +
state;

textViewResult.setText(result);

}
}
res/layout/activity_main_grid.xml

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

<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"

android:layout_height="match_p
arent" android:columnCount="2"

android:rowCount="10"

android:padding="16dp">

<EditText

android:id="@+id/
editTextUserName"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="User Name"
android:layout_columnSpan="2"/
>

<EditText

android:id="@+id/
editTextPassword"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Password"
android:layout_columnSpan="2
"
android:inputType="textPassw
ord"/>
<EditText

android:id="@+id/
editTextAddress"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Address"
android:layout_columnSpan="2"/
>

<EditText

android:id="@+id/editTextAge"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"

android:hint="Age"
android:layout_columnSpan
="2"
android:inputType="numbe
r"/>

<RadioGroup

android:id="@+id/
radioGroupGender"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:orientation="horizontal"
android:layout_columnSpan="2"
>

<RadioButton

android:id="@+id/
radioButtonMale"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Male"/>
<RadioButton

android:id="@+id/
radioButtonFemale"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Female"/>

</RadioGroup>

<DatePicker

android:id="@+id/datePi

cker"

android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:layout_columnSpan="2"/
>

<Spinner

android:id="@+id/spinnerS

tate"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:layout_columnSpan="2"/
>
<Button

android:id="@+id/buttonSubmit"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Submit"
android:onClick="onSubmitC
lick"
android:layout_gravity="cen
ter"/>

<TextView

android:id="@+id/
textViewResult"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:layout_columnSpan="2"/
>

</GridLayout>
3. Develop an application that shows names as a list and on
selecting a name it should show the details of the candidate
on thenext screen with a “Back” button.

MainActivity.java:

package
com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;

import
androidx.appcompat.app.AppCompatActivity;
import android.view.View;

import
android.widget.AdapterView;
import
android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

ListView nameListView;

String[] names = {"Candidate 1", "Candidate 2", "Candidate 3"};

@Override

protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceSt
ate);
setContentView(R.layout.activity_main);
nameListView = findViewById(R.id.nameListView);

ArrayAdapter<String> adapter = new


ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, names);

nameListView.setAdapter(adapter);

nameListView.setOnItemClickListener(new

AdapterView.OnItemClickListener() { @Override

public void onItemClick(AdapterView<?> parent, View view, int


id) position, long
{

String selectedName = names[position];


String[] details =

getCandidateDetails(selectedName); Intent

intent = new Intent(MainActivity.this,

CandidateDetailsActivity.class);

});
}

intent.putExtra("name",
selectedName);
intent.putExtra("rollNo",
details[0]); intent.putExtra("age",
details[1]);
intent.putExtra("experience",
details[2]); startActivity(intent);
// Method to get details of a candidate

private String[] getCandidateDetails(String


name) { String[] details = new String[3];
// Sample data - Replace with actual data
retrieval logic if(name.equals("Candidate 1")) {
details[0] = "101"; // Roll
Number details[1] = "25"; //
Age

details[2] = "3 years"; // Experience

} else if (name.equals("Candidate
2")) { details[0] = "102";
details[1] = "28";

details[2] = "5

years";

} else if (name.equals("Candidate
3")) { details[0] = "103";

details[1] = "30";

details[2] = "7 years";

return details;

}
}
Candidatedetailsactivity.java:

package
com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import
androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import

android.widget.Button;

import

android.widget.TextView;

public class CandidateDetailsActivity extends AppCompatActivity {

TextView nameTextView, rollNoTextView, ageTextView,


experienceTextView; Button backButton;

@Override

protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceSt
ate);

setContentView(R.layout.activity_candidate_details);

nameTextView =
findViewById(R.id.nameTextView);
rollNoTextView =
findViewById(R.id.rollNoTextView);
ageTextView =
findViewById(R.id.ageTextView);
experienceTextView = findViewById(R.id.experienceTextView);

backButton = findViewById(R.id.backButton);
Intent intent =
getIntent(); if (intent !
= null) {
String name =
intent.getStringExtra("name"); String
rollNo = intent.getStringExtra("rollNo");
String age =
intent.getStringExtra("age");

String experience = intent.getStringExtra("experience");

nameTextView.setText(name);

rollNoTextView.setText("Roll No: " + rollNo);


ageTextView.setText("Age: " + age);
experienceTextView.setText("Experience: " +
experience);

backButton.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View
v) { finish();

});
}
}
Activity_main.xml:

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

<ListView

android:id="@+id/nameListView"
android:layout_width="match_pa
rent"
android:layout_height="match_p
arent"
android:padding="16dp" />

</RelativeLayout>

Activity_candidatedet

ails:

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

<RelativeLayout

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

android"
android:layout_width="match_p
arent"
android:layout_height="match_par
ent">

<TextView

android:id="@+id/
nameTextView"
android:layout_width="wrap_cont
ent"
android:layout_height="wrap_con
tent" android:textSize="24sp"
android:layout_centerHorizontal=
"true"
android:layout_marginTop="32dp
"/>

<TextView
android:id="@+id/rollNoTextView"

android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_below="@id/nameText
View"
android:layout_centerHorizontal="tru
e"
android:layout_marginTop="16dp"/>

<TextView

android:id="@+id/ageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_below="@id/rollNoText
View"
android:layout_centerHorizontal="tru
e"
android:layout_marginTop="16dp"/>

<TextView

android:id="@+id/
experienceTextView"
android:layout_width="wrap_conten
t"
android:layout_height="wrap_conte
nt"
android:layout_below="@id/ageText
View"
android:layout_centerHorizontal="tr
ue"
android:layout_marginTop="16dp"/
>

<Button

android:id="@+id/backButton"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent"
android:layout_below="@id/
experienceTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"

android:text="Back"/>

</RelativeLayout>
4. Develop an application that uses a menu with 3 options for
dialing a number, opening a website and to send an SMS. On
selecting an option, the appropriate action should be invoked
using intents.

MainActivity.java
package

com.example.myapplication;

import android.os.Bundle;

import android.content.Intent; import

android.net.Uri; import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


implements View.OnClickListener {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceStat

e);

setContentView(R.layout.activity_

main);

Button dialButton = findViewById(R.id.dial_button);

Button websiteButton = findViewById(R.id.website_button);


Button smsButton = findViewById(R.id.sms_button);

dialButton.setOnClickListener(this)
;
websiteButton.setOnClickListener(
this);
smsButton.setOnClickListener(this
);
}
public void
onClick(View v) {
int id = v.getId();
if (id == R.id.dial_button) {

// Dial a number

Intent dialIntent = new Intent(Intent.ACTION_DIAL);

dialIntent.setData(Uri.parse("tel:123456789")); // Replace with your

phone
number

startActivity(dialIntent);

} else if (id == R.id.website_button) {

// Open a website

Intent websiteIntent = new Intent(Intent.ACTION_VIEW);

websiteIntent.setData(Uri.parse("https://www.example.com")); //
Replace with your website
URL
startActivity(websiteInt
ent);

} else if (id == R.id.sms_button) {

// Send an SMS

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);

smsIntent.setData(Uri.parse("smsto:")); // This ensures only SMS

apps
respond

smsIntent.putExtra("sms_body", "Hello, this is a test


message!"); // Replace with your message

startActivity(smsIntent);

}
}
}

activity_main.xml
<?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:layout_width="match_pa
rent"
android:layout_height="match_p
arent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
tools:context=".MainActivity">

<Button

android:id="@+id/dial_button"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Dial a
Number" />

<Button

android:id="@+id/
website_button"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Open
Website" />

<Button
android:id="@+id/sms_button"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Send
SMS" />

</
LinearLayout
>
5. Develop an application to implement broadcast receivers.

Mainactivity.java:

package

com.example.myapplication;

import android.app.Activity;

import android.content.ComponentName;

import
android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends

Activity { @Override

protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceSt
ate);

setContentView(R.layout.activity_main);

// Broadcast a custom intent.

public void broadcastIntent(View


view){ Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");

intent.setComponent(new ComponentName(getPackageName(),
MyReceiver.class.getName())); // Set the
component explicitly
sendBroadcast(intent);
}
}
Myreceiver.java:

package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.widget.Toast;

/**

* Created by TutorialsPoint7 on 8/23/2016.

*/
public class MyReceiver extends
BroadcastReceiver{ @Override

public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Intent Detected.",


Toast.LENGTH_LONG).show();

}
}
Activity_main.xml:

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

<TextView

android:id="@+id/textView1"
android:layout_width="wrap_cont
ent"
android:layout_height="wrap_con
tent" android:text="Example of
Broadcast"
android:layout_alignParentTop="t
rue"
android:layout_centerHorizontal=
"true"

android:textSize="30dp" />

<Button

android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Broadcast Intent"
android:onClick="broadcastIntent"
android:layout_below="@id/textView1"
android:layout_centerHorizontal="true" />

</RelativeLayout>
Manifest.xml:

<?xml version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/androi
d" xmlns:tools="http://schemas.android.com/tools">
<application

android:allowBackup="t

rue"

android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/
backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/Theme.MyApplication"

tools:targetApi="31">

<activity
android:name=".MainActi
vity"
android:exported="true"
>
<intent-filter>

<action android:name="android.intent.action.MAIN" />

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

</intent-filter>

</activity>

<receiver
android:name="MyReceiver"
android:exported="true">
<intent-filter>

<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>

</intent-filter>

</receiver>

</application>
</manifest>
6. Create an application that uses a text file to store
usernames ans passwords.When the user submits a login
name and password through a screen, the details should be
verified with the text file data and ifthey match, show a dialog
saying that login is successful.Otherwise, show the dialog with
Login Failed message.

package

com.example.myapplication;

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

import android.widget.Button;

import
android.widget.EditText;
import
android.widget.Toast;

import
androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;

import
java.io.IOException;
import
java.io.InputStream;

import java.io.InputStreamReader;

public class MainActivity extends


AppCompatActivity { EditText
editTextUsername, editTextPassword; Button
buttonLogin;

@Override

protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceSt
ate);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);

editTextPassword =
findViewById(R.id.editTextPassword); buttonLogin
= findViewById(R.id.buttonLogin);

buttonLogin.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View v) {

String username =
editTextUsername.getText().toString(); String
password =
editTextPassword.getText().toString();

if (validateCredentials(username, password)) {

Toast.makeText(MainActivity.this, "Login Successful!",

Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(MainActivity.this, "Login Failed. Please check


your username and password.", Toast.LENGTH_SHORT).show();

}
}
});
}

private boolean validateCredentials(String username, String


password) { try {
InputStream inputStream =
getResources().getAssets().open("user_credential
s.txt");
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream));
String line;

while ((line = reader.readLine()) !=


null) { String[] parts = line.split("\
t");
if (parts.length == 2) {

String storedUsername =
parts[0]; String
storedPassword = parts[1];
if
(storedUsername.equals(username) &&
storedPassword.equals(password)) {

reader.close
(); return
true;
}
}
}
reader.close();

} catch (IOException
e) {
e.printStackTrace()
;
}
return false;

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

android:id="@+id/
editTextUsername"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Username"
android:layout_marginTop="50d
p"

android:layout_marginHorizontal="20dp"/>

<EditText

android:id="@+id/
editTextPassword"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent" android:hint="Password"
android:inputType="textPasswor
d"

android:layout_below="@id/editTextUsername"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"/>

<Button

android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/editTextPass
word"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>

</RelativeLayout>
7. Create a user details application that stores the user details
in database table

Mainactivity.java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity; import

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

import android.widget.Button;

import
android.widget.EditText;
import
android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText userNameEdt, userEmailEdt,


userPasswordEdt; private Button registerBtn;
private DBHandler dbHandler;

@Override

protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceSt
ate);

setContentView(R.layout.activity_main);
userNameEdt = findViewById(R.id.idEdtUserName);

userEmailEdt = findViewById(R.id.idEdtUserEmail);

userPasswordEdt =
findViewById(R.id.idEdtUserPassword); registerBtn =
findViewById(R.id.idBtnRegister);

dbHandler = new DBHandler(MainActivity.this);

registerBtn.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View v) {

String userName =
userNameEdt.getText().toString(); String userEmail
= userEmailEdt.getText().toString();

String userPassword = userPasswordEdt.getText().toString();

if (userName.isEmpty() || userEmail.isEmpty() ||

userPassword.isEmpty()) { Toast.makeText(MainActivity.this,

"Please enter all the data..",


Toast.LENGTH_SHORT).show

(); return;

dbHandler.addNewUser(userName, userEmail, userPassword);


Toast.makeText(MainActivity.this, "User registered
successfully.", Toast.LENGTH_SHORT).show();

userNameEdt.setText(""
);
userEmailEdt.setText("")
;
userPasswordEdt.setText("");

}
});
}
}
Activity_mainxml:

<?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:layout_width="match_pa
rent"
android:layout_height="match_p
arent"
android:orientation="vertical"

tools:context=".MainActivity">

<EditText

android:id="@+id/
idEdtUserName"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:layout_margin="10dp"
android:hint="Enter Username"
/>
<EditText

android:id="@+id/
idEdtUserEmail"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:layout_margin="10dp"
android:hint="Enter Email" />

<EditText

android:id="@+id/
idEdtUserPassword"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent"
android:layout_margin="10dp"
android:hint="Enter Password"
android:inputType="textPasswor
d" />

<Button

android:id="@+id/idBtnRegister"
android:layout_width="match_pa
rent"
android:layout_height="wrap_co
ntent"
android:layout_margin="10dp"
android:text="Register"

android:textAllCaps="false" />
</
LinearLayout
>
DBHandler.jav
a:

package com.example.myapplication;
import
android.content.ContentValues;
import android.content.Context;

import

android.database.sqlite.SQLiteDatabase;

import

android.database.sqlite.SQLiteOpenHelper;

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";
private static final String NAME_COL =
"name"; private static final String
EMAIL_COL
= "email";
private static final String PASSWORD_COL = "password";

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

+ EMAIL_COL + " TEXT,"

+ PASSWORD_COL + " TEXT)";

db.execSQL(query);

public void addNewUser(String userName, String


userEmail, String userPassword) {

SQLiteDatabase db =
this.getWritableDatabase(); ContentValues
values = new ContentValues();
values.put(NAME_COL, userName);
values.put(EMAIL_COL, userEmail);
values.put(PASSWORD_COL, userPassword);
db.insert(TABLE_NAME, null, values);

db.close();

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int


newVersion) { db.execSQL("DROP TABLE IF EXISTS " +
TABLE_NAME);
onCreate(db);

}
}
8. Develop an application that inserts some notifications into Notification area
and whenever a notification is inserted, it should show a toast with details of
the notification.

package
com.example.myapplication;
import android.app.Notification;
import
android.app.NotificationChannel;
import
android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import
android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import android.widget.RemoteViews;
import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {
// declaring variables
private NotificationManager
notificationManager; private
Notification.Builder builder;
private NotificationChannel
notificationChannel; private final String
channelId = "i.apps.notifications"; private
final String description = "Test notification";
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// accessing button
Button btn = findViewById(R.id.btn);
// it is a class to notify the user of events that happen.
// This is how you tell the user that something has happened in the
// background.
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVI
CE);
// onClick listener for the button
btn.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View v) {
// pendingIntent is an intent for future use i.e after
// the notification is clicked, this intent will come into action
Intent intent = new Intent(MainActivity.this, afterNotification.class);
// FLAG_UPDATE_CURRENT specifies that if a previous
// PendingIntent already exists, then the current one
// will update it with the latest intent
// 0 is the request code, using it later with the
// same method again will get back the same pending
// intent for future reference
// intent passed here is to our afterNotification class
PendingIntent pendingIntent =
PendingIntent.getActivity(MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// RemoteViews are used to use the content of
// some different layout apart from the current activity layout
RemoteViews contentView = new
RemoteViews(getPackageName(),
R.layout.activity_after_notification);
// checking if android version is greater than oreo(API
26) or not if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(channelId,
description, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationCh
annel); builder = new Notification.Builder(MainActivity.this,
channelId)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getReso
urces(), R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent);
} else {
builder = new Notification.Builder(MainActivity.this)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getReso
urces(), R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent);
}
notificationManager.notify(1234, builder.build());
}
});
}
}
Activity main:
<?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=".MainActivity">
<Button
android:id="@+id/
btn"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent"
android:layout_centerInParent="
true" android:text="Send
Notification" />
</RelativeLayout>
Afteractivity.java
package com.example.myapplication;
import
androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class afterNotification extends
AppCompatActivity { @Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_after_notificat
ion);
}
}
Activity_after_activity.xml
<?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=".afterNotification">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent"
android:layout_centerInParent="
true"
android:text="Welcome To
GeeksforGeeks"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>

You might also like