BPP Mada67
BPP Mada67
:-226370307067
Practical 2
Application components are the essential building blocks of an Android application. These
components are loosely coupled by the application manifest file AndroidManifest.xml that
describes each component of the application and how they interact.
There are following four main components that can be used within an Android application :
1. Activities
An activity represents a single screen with a user interface,in-short Activity performs actions on
the screen. For example, an email application might have one activity that shows a list of new
emails, another activity to compose an email, and another activity for reading emails. If an
application has more than one activity, then one of them should be marked as the activity that
is presented when the application is launched.
2. Services
A service is a component that runs in the background to perform long-running operations. For
example, a service might play music in the background while the user is in a different
application, or it might fetch data over the network without blocking user interaction with an
activity.
3. Broadcast Receivers
Broadcast Receivers simply respond to broadcast messages from other applications or from the
system. For example, applications can also initiate broadcasts to let other applications know
that some data has been downloaded to the device and is available for them to use, so this is
broadcast receiver who will intercept this communication and will initiate appropriate action.
En no.:-226370307067
4. Content Providers
A content provider component supplies data from one application to others on request. Such
requests are handled by the methods of the ContentResolver class. The data may be stored in
the file system, the database or somewhere else entirely.
5. Intents
6. Widgets
These are the small visual application components that you can find on the home screen of the
devices. They are a special variation of Broadcast Receivers that allow us to create dynamic,
interactive application components for users to embed on their Home Screen.
En no.:-226370307067
7. Notifications
Notifications are the application alerts that are used to draw the user’s attention to some
particular app event without stealing focus or interrupting the current activity of the user. They
are generally used to grab user’s attention when the application is not visible or active,
particularly from within a Service or Broadcast Receiver. Examples: E-mail popups, Messenger
popups, etc.
8. Fragments
A fragment is a portion of the total user interface. Users can combine more than one fragment
in a single activity and these fragments can be reused in multiple activities. A fragment
generally contains Views and ViewGroups inside them.
Layout is the structure for the user interface in the application. XML files provide different types
of layouts for the different type of screen, it also specifies which GUI component, an activity or
fragment holds.
Apk file is the package file format that contains the program’s code, resources, assets. The
Android operating system uses them for installing mobile applications and middleware.
11. Resources
Resources in Android is for defining Images, texts, string values. Everything is defined in the
resource file and it can be referenced within the source code. We will learn about Android
Resources, in detail in our next upcoming article on Resources.
En no.:-226370307067
Practical 4
AIM:Develop Android Application to demonstrate methods of Activity Life Cycle.
MainActivity.java
public class MainActivity extends Activity {
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
@Override
super.onStart();
Log.d("lifecycle","onStart invoked");
@Override
super.onResume();
Log.d("lifecycle","onResume invoked");
@Override
super.onPause();
Log.d("lifecycle","onPause invoked");
}
En no.:-226370307067
@Override
super.onStop();
Log.d("lifecycle","onStop invoked");
@Override
super.onRestart();
Log.d("lifecycle","onRestart invoked");
@Override
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="example.javatpoint.com.activitylifecycle.MainActivity">
En no.:-226370307067
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
En no.:-226370307067
Practical 12.1
AIM:Develop the code to manage Permission using Manifest file and run time from
Activity.
PermissionActivity.this
@SuppressLint("MissingInflatedId")
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);
check_permission.setOnClickListener(new View.OnClickListener() {
@Override
if (checkPermission()) {
} else {
}
En no.:-226370307067
});
request_permission.setOnClickListener(new View.OnClickListener() {
@Override
if (!checkPermission()) {
requestPermission();
} else {
});
@Override
En no.:-226370307067
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
Toast.makeText(this, "Permission Granted, Now you can access location data and
camera.", Toast.LENGTH_SHORT).show();
else {
if (shouldShowRequestPermissionRationale(ACCESS_FINE_LOCATION)) {
new DialogInterface.OnClickListener() {
@Override
requestPermissions(new String[]{ACCESS_FINE_LOCATION,
CAMERA},
PERMISSION_REQUEST_CODE);
En no.:-226370307067
});
return;
} }
}}
break;
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show(); }
activity_permission.xml
<LinearLayout 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"
En no.:-226370307067
android:background="#b8adb2"
android:orientation="vertical"
tools:context=".RadioActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/theme">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="My Application"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold" />
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/back_main"
android:orientation="vertical"
tools:context=".RadioActivity">
<LinearLayout
En no.:-226370307067
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context=".RadioActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="@color/text_color"
android:textSize="25dp"
android:textStyle="bold" />
<Button
android:id="@+id/check_permission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:background="@drawable/button_back"
android:text="Check Permission"
android:textColor="@color/white"
android:textAllCaps="false" />
<Button
En no.:-226370307067
android:id="@+id/request_permission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:background="@drawable/button_back"
android:text="Request Permission"
android:textColor="@color/white"
android:textAllCaps="false" /
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical">
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/idTVSelectedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
En no.:-226370307067
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/idTVSelectedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Output :
En no.:-226370307067
Practical 12.2
AIM:Develop the code to manage toggle state of WiFi and Bluetooth.
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"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/btnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/btnOFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnOn"
android:layout_toRightOf="@+id/btnOn"
</RelativeLayout>
En no.:-226370307067
Main activity.java
public class MainActivity extends AppCompatActivity {
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_WIFI_STATE},
REQUEST_CODE_WIFI_PERMISSION);
} else {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN},
REQUEST_CODE_BLUETOOTH_PERMISSION);
} else {
En no.:-226370307067
if (bluetoothAdapter != null) {
if (bluetoothAdapter.isEnabled()) {
} else {
@Override
switch (requestCode) {
case REQUEST_CODE_WIFI_PERMISSION: {
} else {
break;
case REQUEST_CODE_BLUETOOTH_PERMISSION: {
if (bluetoothAdapter != null) {
if (bluetoothAdapter.isEnabled()) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// ActivityCompat#requestPermissions
// int[] grantResults)
// to handle the case where the user grants the permission. See the
documentation
return;
} else {
} else {
break;
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Sharedpr10"
En no.:-226370307067
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
Output :
En no.:-226370307067
Practical 14
AIM: Develop Android Applications to demonstrate different AlertDialogs and the
Custom Dialog.
AlertDialougeActivity.java
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialouge);
button1.setOnClickListener(new View.OnClickListener() {
@Override
builder.setTitle("Alert !");
builder.setCancelable(false);
finish();
});
dialog.cancel();
En no.:-226370307067
});
alertDialog.show();
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
showAlertDialogButtonClicked();
});
R.layout.custom_layout,
(ConstraintLayout)findViewById(R.id.layoutDialogContainer)
);
builder.setView(view);
((TextView) view.findViewById(R.id.textTitle))
.setText(("Warning title"));
((TextView) view.findViewById(R.id.textMessage))
En no.:-226370307067
.setText("the reason for the warning and the potential problem, how someone should
act, and what happens if they don't act.");
((Button) view.findViewById(R.id.buttonYes))
.setText("yes");
((Button) view.findViewById(R.id.buttonNo))
.setText("no");
((ImageView) view.findViewById(R.id.imageIcon))
.setImageResource(R.drawable.ic_launcher_foreground);
view.findViewById(R.id.buttonYes).setOnClickListener(new View.OnClickListener() {
@Override
alertDialog.dismiss();
Toast.makeText(AlertDialougeActivity.this,
"Yes", Toast.LENGTH_SHORT).show();
});
view.findViewById(R.id.buttonNo).setOnClickListener(new View.OnClickListener() {
@Override
alertDialog.dismiss();
Toast.makeText(AlertDialougeActivity.this,
"No", Toast.LENGTH_SHORT).show();
}
En no.:-226370307067
});
if (alertDialog.getWindow() != null){
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
alertDialog.show();
activity_alert_dialouge.xml
<LinearLayout 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="@color/back"
android:orientation="vertical"
tools:context=".RadioActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/theme">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
En no.:-226370307067
android:layout_marginLeft="20dp"
android:text="Alert Dialouge"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold" />
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/back_main"
android:orientation="vertical"
tools:context=".RadioActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context=".RadioActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
En no.:-226370307067
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:background="@drawable/button_back"
android:text="AlertDialog"
android:textAllCaps="false"
android:textColor="@color/white" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:background="@drawable/button_back"
android:text="Custom AlertDialog"
android:textAllCaps="false"
android:textColor="@color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical">
<TextView
En no.:-226370307067
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/idTVSelectedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/idTVSelectedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout
En no.:-226370307067
custom_layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layoutDialogContainer"
android:layout_margin="20dp"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layoutDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textTitle"
En no.:-226370307067
android:background="@drawable/warning_background"
android:padding="10dp"
android:textColor="#fff"
android:textSize="17sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/imageIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginEnd="10dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="@id/textTitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/textTitle"
app:tint="@color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textMessage"
android:layout_marginStart="20dp"
android:layout_marginTop="18sp"
En no.:-226370307067
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
android:textColor="@color/text_color"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/textTitle"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:id="@+id/buttonNo"
android:layout_marginStart="40dp"
android:layout_marginEnd="10dp"
android:background="@drawable/button_neutral_background"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/layoutDialog"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/buttonYes"
app:layout_constraintTop_toBottomOf="@id/layoutDialog"/>
<Button
android:layout_width="0dp"
En no.:-226370307067
android:layout_height="40dp"
android:id="@+id/buttonYes"
android:layout_marginStart="10dp"
android:layout_marginEnd="40dp"
android:background="@drawable/button_warning_background"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/layoutDialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/buttonNo"
app:layout_constraintTop_toBottomOf="@id/layoutDialog"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Output :
En no.:-226370307067
Practical 16
AIM:Develop an Android Application to demonstrate the use of RecyclerView and
CardView for displaying list of items with multiple information
MainActivity.java
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
};
En no.:-226370307067
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
GridLayoutManager gridLayoutManager =
new GridLayoutManager(getApplicationContext(),2);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
activity_main.xml
<LinearLayout 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="@color/back"
android:orientation="vertical"
tools:context=".RadioActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="60dp"
En no.:-226370307067
android:background="@color/theme">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Recycleview"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold" />
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_margin="10dp"
android:padding="10dp"
android:background="@color/back_main"
android:id="@+id/recyclerView"/>
</LinearLayout>
MyListAdapter.java
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{
// RecyclerView recyclerView;
this.listdata = listdata;
@Override
return viewHolder;
@Override
holder.textView.setText(listdata[position].getDescription());
holder.imageView.setImageResource(listdata[position].getImgId());
holder.cardview.setOnClickListener(new View.OnClickListener() {
@Override
Toast.makeText(view.getContext(),"click on item:
"+myListData.getDescription(),Toast.LENGTH_LONG).show();
}
En no.:-226370307067
});
@Override
return listdata.length;
super(itemView);
cardview = (CardView)itemView.findViewById(R.id.cardview);
list_item.xml
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
app:cardBackgroundColor="@color/back"
app:cardCornerRadius="15dp"
app:cardElevation="10dp"
android:layout_margin="5dp"
android:layout_height="?android:attr/listPreferredItemHeightLarge">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightLarge">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
En no.:-226370307067
android:layout_marginEnd="10dp"
android:contentDescription="Icon" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Output :
En no.:-226370307067
Practical18
AIM:Develop an android application using Kotlin having a Button “Click” and upon
clicking on that Button a Toast message “Button Clicked” should be displayed on
screen through Toast Message
MainActivity.kt
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_click_me.setOnClickListener {
activity_main.xml
<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"
En no.:-226370307067
android:layout_height="match_parent"
tools:context=".MainActivity2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click Me!"
android:textSize="20dp"
android:id="@+id/btn_click_me"/>
</RelativeLayout>
Outout :