2.
Develop an Android Application using Widgets
Create the layout file (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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Widgets!"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter text"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_below="@id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check me"
android:layout_below="@id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button"
android:layout_below="@id/checkBox"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle me"
android:layout_below="@id/radioButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
Create the MainActivity.java file and add event listeners:
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button 1 clicked",
Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button 2 clicked",
Toast.LENGTH_SHORT).show();
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button 3 clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}
3. Develop an Android Application for Layout Managers and Event Listeners
Create the layout file (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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp" />
</RelativeLayout>
Create the MainActivity.java file and add event listeners:
package com.example.content.layout;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button 1 clicked",
Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button 2 clicked",
Toast.LENGTH_SHORT).show();
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button 3 clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}
7. Develop an Android Application using Multimedia
simple Android application that demonstrates how to play multimedia (audio) using
MediaPlayer:
1. First, create a new Android project in Android Studio.
2. Add an audio file (e.g., audio.mp3) to the res/raw directory. If the
directory doesn't exist, you can create it.
3. In your MainActivity.java, add the following code to play the audio file when
the activity starts:
import android.media.MediaPlayer;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.audio);
mediaPlayer.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
Make sure you have the necessary permissions in your AndroidManifest.xml to read
the audio file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run your application, and it should start playing the audio file audio.mp3 from the
res/raw directory when the activity starts.
Remember that this is a basic example to get you started. Depending on your
requirements, you may want to add more features like play/pause controls, volume
control, seekbar for progress tracking, etc. You would implement these controls in
your layout XML file and handle their actions accordingly in your activity code.
9. Develop an Android Application using Telephony –Call, SMS, and Email.
an Android application that utilizes telephony functionalities for making calls,
sending SMS, and sending emails. However, please note that certain permissions are
required for these functionalities, and you should handle permissions properly in
your application.
1. Permissions: Add necessary permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
Layout: Create your layout file (activity_main.xml) with buttons for making calls,
sending SMS, and sending emails:
<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/buttonCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />
<Button
android:id="@+id/buttonSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonCall"
android:text="Send SMS" />
<Button
android:id="@+id/buttonEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSMS"
android:text="Send Email" />
</RelativeLayout>
Activity: Implement the functionality in your MainActivity.java:
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CALL_PERMISSION = 100;
private static final int REQUEST_SMS_PERMISSION = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonCall = findViewById(R.id.buttonCall);
Button buttonSMS = findViewById(R.id.buttonSMS);
Button buttonEmail = findViewById(R.id.buttonEmail);
buttonCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makePhoneCall();
}
});
buttonSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});
buttonEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
}
private void makePhoneCall() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PERMISSION);
return;
}
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "1234567890")); // Replace with desired
phone number
startActivity(intent);
}
private void sendSMS() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.SEND_SMS}, REQUEST_SMS_PERMISSION);
return;
}
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("1234567890", null, "Hello from my app!", null,
null); // Replace with desired phone number
Toast.makeText(this, "SMS sent.", Toast.LENGTH_SHORT).show();
}
private void sendEmail() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"
[email protected]"});
// Replace with recipient email
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(intent, "Send Email"));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CALL_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
makePhoneCall();
} else {
Toast.makeText(this, "Permission denied.",
Toast.LENGTH_SHORT).show();
}
} else if (requestCode == REQUEST_SMS_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
sendSMS();
} else {
Toast.makeText(this, "Permission denied.",
Toast.LENGTH_SHORT).show();
}
}
}
}
Remember to replace placeholder phone numbers and email addresses with your desired
recipients. Also, handle the email sending properly with a real email client or
your own server. This is just a basic example to get you started.
4. Develop an Android Application using Activity and Intents
Create the first activity layout file (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">
<Button
android:id="@+id/buttonNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate to Second Activity"
android:layout_centerInParent="true" />
</RelativeLayout>
Create the second activity layout file (activity_second.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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="24sp"
android:layout_centerInParent="true" />
</RelativeLayout>
Create the first activity Java file (MainActivity.java):
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonNavigate = findViewById(R.id.buttonNavigate);
buttonNavigate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Navigate to the second activity
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
}
}
Create the second activity Java file (SecondActivity.java):
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Declare the activities in the AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
</application>
</manifest>
5. Develop an Android Application using Menus
Create the menu XML file (menu_main.xml):
Create a new XML file under the res/menu directory and name it menu_main.xml. This
file will define the menu items.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_settings"
android:title="Settings" />
<item
android:id="@+id/action_about"
android:title="About" />
</menu>
Create the layout file (activity_main.xml):
Create a layout file for your main activity. In this example, we'll keep it simple
with just a TextView displaying a message.
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Menu Example!"
android:layout_centerInParent="true" />
</RelativeLayout>
Create the Java file for your main activity (MainActivity.java):
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the TextView reference
TextView textView = findViewById(R.id.textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
int id = item.getItemId();
// Handle clicks on the menu items
if (id == R.id.action_settings) {
Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.action_about) {
Toast.makeText(this, "About clicked", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Declare the activity in the AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now, when you run the application, you will see a simple TextView displayed on the
screen. When you click on the overflow menu icon (usually represented by three
dots) in the toolbar, you will see the "Settings" and "About" options. Clicking on
either of these options will display a toast message indicating the clicked action.
You can further extend this example by adding more menu items and handling their
actions accordingly.