0% found this document useful (0 votes)
10 views

final code

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)
10 views

final code

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

Here's an even simpler version of the SMS app in Android without any advanced logic.

This
version only sends an SMS when you click a button, and the phone number and message are
hardcoded for simplicity.

Step 1: Permissions in AndroidManifest.xml

Add the following permission to allow sending SMS:

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

Step 2: Create the Layout (activity_main.xml)

This is a very basic layout with a single button to send the 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">

<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_centerInParent="true"/>
</RelativeLayout>

Step 3: MainActivity.java

This is the simple Java code to send an SMS when you click the button.

import android.os.Bundle;
import android.telephony.SmsManager;
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 sendButton = findViewById(R.id.sendButton);


sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS(); // Call the method to send the SMS
}
});
}

// Send SMS method


private void sendSMS() {
try {
String phoneNumber = "1234567890"; // Hardcoded phone number
String message = "Hello! This is a test message."; // Hardcoded
message

SmsManager smsManager = SmsManager.getDefault();


smsManager.sendTextMessage(phoneNumber, null, message, null,
null);

Toast.makeText(this, "SMS Sent", Toast.LENGTH_SHORT).show(); //


Show a toast message
} catch (Exception e) {
Toast.makeText(this, "SMS Failed to Send",
Toast.LENGTH_SHORT).show(); // Show a failure message
}
}
}

How It Works:

1. Hardcoded Phone Number: The phone number and message are hardcoded for
simplicity. You can change them as per your requirement.
2. Button Click: When you click the button, it calls the sendSMS() method to send an
SMS.
3. Toast Messages: A Toast message will show whether the SMS was successfully sent or
if it failed.

Step 4: Test the App

1. Run the App on a physical device (SMS won't work on an emulator without
configuration).
2. Click the Button: Press the "Send SMS" button, and the SMS will be sent to the
hardcoded number with the message.
This is as simple as it gets! If you want to add more features (like taking input from the user),
just let me know.

Here's an ultra-simple and basic example for Geocoding and Reverse Geocoding in Android.
This example uses the Geocoder class to convert an address into coordinates and vice versa.
This version is as minimal as possible, focused purely on the essentials.

Step 1: Permissions in AndroidManifest.xml

Ensure you have permissions for using the internet and location:

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


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

Step 2: Minimal Layout (activity_main.xml)

This layout includes:

 A TextView for displaying results.


 An EditText to enter an address.
 A Button to trigger Geocoding (Address to Coordinates).
 Another Button for Reverse Geocoding (Coordinates to Address).

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

<!-- EditText for entering address -->


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

<!-- Button for Geocoding -->


<Button
android:id="@+id/btnGeocode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geocode" />

<!-- Button for Reverse Geocoding -->


<Button
android:id="@+id/btnReverseGeocode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reverse Geocode" />
<!-- TextView for displaying results -->
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Results will appear here"
android:layout_marginTop="16dp"/>
</LinearLayout>

Step 3: Minimal Java Code (MainActivity.java)

This code does the Geocoding (Address to Coordinates) and Reverse Geocoding (Coordinates
to Address).

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private EditText editText;


private TextView resultText;
private Geocoder geocoder;

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

// Initialize views
editText = findViewById(R.id.editText);
resultText = findViewById(R.id.resultText);
Button btnGeocode = findViewById(R.id.btnGeocode);
Button btnReverseGeocode = findViewById(R.id.btnReverseGeocode);

// Initialize Geocoder
geocoder = new Geocoder(this);

// Geocode button click (Address to Coordinates)


btnGeocode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String address = editText.getText().toString();
if (!address.isEmpty()) {
geocode(address);
}
}
});

// Reverse Geocode button click (Coordinates to Address)


btnReverseGeocode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String address = editText.getText().toString();
if (!address.isEmpty()) {
reverseGeocode(address);
}
}
});
}

// Geocode Address to Coordinates


private void geocode(String address) {
try {
List<Address> addresses = geocoder.getFromLocationName(address,
1);
if (addresses != null && !addresses.isEmpty()) {
Address location = addresses.get(0);
String result = "Lat: " + location.getLatitude() + ", Long: "
+ location.getLongitude();
resultText.setText(result);
} else {
resultText.setText("Address not found");
}
} catch (IOException e) {
resultText.setText("Error geocoding");
}
}

// Reverse Geocode Coordinates to Address


private void reverseGeocode(String coordinates) {
try {
String[] latLng = coordinates.split(",");
if (latLng.length == 2) {
double latitude = Double.parseDouble(latLng[0].trim());
double longitude = Double.parseDouble(latLng[1].trim());
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
resultText.setText(address.getAddressLine(0));
} else {
resultText.setText("No address found");
}
} else {
resultText.setText("Invalid coordinates");
}
} catch (Exception e) {
resultText.setText("Error reverse geocoding");
}
}
}

How It Works:

1. Geocoding (Address to Coordinates):


