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

Practical_Programs

The document contains multiple Android application code snippets, each demonstrating a specific functionality such as creating a search engine interface, toggling Bluetooth, implementing a login form, displaying checkboxes with counts, showing a progress bar, changing images, displaying buttons in a grid view, and handling orders with checkboxes. Each section includes XML layout code and corresponding Java code for the main activity. The examples cover various UI components and interactions in Android development.

Uploaded by

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

Practical_Programs

The document contains multiple Android application code snippets, each demonstrating a specific functionality such as creating a search engine interface, toggling Bluetooth, implementing a login form, displaying checkboxes with counts, showing a progress bar, changing images, displaying buttons in a grid view, and handling orders with checkboxes. Each section includes XML layout code and corresponding Java code for the main activity. The examples cover various UI components and interactions in Android development.

Uploaded by

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

1) Write a program to create first display screen of any search engine using auto

complete text view.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Search Anything.."
android:textStyle="bold"/>

<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:hint="type here..."/>

</LinearLayout>

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity


{
AutoCompleteTextView txt;
String[] sub={"java","mad","php","python","ede","eti","mgt"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=findViewById(R.id.txt);

ArrayAdapter<String> adapter=new
ArrayAdapter<>(this,android.R.layout.simple_dropdown_item_1line,sub);
txt.setThreshold(1);
txt.setAdapter(adapter);
}
}
2) Develop a program to turn ON and OFF Bluetooth.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Bluetooth ON"
android:textOff="Bluetooth OFF"/>
</LinearLayout>

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


BluetoothAdapter ba;
ToggleButton tg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tg = findViewById(R.id.toggleButton);
ba = BluetoothAdapter.getDefaultAdapter();

// Set initial toggle state


tg.setChecked(ba.isEnabled());

tg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tg.isChecked()) {
ba.enable(); // Turn ON Bluetooth
Toast.makeText(MainActivity.this, "Bluetooth Turned ON",
Toast.LENGTH_SHORT).show();
} else {
ba.disable(); // Turn OFF Bluetooth
Toast.makeText(MainActivity.this, "Bluetooth Turned OFF",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
3) WAP to create a login form for social networking site.

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<ImageView
android:id="@+id/img"
android:layout_width="190dp"
android:layout_height="150dp"
android:src="@drawable/images"/>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Social Network"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_marginBottom="32dp"/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username or Email"
android:inputType="textEmailAddress"
android:padding="16dp"
android:layout_marginBottom="16dp"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="16dp"
android:layout_marginBottom="32dp"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:padding="16dp"/>
</LinearLayout>

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.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText u,p;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
u=findViewById(R.id.username);
p=findViewById(R.id.password);
b=findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=u.getText().toString().trim();
String password=p.getText().toString().trim();
if(username.isEmpty() || password.isEmpty())
{
Toast.makeText(MainActivity.this,"Please Enter all
Details...",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this,"Login
Successul...",Toast.LENGTH_LONG).show();
}
}
});
}
}

4) Write a program to show five checkboxes and total selected checkboxes.

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 1"
android:id="@+id/cb1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 2"
android:id="@+id/cb2"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 3"
android:id="@+id/cb3"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 4"
android:id="@+id/cb4"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option 5"
android:id="@+id/cb5"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Count"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:text="Selected Checkboxes: "/>

</LinearLayout>

package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
CheckBox cb1,cb2,cb3,cb4,cb5;
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cb1=findViewById(R.id.cb1);
cb2=findViewById(R.id.cb2);
cb3=findViewById(R.id.cb3);
cb4=findViewById(R.id.cb4);
cb5=findViewById(R.id.cb5);
btn=findViewById(R.id.btn);
txt=findViewById(R.id.txt);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
int count=0;
if(cb1.isChecked()) count++;
if(cb2.isChecked()) count++;
if(cb3.isChecked()) count++;
if(cb4.isChecked()) count++;
if(cb5.isChecked()) count++;

txt.setText("Selected Checboxes: "+count);


}
});

}
}
5) WAP to show the following output

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt1"
android:text="File downloading"
android:textSize="20dp"
android:padding="10dp"/>
<ProgressBar
android:id="@+id/pb"
android:layout_width="250dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:progress="0"
android:max="100"
android:layout_marginTop="10dp"/>
<TextView
android:layout_widtht="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt2"
android:text="0%"
android:layout_marginTop="10dp"
android:textSize="16dp"/>

</LinearLayout>

package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
ProgressBar pb;
TextView txt2;
int progress=0;
Handler handler=new Handler();

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pb=findViewById(R.id.pb);
txt2=findViewById(R.id.txt2);
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if(progress<=100)
{
pb.setProgress(progress);
txt2.setText(progress+ "%");
progress++;
handler.postDelayed(this,100);
}
}
},100);
}
}

6) Write a program to implement Image View

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
android:src="@drawable/img1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_marginTop="30dp"
android:text="Change Image"/>
</LinearLayout>

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
ImageView img;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn=findViewById(R.id.btn);
img=findViewById(R.id.img);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageResource(R.drawable.img2);
}
});
}

