Cleaned Code (1)
Cleaned Code (1)
Manifest File:
<uses-feature android:name="android.hardware.telephony"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:ems="10"
android:inputType="text"
android:hint="Phone Number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Message" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java File:
package com.example.sms_send;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = findViewById(R.id.send);
msg = findViewById(R.id.sms);
phone = findViewById(R.id.phone);
requestMessage();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
sendSMS();
} else {
requestMessage();
}
}
});
}
--------------------------------------
XML Layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
Java File:
package com.example.prg30;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toEditText = findViewById(R.id.toEditText);
ccEditText = findViewById(R.id.ccEditText);
bccEditText = findViewById(R.id.bccEditText);
subjectEditText = findViewById(R.id.subjectEditText);
bodyEditText = findViewById(R.id.bodyEditText);
sendButton = findViewById(R.id.sendButton);
sendButton.setOnClickListener(v -> {
String to = toEditText.getText().toString();
String cc = ccEditText.getText().toString();
String bcc = bccEditText.getText().toString();
String subject = subjectEditText.getText().toString();
String body = bodyEditText.getText().toString();
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
});
}
}