Practical27 30
Practical27 30
X. Exercise
1. write a program to create Login form and display login successful/unsuccessful toast message
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="@+id/loginText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userNameText"
android:inputType="text"
android:hint="User name"
android:layout_below="@+id/loginText"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:id="@+id/passwordText"
android:layout_below="@+id/userNameText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@+id/passwordText"
android:id="@+id/loginButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancelButton"
android:layout_below="@+id/loginButton"/>
</RelativeLayout>
package com.example.myapplication27_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
case R.id.loginButton:
if(userName.equals(enteredUserName) &&
passwd.equals(enteredPassword)) {
Toast.makeText(getApplicationContext(),loginSuccess,Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),cancel,Toast.LENGTH_SHORT).show();
}
break;
case R.id.cancelButton:
finish();
}
}
Practical 28
X. Exercise
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
case R.id.loginButton:
if (enteredUserName.length() >= 8) {
if (enteredPassword.length() >= 8) {
if (userName.equals(enteredUserName) &&
passwd.equals(enteredPassword)) {
Toast.makeText(getApplicationContext(),
loginSuccess, Toast.LENGTH_SHORT).show();
break;
} else {
Toast.makeText(getApplicationContext(), cancel,
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Length of
password less than 8", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Length of
username is less than 8", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "username or password
is empty", Toast.LENGTH_SHORT).show();
}
break;
case R.id.cancelButton:
finish();
}
}
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userNameText"
android:inputType="text"
android:hint="User name"
android:layout_below="@+id/loginText"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:id="@+id/passwordText"
android:layout_below="@+id/userNameText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@+id/passwordText"
android:id="@+id/loginButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancelButton"
android:layout_below="@+id/loginButton"/>
</RelativeLayout>
Practical 29
X. Exercise
</RelativeLayout>
package com.example.myapplication29;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
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
public void onClick(View v) {
phoneNo = p.getText().toString();
msg = m.getText().toString();
if(ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)!=
PackageManager.PERMISSION_GRANTED) {
if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.SEN
D_SMS)) {
} else {
ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.SEND_SMS},MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}
public void onRequestPermissionsResult(int requestCode,String permissions[],int
[] grantResults) {
switch(requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS:
{
if(grantResults.length >0 &&
grantResults[0]==PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,null,msg,null,null);
Toast.makeText(this, "SMS Send", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"SMS sending
failed",Toast.LENGTH_SHORT).show();
return;
}
}
}
}
}
</application>
</manifest>
Practical 30
X. Exercise
import androidx.appcompat.app.AppCompatActivity;
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 android.widget.Toast;
@Override
public void onClick(View v) {
String toR = toRec.getText().toString();
String subject = subj.getText().toString();
String ms = msg.getText().toString();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{toR});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,subject);
emailIntent.putExtra(Intent.EXTRA_TEXT,ms);
try {
startActivity(Intent.createChooser(emailIntent,"Send mail"));
finish();
Toast.makeText(MainActivity.this,"MAil
sent",Toast.LENGTH_SHORT).show();
} catch(Exception e) {
Toast.makeText(MainActivity.this,"No client
installed",Toast.LENGTH_SHORT).show();
}
}
<?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="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/to"
android:hint="To"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/subject"
android:hint="Subject"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/msg"
android:inputType="text"
android:hint="message"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/sendEmail"
android:text="Send Email"
/>
</LinearLayout>