0% found this document useful (0 votes)
6 views9 pages

Android Programs

The document contains multiple Android application programs including one for obtaining the user's current location, displaying a progress bar, implementing a scroll view with buttons, receiving SMS messages, and finding directions to a specific location. Each program includes Java code and corresponding XML layout files for user interface design. The programs utilize various Android components such as LocationManager, ProgressBar, ScrollView, and Google Maps API for functionality.

Uploaded by

ramanpandge151
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)
6 views9 pages

Android Programs

The document contains multiple Android application programs including one for obtaining the user's current location, displaying a progress bar, implementing a scroll view with buttons, receiving SMS messages, and finding directions to a specific location. Each program includes Java code and corresponding XML layout files for user interface design. The programs utilize various Android components such as LocationManager, ProgressBar, ScrollView, and Google Maps API for functionality.

Uploaded by

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

PROGRAM TO GET USER’S CURRENT LOCATION

package com.javapapers.android.geolocationfinder;

import android.os.Bundle;import android.app.Activity;import


android.content.Context;import android.location.Location;import
android.location.LocationListener;import
android.location.LocationManager;import android.widget.TextView;

import android.util.Log;

public class MainActivity extends Activity implements


LocationListener{protected LocationManager locationManager;protected
LocationListener locationListener;protected Context context;TextView
txtLat;String lat;String provider;protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;

@Overrideprotected void onCreate(Bundle savedInstanceState)


{super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtLat = (TextView) findViewById(R.id.textview1);

locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, this);}@Overridepublic void onLocationChanged(Location location)
{

txtLat = (TextView) findViewById(R.id.textview1);

txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"


+ location.getLongitude());}

@Overridepublic void onProviderDisabled(String provider)


{Log.d("Latitude","disable");}

@Overridepublic void onProviderEnabled(String provider)


{Log.d("Latitude","enable");}

@Overridepublic void onStatusChanged(String provider, int status,


Bundle extras) {Log.d("Latitude","status");}}
XML files for layout

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

<TextView

android:id="@+id/textview1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:text="@string/hello_world" />

</RelativeLayout>

PROGRAM FOR PROGRESSBAR

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/startButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Start"
android:textSize="20sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:padding="10dp"
android:background="#0f0"
android:textColor="#fff"/>

</RelativeLayout>

.JAVA FILE
package example.gb.progressbarexample;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate progress bar and start button
final ProgressBar simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);
Button startButton = (Button) findViewById(R.id.startButton);
// perform click event on button
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// visible the progress bar
simpleProgressBar.setVisibility(View.VISIBLE);
}
});
}

PROGRAM FOR SCROLLVIEW


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

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="20dp"
android:orientation="vertical">

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#f00"
android:text="Button 1"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#0f0"
android:text="Button 2"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#00f"
android:text="Button 3"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#ff0"
android:text="Button 4"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#f0f"
android:text="Button 5"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#f90"
android:text="Button 6"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#f00"
android:text="Button 7"
android:textColor="#ff9"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#444"
android:text="Button 8"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#ff002211"
android:text="Button 9"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#0f0"
android:text="Button 10"
android:textColor="#fff"
android:textSize="20sp" />

</LinearLayout>

</ScrollView>

</RelativeLayout>
.JAVA FILE
package com.example.gourav.scrollviewExample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

PROGRAM TO RECEIVE SMS


package com.javapapers.androidreceivesms;

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 SmsBroadcastReceiver extends BroadcastReceiver {

public static final String SMS_BUNDLE = "pdus";

public void onReceive(Context context, Intent intent) {

Bundle intentExtras = intent.getExtras();

if (intentExtras != null) {

Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);

String smsMessageStr = "";

for (int i = 0; i < sms.length; ++i) {

SmsMessage smsMessage =
SmsMessage.createFromPdu((byte[]) sms[i]);

String smsBody =
smsMessage.getMessageBody().toString();

String address = smsMessage.getOriginatingAddress();


smsMessageStr += "SMS From: " + address + "\n";

smsMessageStr += smsBody + "\n";

Toast.makeText(context, smsMessageStr,
Toast.LENGTH_SHORT).show();

//this will update the UI with message

SmsActivity inst = SmsActivity.instance();

inst.updateList(smsMessageStr);

}}

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


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

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:id="@+id/MainLayout"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="SMS Inbox"

android:id="@+id/textView"

android:layout_gravity="center_horizontal" />

</LinearLayout>
Write a program to find the direction from user's current location
to MSBTE, Bandra.

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {


private GoogleMap mMap;
private FusedLocationProviderClient fusedLocationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
getCurrentLocation();
}

private void getCurrentLocation() {


if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}
fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
if (location != null) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(userLocation).title("You are here"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
getDirections(userLocation);
}
});
}

private void getDirections(LatLng userLocation) {


LatLng msbteLocation = new LatLng(19.0728, 72.8347); // MSBTE, Bandra coordinates
// Call Google Directions API here to get the route
// Display the route on the map
}
}

.xml
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

You might also like