0% found this document useful (0 votes)
9 views40 pages

BPP Mada67

pracrical computer egg

Uploaded by

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

BPP Mada67

pracrical computer egg

Uploaded by

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

En no.

:-226370307067

Practical 2

AIM: Understanding of Various Components available in Android Application.

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.

An activity is implemented as a subclass of Activity class as follows −

public class MainActivity extends Activity {

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.

A service is implemented as a subclass of Service class as follows −

public class MyService extends Service {

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

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and each message


is broadcaster as an Intent object.

public class MyReceiver extends BroadcastReceiver {

public void onReceive(context,intent){}

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.

A content provider is implemented as a subclass of ContentProvider class and must implement


a standard set of APIs that enable other applications to perform transactions.

public class MyContentProvider extends ContentProvider {

public void onCreate(){}

5. Intents

It is a powerful inter-application message-passing framework. They are extensively used


throughout Android. Intents can be used to start and stop Activities and Services, to broadcast
messages system-wide or to an explicit Activity, Service or Broadcast Receiver or to request
action be performed on a particular piece of data.

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.

9. Layout XML Files

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.

10. App APK files

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

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Log.d("lifecycle","onCreate invoked");

@Override

protected void onStart() {

super.onStart();

Log.d("lifecycle","onStart invoked");

@Override

protected void onResume() {

super.onResume();

Log.d("lifecycle","onResume invoked");

@Override

protected void onPause() {

super.onPause();

Log.d("lifecycle","onPause invoked");

}
En no.:-226370307067

@Override

protected void onStop() {

super.onStop();

Log.d("lifecycle","onStop invoked");

@Override

protected void onRestart() {

super.onRestart();

Log.d("lifecycle","onRestart invoked");

@Override

protected void onDestroy() {

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

public class PermissionActivity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_CODE = 200;

private View view;

private Button check_permission;

private Button request_permission;

@SuppressLint("MissingInflatedId")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_permission);

Button check_permission = (Button) findViewById(R.id.check_permission);

Button request_permission = (Button) findViewById(R.id.request_permission);

check_permission.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (checkPermission()) {

Toast.makeText(PermissionActivity.this, "Permission already granted.",


Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(PermissionActivity.this, "Please request permission.",


Toast.LENGTH_SHORT).show();

}
En no.:-226370307067

});

request_permission.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (!checkPermission()) {

requestPermission();

} else {

Toast.makeText(PermissionActivity.this, "Permission already granted.",


Toast.LENGTH_SHORT).show(); }

});

private boolean checkPermission() {

int result = ContextCompat.checkSelfPermission(getApplicationContext(),


ACCESS_FINE_LOCATION);

int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);

return result == PackageManager.PERMISSION_GRANTED && result1 ==


PackageManager.PERMISSION_GRANTED;

private void requestPermission() {

ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION, CAMERA},


PERMISSION_REQUEST_CODE);

@Override
En no.:-226370307067

public void onRequestPermissionsResult(int requestCode, String permissions[], int[]


grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {

case PERMISSION_REQUEST_CODE:

if (grantResults.length > 0) {

boolean locationAccepted = grantResults[0] ==


PackageManager.PERMISSION_GRANTED;

boolean cameraAccepted = grantResults[1] ==


PackageManager.PERMISSION_GRANTED;

if (locationAccepted && cameraAccepted)

Toast.makeText(this, "Permission Granted, Now you can access location data and
camera.", Toast.LENGTH_SHORT).show();

else {

Toast.makeText(this, "Permission Denied, You cannot access location data and


camera.", Toast.LENGTH_SHORT).show();

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

if (shouldShowRequestPermissionRationale(ACCESS_FINE_LOCATION)) {

showMessageOKCancel("You need to allow access to both the permissions",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

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

requestPermissions(new String[]{ACCESS_FINE_LOCATION,
CAMERA},

PERMISSION_REQUEST_CODE);
En no.:-226370307067

});

return;

} }

}}

break;

private void showMessageOKCancel(String message, DialogInterface.OnClickListener


okListener) { new AlertDialog.Builder(PermissionActivity.this)

.setMessage(message)

.setPositiveButton("OK", okListener)

.setNegativeButton("Cancel", null)

.create()

.show(); }

activity_permission.xml

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

<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:text="Run Time Permission"

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"

android:text="Turn On" android:layout_marginLeft="100dp"


android:layout_marginTop="200dp" />

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

android:text="Turn OFF" />

</RelativeLayout>
En no.:-226370307067

Main activity.java
public class MainActivity extends AppCompatActivity {

private static final int REQUEST_CODE_WIFI_PERMISSION = 101;

private static final int REQUEST_CODE_BLUETOOTH_PERMISSION = 102;

@Override

protected void onCreate(Bundle savedInstanceState) {

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 {

WifiManager wifiManager = (WifiManager)


getApplicationContext().getSystemService(WIFI_SERVICE);

wifiManager.setWifiEnabled(!wifiManager.isWifiEnabled()); // Toggle WiFi state

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

// Bluetooth permissions granted, you can toggle Bluetooth state here

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter != null) {

if (bluetoothAdapter.isEnabled()) {

bluetoothAdapter.disable(); // Turn off Bluetooth

} else {

bluetoothAdapter.enable(); // Turn on Bluetooth

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,


@NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {

case REQUEST_CODE_WIFI_PERMISSION: {

if (grantResults.length > 0 && grantResults[0] ==


PackageManager.PERMISSION_GRANTED) {

WifiManager wifiManager = (WifiManager)


getApplicationContext().getSystemService(WIFI_SERVICE);

wifiManager.setWifiEnabled(!wifiManager.isWifiEnabled()); // Toggle WiFi state

} else {

Toast.makeText(this, "WiFi permission denied", Toast.LENGTH_SHORT).show();


En no.:-226370307067

break;

case REQUEST_CODE_BLUETOOTH_PERMISSION: {

if (grantResults.length > 1 && grantResults[0] ==


PackageManager.PERMISSION_GRANTED

&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {

// Bluetooth permissions granted, you can toggle Bluetooth state here

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter != null) {

if (bluetoothAdapter.isEnabled()) {

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {

// ActivityCompat#requestPermissions

// here to request the missing permissions, and then overriding

// public void onRequestPermissionsResult(int requestCode, String[]


permissions,

// int[] grantResults)

// to handle the case where the user grants the permission. See the
documentation

// for ActivityCompat#requestPermissions for more details.

return;

bluetoothAdapter.disable(); // Turn off Bluetooth

} else {

bluetoothAdapter.enable(); // Turn on Bluetooth


En no.:-226370307067

} else {

Toast.makeText(this, "Bluetooth permission denied", Toast.LENGTH_SHORT).show();

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

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

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

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

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

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

public class AlertDialougeActivity extends AppCompatActivity {

private Button button1;

private Button button2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_alert_dialouge);

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

button2 = (Button) findViewById(R.id.button2);

button1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialougeActivity.this);

builder.setMessage("Do you want to exit ?");

builder.setTitle("Alert !");

builder.setCancelable(false);

builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog, which) -> {

finish();

});

builder.setNegativeButton("No", (DialogInterface.OnClickListener) (dialog, which) -> {

dialog.cancel();
En no.:-226370307067

});

AlertDialog alertDialog = builder.create();

alertDialog.show();

});

button2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

showAlertDialogButtonClicked();

});

public void showAlertDialogButtonClicked() {

// Create an alert builder

AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialougeActivity.this);

View view = LayoutInflater.from(AlertDialougeActivity.this).inflate(

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

final AlertDialog alertDialog = builder.create();

view.findViewById(R.id.buttonYes).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

alertDialog.dismiss();

Toast.makeText(AlertDialougeActivity.this,

"Yes", Toast.LENGTH_SHORT).show();

});

view.findViewById(R.id.buttonNo).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

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

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

<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

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

<androidx.constraintlayout.widget.ConstraintLayout

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

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

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

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

MyListData[] myListData = new MyListData[] {

new MyListData("Email", android.R.drawable.ic_dialog_email),

new MyListData("Info", android.R.drawable.ic_dialog_info),

new MyListData("Delete", android.R.drawable.ic_delete),

new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),

new MyListData("Alert", android.R.drawable.ic_dialog_alert),

new MyListData("Map", android.R.drawable.ic_dialog_map),

new MyListData("Email", android.R.drawable.ic_dialog_email),

new MyListData("Info", android.R.drawable.ic_dialog_info),

new MyListData("Delete", android.R.drawable.ic_delete),

new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),

new MyListData("Alert", android.R.drawable.ic_dialog_alert),

new MyListData("Map", android.R.drawable.ic_dialog_map),

};
En no.:-226370307067

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

MyListAdapter adapter = new MyListAdapter(myListData);

recyclerView.setHasFixedSize(true);

recyclerView.setLayoutManager(new LinearLayoutManager(this));

GridLayoutManager gridLayoutManager =

new GridLayoutManager(getApplicationContext(),2);

recyclerView.setLayoutManager(gridLayoutManager);

recyclerView.setAdapter(adapter);

activity_main.xml

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

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

private MyListData[] listdata;


En no.:-226370307067

// RecyclerView recyclerView;

public MyListAdapter(MyListData[] listdata) {

this.listdata = listdata;

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());

View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);

ViewHolder viewHolder = new ViewHolder(listItem);

return viewHolder;

@Override

public void onBindViewHolder(ViewHolder holder, int position) {

final MyListData myListData = listdata[position];

holder.textView.setText(listdata[position].getDescription());

holder.imageView.setImageResource(listdata[position].getImgId());

holder.cardview.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Toast.makeText(view.getContext(),"click on item:
"+myListData.getDescription(),Toast.LENGTH_LONG).show();

}
En no.:-226370307067

});

@Override

public int getItemCount() {

return listdata.length;

public static class ViewHolder extends RecyclerView.ViewHolder {

public ImageView imageView;

public TextView textView;

public CardView cardview;

public ViewHolder(View itemView) {

super(itemView);

this.imageView = (ImageView) itemView.findViewById(R.id.imageView);

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

cardview = (CardView)itemView.findViewById(R.id.cardview);

list_item.xml

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


En no.:-226370307067

<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

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val btn_click_me = findViewById(R.id.btn_click_me) as Button

btn_click_me.setOnClickListener {

Toast.makeText(this@MainActivity2, "You clicked me.", Toast.LENGTH_SHORT).show()

activity_main.xml

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

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

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"
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 :

You might also like