Mad Cheet
Mad Cheet
Mad Cheet
and resource files. We will explore all the folders and files in the android app.
1. Manifests Folder
2. Java Folder
3. res (Resources) Folder
Drawable Folder
Layout Folder
Mipmap Folder
Values Folder
4. Gradle Scripts
Manifests Folder
Manifests folder contains AndroidManifest.xml for creating our android
application. This file contains information about our application such as the
Android version, metadata, states package for Kotlin file, and other application
components. It acts as an intermediator between android OS and our
application.
Java folder
The Java folder contains all the java and Kotlin source code (.java) files that we
create during the app development, including other Test files. If we create any
new project using Kotlin, by default the class file MainActivity.kt file will
create automatically under the package name
res/drawable folder
It contains the different types of images used for the development of the
application. We need to add all the images in a drawable folder for the
application development.
res/layout folder
The layout folder contains all XML layout files which we used to define the
user interface of our application. It contains the activity_main.xml file.
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageButton = findViewById(R.id.myImageButton);
myImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform some action when the image button is clicked
}
});
}
}
Xml code - :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/myTimePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
</LinearLayout>
Java code -:
import android.os.Bundle;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
myTimePicker = findViewById(R.id.myTimePicker);
// Set the current time as the default value for the time picker
Calendar calendar = Calendar.getInstance();
myTimePicker.setHour(calendar.get(Calendar.HOUR_OF_DAY));
myTimePicker.setMinute(calendar.get(Calendar.MINUTE));
Starting a new activity: You can use an Intent to start a new activity in
your application, or even in another application. This is commonly used
to transition between different screens or sections of an app, or to launch
a third-party app for a specific task.
Passing data between activities: You can use an Intent to pass data
between activities, such as user input or data retrieved from a web
service. This data can be simple values like strings and integers, or more
complex objects like arrays or custom classes.
In Android, there are two types of Intent: explicit and implicit. Here's an
explanation of each type with a program example:
Explicit Intent: An explicit intent is used to start a specific activity within your
own application. The target activity is identified by its class name.
Here's an example of how to use an explicit Intent to start a new activity:
// Create an explicit intent to start the SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
35. Write a program to create a calculator application and display the result on
second activity using explicit intent.
1. onCreate()
It is called when the activity is first created. This is where all the static work is
done like creating views, binding data to lists, etc. This method also provides a
Bundle containing its previous frozen state, if there was one.
2. onStart()
It is invoked when the activity is visible to the user. It is followed by
onResume() if the activity is invoked from the background. It is also invoked
after onCreate() when the activity is first started.
3. onRestart()
It is invoked after the activity has been stopped and prior to its starting stage
and thus is always followed by onStart() when any activity is revived from
background to on-screen.
4. onResume()
It is invoked when the activity starts interacting with the user. At this point, the
activity is at the top of the activity stack, with a user interacting with it. Always
followed by onPause() when the activity goes into the background or is closed
by the user.
5. onPause()
It is invoked when an activity is going into the background but has not yet been
killed. It is a counterpart to onResume(). When an activity is launched in front
of another activity, this callback will be invoked on the top activity (currently
on screen). The activity, under the active activity, will not be created until the
active activity’s onPause() returns, so it is recommended that heavy processing
should not be done in this part.
6. onStop()
It is invoked when the activity is not visible to the user. It is followed
by onRestart() when the activity is revoked from the background, followed by
onDestroy() when the activity is closed or finished, and nothing when the
activity remains on the background only. Note that this method may never be
called, in low memory situations where the system does not have enough
memory to keep the activity’s process running after its onPause() method is
called.
7. onDestroy()
The final call received before the activity is destroyed. This can happen either
because the activity is finishing (when finish() is invoked) or because the
system is temporarily destroying this instance of the activity to save space. To
distinguish between these scenarios, check it with isFinishing() method.
System Broadcasts: These are broadcasts that are sent by the system to notify
applications of system-level events, such as the device being restarted or the
battery level changing.
Here are some examples of system broadcasts:
In Android, Content Providers are a very important component that serves the
purpose of a relational database to store the data of applications. The role of the
content provider in the android system is like a central repository in which data
of the applications are stored, and it facilitates other applications to securely
access and modifies that data based on the user requirements. Android system
allows the content provider to store the application data in several ways. Users
can manage to store the application data like images, audio, videos, and
personal contact information by storing them in SQLite Database , in files, or
even on a network. In order to share the data, content providers have certain
permissions that are used to grant or restrict the rights to other applications to
interfere with the data.
Create a class in the same directory where the that MainActivity file resides
and this class must extend the ContentProvider base class.
To access the content, define a content provider URI address.
Create a database to store the application data.
Implement the six abstract methods of ContentProvider class.
Register the content provider in AndroidManifest.xml file
using <provider> tag.
42. Define service. What do you mean by started and bound service.
Media Player API: This API allows developers to play audio and video
files, as well as streaming media. It provides a range of features, such as
playback speed control, volume control, and support for various media
formats.
Media Recorder API: This API allows developers to record audio and
video, as well as capture images. It provides a range of features, such as
video resolution and bitrate control, and support for various media
formats.
Camera API: This API provides access to the device's camera, allowing
developers to capture images and video. It provides a range of features,
such as autofocus, zoom, and flash control.
OpenGL ES: This API provides a way for developers to create and
manipulate 3D graphics on Android devices. It provides a range of
features, such as texture mapping, lighting, and animation.
OpenMAX AL: This API provides a low-level interface for accessing
hardware acceleration for multimedia tasks. It provides a range of
features, such as audio and video decoding and encoding, and supports
various media formats.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.image_view);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode ==
RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
}
Xml code -:
<TextView
android:layout_gravity="center"
android:textSize="30dp"
android:layout_marginTop="20dp"
android:text="SMS Sending App"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText
android:padding="20dp"
android:id="@+id/mobilenumber"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Enter phone number"
android:layout_width="match_parent"
android:layout_height="wrap_content"></EditText>
<EditText
android:padding="20dp"
android:id="@+id/message"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Enter your message"
android:layout_width="match_parent"
android:layout_height="wrap_content"></EditText>
<Button
android:layout_margin="20dp"
android:padding="20dp"
android:text="Send SMS"
android:id="@+id/send_btn"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
</LinearLayout>
Java Code -:
package com.example.smsapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Button send_btn;
EditText send_phonenumber;
EditText send_message;
String phoneno;
String mymessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send_btn = findViewById(R.id.send_btn);
send_message = findViewById(R.id.message);
send_phonenumber = findViewById(R.id.mobilenumber);
send_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneno = send_phonenumber.getText().toString();
mymessage = send_message.getText().toString();
try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneno,null,mymessage,null,null);
Toast.makeText(getApplicationContext(),"Message
sent",Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_LONG).sho
w();
}
}
});
}
}
Manifest File -:
<uses-permission android:name="android.permission.SEND_SMS"></uses-
permission>
@Override
public void onCreate(SQLiteDatabase db) {
// Create the table
String sql = "CREATE TABLE mytable ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "name TEXT,"
+ "age INTEGER)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Upgrade the table if needed
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationTextView = findViewById(R.id.location_text_view);
}
@Override
protected void onResume() {
super.onResume();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, this);
} else {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
locationTextView.setText("Latitude: " + latitude + "\nLongitude: " +
longitude);
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "GPS disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "GPS enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Manifest code -:
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
49. Explain the steps how to publish an android application on google playstore.
50. List sensors in android and explain any one them .
Android devices are equipped with a variety of sensors that enable various
functionalities in apps. Here are some of the common sensors in Android:
One of the most commonly used sensors in Android is the GPS sensor. The GPS
sensor uses signals from GPS satellites to determine the device's location. Here
is an example of how to use the GPS sensor in an Android app:
Code -:
public class MainActivity extends AppCompatActivity implements
LocationListener {
private LocationManager locationManager;
private TextView latitudeTextView, longitudeTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
latitudeTextView = findViewById(R.id.latitude_text_view);
longitudeTextView = findViewById(R.id.longitude_text_view);
}
@Override
protected void onResume() {
super.onResume();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, this);
} else {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, this);
}
}
}
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
51. Describe the need of permission in android. Explain with any example or
program.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
} else {
startCamera();
}
}
private void startCamera() {
try {
camera = Camera.open();
CameraPreview preview = new CameraPreview(this, camera);
FrameLayout previewLayout = findViewById(R.id.preview_layout);
previewLayout.addView(preview);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
Toast.makeText(this, "Camera permission denied",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
}
}
52. State and elaborate the syntax of required class and methods
for Geocoding.
53. Define activity in android. Explain how to create a new activity and switch
from one activity to another.
Activities are a fundamental building block of Android apps, and they are
responsible for managing their own lifecycle, including being created, started,
paused, resumed, stopped, and destroyed. They can also be organized into
backstacks to enable users to navigate between different screens and workflows.