Certificate: J.N.T.U.H. University College of Engineering, Science & Technology Hyderabad
Certificate: J.N.T.U.H. University College of Engineering, Science & Technology Hyderabad
Certificate
Certified that this is the bonafide record of the practical work done during
Name
in the Laboratory of _
of the Department of
Date of Examination_
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;
setContentView(R.layout.activity_main);
}
activity_main.xml
<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()) {
}}
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 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);
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 state =
spinnerState.getSelectedItem().toString(); String
result = "User Name: " + userName + "\
nPassword: " + password +
"\nAddress: " +
address + "\nAge: " +
age + "\nGender: " +
gender +
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
<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 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();
}
android.R.layout.simple_spinner_
item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropd
o wn_ item);
spinnerState.setAdapter(adapter);
String userName =
editTextUserName.getText().toString(); String
password = editTextPassword.getText().toString();
String address =
editTextAddress.getText().toString(); String age =
editTextAge.getText().toString();
"\nAddress: " +
address + "\nAge: " +
age + "\nGender: " +
gender +
"\nDate of Birth: " +
dateOfBirth + "\nState: " +
state;
textViewResult.setText(result);
}
}
res/layout/activity_main_relative.xml
<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 TextView
textViewResult; @Override
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 password =
editTextPassword.getText().toString(); String
address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();
"\nAddress: " +
address + "\nAge: " +
age + "\nGender: " +
gender +
"\nDate of Birth: " +
dateOfBirth + "\nState: " +
state;
textViewResult.setText(result);
}
}
res/layout/activity_main_grid.xml
<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;
ListView nameListView;
@Override
nameListView.setAdapter(adapter);
nameListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() { @Override
getCandidateDetails(selectedName); Intent
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
} 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";
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;
@Override
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");
nameTextView.setText(name);
backButton.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View
v) { finish();
});
}
}
Activity_main.xml:
<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:
<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.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceStat
e);
setContentView(R.layout.activity_
main);
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
phone
number
startActivity(dialIntent);
// Open a website
websiteIntent.setData(Uri.parse("https://www.example.com")); //
Replace with your website
URL
startActivity(websiteInt
ent);
// Send an SMS
apps
respond
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;
Activity { @Override
setContentView(R.layout.activity_main);
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;
/**
*/
public class MyReceiver extends
BroadcastReceiver{ @Override
}
}
Activity_main.xml:
<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>
<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;
@Override
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.LENGTH_SHORT).show();
} else {
}
}
});
}
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 android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
@Override
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);
registerBtn.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View v) {
String userName =
userNameEdt.getText().toString(); String userEmail
= userEmailEdt.getText().toString();
if (userName.isEmpty() || userEmail.isEmpty() ||
userPassword.isEmpty()) { Toast.makeText(MainActivity.this,
(); return;
userNameEdt.setText(""
);
userEmailEdt.setText("")
;
userPasswordEdt.setText("");
}
});
}
}
Activity_mainxml:
<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;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(query);
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
}
}
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>