Create an application to send message between two emulators.
MainActivity.java
MainActivity.java
package com.example.assignment21;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements OnClickListener, TextWatcher {
EditText txtnumber, txtmessage;
Button btnsend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtnumber = (EditText) findViewById(R.id.txtnumber);
txtmessage = (EditText) findViewById(R.id.txtmsg);
btnsend = (Button) findViewById(R.id.btnsend);
txtmessage.setEnabled(false);
btnsend.setEnabled(false);
btnsend.setOnClickListener(this);
txtnumber.addTextChangedListener(this);
}
@Override
public void onClick(View v) {
try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(txtnumber.getText().toString(), null, txtmessage.getText().toString(), null, null);
txtnumber.setText("");
txtmessage.setText("");
btnsend.setEnabled(false);
txtmessage.setEnabled(false);
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, e.toString(), 1).show();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if((txtmessage.getText().toString().equals("")))
{
btnsend.setEnabled(true);
}
txtmessage.setEnabled(true);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Enter Number :"
android:layout_y="27dp"
android:layout_x="3dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView1"/>
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Message :"
android:layout_y="110dp"
android:layout_x="2dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView2"/>
<EditText
android:id="@+id/txtmsg"
android:layout_width="388dp"
android:layout_height="117dp"
android:layout_x="0dp"
android:layout_y="160dp"
android:hint="Type Message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/btnsend"
android:layout_width="81dp"
android:layout_height="wrap_content"
android:layout_x="156dp"
android:layout_y="310dp"
android:text="Send" />
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_y="65dp"
android:layout_x="0dp"
android:id="@+id/txtnumber"
android:hint="Enter Phone Number"
android:inputType="phone">
</EditText>
</AbsoluteLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment21">
<uses-permission android:name="android.permission.SEND_SMS"/>
<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>
No comments:
Post a Comment