7) WAP to display 15 buttons using Grid view

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center">

<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/grid"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:columnWidth="60dp"/>
</LinearLayout>

package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
GridView grid;
String[] gridarray={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

grid=findViewById(R.id.grid);
ArrayAdapter<String> adapter= new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,gridarray);
grid.setAdapter(adapter);
}

}
8) Write a program to display three checkboxes and one button named "Order". When
the user clicks the button, a Toast message will display the selected items, their
individual prices, and the total price.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza: 100Rs."
android:id="@+id/cb1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger: 80Rs."
android:id="@+id/cb2"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pasta: 60Rs."
android:id="@+id/cb3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Order"/>
</LinearLayout>

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
CheckBox cb1,cb2,cb3;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cb1=findViewById(R.id.cb1);
cb2=findViewById(R.id.cb2);
cb3=findViewById(R.id.cb3);
btn=findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result="selected Items: ";
int total=0;

if(cb1.isChecked())
{
result+="Pizza: 100Rs.\n";
total+=100;
}
if(cb2.isChecked())
{
result+="Burger: 80Rs.\n";
total+=80;
}
if(cb3.isChecked())
{
result+="Pasta: 60Rs.\n";
total+=60;
}
result+="Total: Rs."+total;
Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();
}
});

9) Program to display Date and Time using buttons and TextViews

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="20dp">

<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date" />

<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="Date will appear here" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time" />

<TextView
android:id="@+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="Time will appear here" />
</LinearLayout>

</LinearLayout>

package com.example.myapplication;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

Button btnDate, btnTime;


TextView txtDate, txtTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnDate = findViewById(R.id.btnDate);
btnTime = findViewById(R.id.btnTime);
txtDate = findViewById(R.id.txtDate);
txtTime = findViewById(R.id.txtTime);

btnDate.setOnClickListener(v -> {
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dp = new DatePickerDialog(MainActivity.this, (view, year,


month, dayOfMonth) ->
txtDate.setText(dayOfMonth + "/" + (month + 1) + "/" + year), y, m,
d);
dp.show();
});

btnTime.setOnClickListener(v -> {
Calendar c = Calendar.getInstance();
int h = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
TimePickerDialog tp = new TimePickerDialog(MainActivity.this, (view,
hourOfDay, minute) ->
txtTime.setText(hourOfDay + ":" + minute), h, min, false);
tp.show();
});
}
}

10) Activity Lifecycle


package com.example.myapplication;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this,"onCreate() Method
called.",Toast.LENGTH_LONG).show();
}

@Override
protected void onStart()
{
super.onStart();
Toast.makeText(MainActivity.this,"onStart() Method
called.",Toast.LENGTH_LONG).show();

@Override
protected void onResume()
{
super.onResume();
Toast.makeText(MainActivity.this,"onResume() Method
called.",Toast.LENGTH_LONG).show();

@Override
protected void onPause()
{
super.onPause();
Toast.makeText(MainActivity.this,"onPause() Method
called.",Toast.LENGTH_LONG).show();

@Override
protected void onStop()
{
super.onStop();
Toast.makeText(MainActivity.this,"onStop() Method
called.",Toast.LENGTH_LONG).show();
}

@Override
protected void onRestart()
{
super.onRestart();
Toast.makeText(MainActivity.this,"onRestart() Method
called.",Toast.LENGTH_LONG).show();

@Override
protected void onDestroy()
{
super.onDestroy();
Toast.makeText(MainActivity.this,"onDestroy() Method
called.",Toast.LENGTH_LONG).show();

}
}

11) Write a program to create a text View and button “Navigate” .When you enter www.google.com and
press navigate, it should open a goggle page
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Your URL here"
android:id="@+id/edt"
android:layout_margin="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edt"
android:text="Navigate"
android:id="@+id/btn"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

package com.example.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText edt;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt=findViewById(R.id.edt);
btn=findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new
Intent(Intent.ACTION_VIEW,Uri.parse(edt.getText().toString()));
startActivity(intent);
}
});

}
}

12) Sensors
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:text="Available Sensors:"/>
</LinearLayout>

package com.example.myapplication;
import android.os.Bundle;
import android.widget.*;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class MainActivity extends AppCompatActivity


{
TextView txt;
SensorManager sm;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt=findViewById(R.id.txt);
sm=(SensorManager) getSystemService (SENSOR_SERVICE);
List<Sensor> sensor=sm.getSensorList(Sensor.TYPE_ALL);
StringBuilder name=new StringBuilder();
int i;
for(i=0;i<sensor.size();i++)
{
name.append(sensor.get(i).getName()).append("\n");
}
txt.setText(name.toString());
}
}

13) Camera
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Capture Image"
android:layout_marginTop="30dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn"
android:id="@+id/img"/>
</RelativeLayout>