o The user enters an address in the EditText.
o When the Geocode button is clicked, the app fetches latitude and longitude using
geocoder.getFromLocationName().
o The result is displayed in the TextView.
2. Reverse Geocoding (Coordinates to Address):
o The user enters coordinates (e.g., "40.7128, -74.0060") in the EditText.
o When the Reverse Geocode button is clicked, the app fetches the address using
geocoder.getFromLocation().
o The result is displayed in the TextView.

Step 4: Run the App

 Geocode: Enter an address like "New York" and click Geocode to see the coordinates
(latitude and longitude).
 Reverse Geocode: Enter coordinates like "40.7128, -74.0060" and click Reverse
Geocode to see the address.

Key Points:

 This is a very basic, minimal Android app.


 Uses Geocoder to convert between addresses and coordinates.
 The TextView is used to display the results.

This is the simplest possible version of a Geocoding and Reverse Geocoding app!

Here’s a very basic and minimal example of using Bluetooth in Android — just to enable
Bluetooth and show a message if it's supported or not.

✅ Step 1: Add Bluetooth permission in AndroidManifest.xml


<uses-permission android:name="android.permission.BLUETOOTH" />
✅ Step 2: Basic layout (activity_main.xml)
<?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">

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

<TextView
android:id="@+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status will appear here"
android:layout_marginTop="20dp"/>
</LinearLayout>

✅ Step 3: Basic Java code (MainActivity.java)


import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

BluetoothAdapter bluetoothAdapter;
TextView txtStatus;

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

txtStatus = findViewById(R.id.txtStatus);
Button btnBluetooth = findViewById(R.id.btnBluetooth);

// Get Bluetooth adapter


bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter == null) {
txtStatus.setText("Bluetooth not supported on this device.");
} else {
txtStatus.setText("Bluetooth supported.");
}
// Button click to enable Bluetooth
btnBluetooth.setOnClickListener(v -> {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
} else {
txtStatus.setText("Bluetooth is already ON");
}
});
}
}

✅ What this app does:

 Checks if Bluetooth is available.


 Shows status in TextView.
 When button is clicked, it turns on Bluetooth if it's off.

Would you like the next step to include listing paired devices or scanning nearby ones?

Sure! Here's the most simple and basic example to just create a SQLite database and a table
in Android — no input, no buttons, just auto-creates when the app runs and shows a message.

✅ Goal:

Just create a database and one table when the app starts. Show a message like:
"Database and table created!"

🧱 Step 1: Layout (activity_main.xml)


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

<TextView
android:id="@+id/textMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Starting app..." />
</LinearLayout>

✅ Step 2: Java Code (MainActivity.java)


import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

TextView textMessage;

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

textMessage = findViewById(R.id.textMessage);

// Create database and table


MyDatabaseHelper dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase(); // This triggers
onCreate

textMessage.setText("Database and table created!");


}

// Very basic database helper


class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(MainActivity context) {
super(context, "MyBasicDB", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE student (id INTEGER PRIMARY KEY, name
TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
// Not used here
}
}
}

✅ What this does:

 Automatically creates a database called MyBasicDB.


 Creates a table called student.
 Shows a message on screen when done.

Would you like to add inserting data or viewing it next, still in a very simple way?

This should give you a complete, simple Android app that demonstrates both Geocoding and
Reverse Geocoding.

Here's a simple Android Text to Speech (TTS) app based on the layout in your image that
speaks the word "thanks" when a button is clicked.

✅ 1. XML Layout (activity_main.xml)


<?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:gravity="center"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Text to Speech (TTS)\nDemo"
android:textSize="18sp"
android:textAlignment="center"
android:layout_marginBottom="20dp"/>

<EditText
android:id="@+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="thanks"
android:hint="Enter text"
android:gravity="center"
android:layout_marginBottom="20dp" />
<Button
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK TO CONVERT TEXT TO SPEECH" />
</LinearLayout>

✅ 2. Java Code (MainActivity.java)


import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

TextToSpeech tts;
EditText editText;
Button btnSpeak;

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

editText = findViewById(R.id.editTextInput);
btnSpeak = findViewById(R.id.btnSpeak);

// Initialize TextToSpeech
tts = new TextToSpeech(this, status -> {
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.US); // Set language to US English
}
});

btnSpeak.setOnClickListener(v -> {
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
});
}

@Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
Here's a simple full example of a Toast message in Android using Java with a Button:

✅ Step-by-Step Example

1. activity_main.xml (UI layout)


<?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:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/showToastButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast" />
</LinearLayout>

2. MainActivity.java
package com.example.toastdemo;

import android.os.Bundle;
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 showToastButton = findViewById(R.id.showToastButton);

showToastButton.setOnClickListener(v -> {
Toast.makeText(this, "Welcome!", Toast.LENGTH_SHORT).show();
});
}
}

🧱 What Happens?

When you click the "Show Toast" button, a small popup message (Toast) appears saying:
"This is a toast message!"

Would you like a Kotlin version too?


Toast.makeText(this, "Welcome!", Toast.LENGTH_SHORT).show();

You might also like