package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.os.Bundle;
import android.widget.*;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
Button btn;
ImageView img;
static final int REQUEST_IMAGE_CAPTURE=1;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
img=findViewById(R.id.img);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
}
});
}
@Override
protected void onActivityResult(int requestcode, int resultcode, @Nullable Intent
data)
{
super.onActivityResult(requestcode,resultcode,data);
if(requestcode==REQUEST_IMAGE_CAPTURE && resultcode==RESULT_OK)
{
Bitmap photo=(Bitmap) data.getExtras().get("data");
img.setImageBitmap(photo);
}
}
}

14) Send SMS


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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etphone"
android:hint="Enter Phone Number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etmsg"
android:hint="Enter Message"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Send SMS"/>

</RelativeLayout>

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.os.Bundle;
import android.Manifest;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
EditText etphone,etmsg;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etphone=findViewById(R.id.etphone);
etmsg=findViewById(R.id.etmsg);
btn=findViewById(R.id.btn);

ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.SEND_SMS},1);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone=etphone.getText().toString();
String msg=etmsg.getText().toString();
if(!phone.isEmpty() && !msg.isEmpty())
{
SmsManager sm=SmsManager.getDefault();
sm.sendTextMessage(phone,null,msg,null,null);
Toast.makeText(MainActivity.this,"Sms
Send!!",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Sms not
Send!!",Toast.LENGTH_LONG).show();
}
}
});

}
}

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

Send and Receive SMS

Same xml, java files just create one more java file with name SmsReceiver.java

package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver


{
public void onReceive(Context context, Intent intent)
{
Bundle bundle=intent.getExtras();
if(bundle!=null)
{
Object[] pdus=(Object[]) bundle.get("pdus");
for(Object pdu :pdus)
{
SmsMessage sm= SmsMessage.createFromPdu((byte[]) pdu);
String msg= sm.getMessageBody();
String from=sm.getOriginatingAddress();
Toast.makeText(context,"Sms from
"+from+":"+"msg",Toast.LENGTH_LONG).show();
}
}
}
}

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<receiver android:name=".SmsReciever"
android:exported="true">
<intent-filter>
<action android:name=""android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

15) Send Email


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/to"
android:hint="Enter recipient email"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sub"
android:hint="Enter Subject"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/msg"
android:hint="Enter Message"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Send email"/>

</LinearLayout>

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.content.Intent;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
EditText to,sub,msg;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

to=findViewById(R.id.to);
sub=findViewById(R.id.sub);
msg=findViewById(R.id.msg);
btn=findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String e_to=to.getText().toString();
String e_sub=sub.getText().toString();
String e_msg=msg.getText().toString();

Intent intent=new Intent(Intent.ACTION_SEND);


intent.putExtra(Intent.EXTRA_EMAIL,new String[]{e_to});
intent.putExtra(Intent.EXTRA_SUBJECT,e_sub);
intent.putExtra(Intent.EXTRA_TEXT,e_msg);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent,"Choose Email App"));
}
});
}
}

16) Write a program to turn on and list devices Bluetooth.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<TextView
android:text="BLUETOOTH"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="10dp"/>

<Button
android:id="@+id/btnTurnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"/>

<Button
android:id="@+id/btnListDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices"/>

</LinearLayout>

package com.example.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Set;

public class MainActivity extends AppCompatActivity {


BluetoothAdapter bluetoothAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Button btnTurnOn = findViewById(R.id.btnTurnOn);
Button btnListDevices = findViewById(R.id.btnListDevices);

// Turn On Bluetooth
btnTurnOn.setOnClickListener(v -> {
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
Toast.makeText(this, "Turning On Bluetooth", Toast.LENGTH_LONG).show();
}
});

// List Paired Devices


btnListDevices.setOnClickListener(v -> {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.isEmpty()) {
Toast.makeText(this, "No Paired Devices", Toast.LENGTH_SHORT).show();
} else {
for (BluetoothDevice device : pairedDevices) {
Toast.makeText(this, device.getName(), Toast.LENGTH_SHORT).show();
}
}
});

}
}

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

17) Calculator

<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:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Enter first number" />

<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Enter second number" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/addButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />

<Button
android:id="@+id/subtractButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subtract" />

<Button
android:id="@+id/multiplyButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Multiply" />

<Button
android:id="@+id/divideButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Divide" />
</LinearLayout>

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Result will be displayed here" />

</LinearLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText number1, number2;


Button addButton, subtractButton, multiplyButton, divideButton;
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

number1 = findViewById(R.id.number1);
number2 = findViewById(R.id.number2);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);
result = findViewById(R.id.result);

addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 + num2;
result.setText("Result: " + res);
}
});

subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 - num2;
result.setText("Result: " + res);
}
});

multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 * num2;
result.setText("Result: " + res);
}
});

divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 / num2;
result.setText("Result: " + res);
}
});
}
}

18) Shuffled

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>

package com.example.myapplication;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

linearLayout = findViewById(R.id.main);

if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
linearLayout.setBackgroundColor(Color.parseColor("#FFEB3B")); // Yellow for
portrait
} else if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
linearLayout.setBackgroundColor(Color.parseColor("#2CBDF2")); // Blue for
landscape
}
}
}

You might also like