0% found this document useful (0 votes)
32 views10 pages

Practical27 30

The document discusses practical exercises related to creating login forms and sending SMS messages in Android. It includes code snippets to create a login form with validation and send SMS messages. The exercises focus on building applications with common UI elements and features in Android.

Uploaded by

Sayyam Vaishnav
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)
32 views10 pages

Practical27 30

The document discusses practical exercises related to creating login forms and sending SMS messages in Android. It includes code snippets to create a login form with validation and send SMS messages. The exercises focus on building applications with common UI elements and features in Android.

Uploaded by

Sayyam Vaishnav
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/ 10

Practical 27

IX. Practical related questions

1. Explain the use of equals() function


equals() is used to compare two objects
2. List the important functions which are related to GUI component “Button”
Button is used take actions, and make choices
3. State the use of toast message
Toast class can be used to display some message to the user

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;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
EditText userName,passwordText;
Button loginButton, cancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText)findViewById(R.id.userNameText);
passwordText = (EditText) findViewById(R.id.passwordText);
loginButton = (Button) findViewById(R.id.loginButton);
cancelButton = (Button)findViewById(R.id.cancelButton);
loginButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}
public void onClick(View v) {
String enteredUserName = userName.getText().toString();
String enteredPassword = passwordText.getText().toString();
String userName = "Admin";
String passwd = "Admin";
String loginSuccess = "Login successful";
String cancel = "Sorry, wrong credentials";
switch(v.getId()) {

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

IX. Practical related question

1. Explain validation of user input


All input should be validated so that proper input is obtained in the program. When a phone
number is expected, programmatically check is done if the input is a number or not
2. List and explain various GUI components used to design login page
TextView – to display any label
EditView – to display text fields
Button – to take some action
3. Differentiate between TextView and EditView
TextView object is normally created to display a text to the user for information
EditText object is used to obtain input from the user. It is similar to text field

X. Exercise

1. WAP to create login form with necessary validation like length


package com.example.myapplication28;

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;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
EditText userName,passwordText;
Button loginButton, cancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText)findViewById(R.id.userNameText);
passwordText = (EditText) findViewById(R.id.passwordText);
loginButton = (Button) findViewById(R.id.loginButton);
cancelButton = (Button)findViewById(R.id.cancelButton);
loginButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}
public void onClick(View v) {
String enteredUserName = userName.getText().toString();
String enteredPassword = passwordText.getText().toString();
String userName = "Admin123";
String passwd = "Admin123";
String loginSuccess = "Login successful";
String cancel = "Sorry, wrong credentials";
switch(v.getId()) {

case R.id.loginButton:

if (enteredUserName != null || enteredPassword != null ||


!(enteredUserName.isEmpty() || enteredPassword.isEmpty())) {

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();

}
}

<?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>
Practical 29

IX. Practical related questions

1. Explain the use of SMSManager class


This calss manages operations like sending data text.
2. List the changes done in android manifest file to send and receive message
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

X. Exercise

1. Write a program to send SMS


<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/androidSMS"
android:text="Android SMS"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/phoneNumber"
android:hint="Phone Number"
android:inputType="phone"
android:layout_below="@+id/androidSMS"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/smsmsg"
android:hint="SMS Message"
android:inputType="text"
android:layout_below="@+id/phoneNumber"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sendSms"
android:text="Send SMS"
android:layout_below="@+id/smsmsg"
/>

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

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
Button sendSms;
EditText p,m;
String phoneNo, msg;
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendSms = (Button) findViewById(R.id.sendSms);
p = (EditText) findViewById(R.id.phoneNumber);
m = (EditText) findViewById(R.id.smsmsg);
sendSms.setOnClickListener(this);

@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;
}
}
}

}
}

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication29">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_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>
Practical 30

IX. Practical related questions

1. List the extra fields that can be used in sending emails


EXTRA_BCC, EXTRA_CC, EXTRA_EMAIL, EXTRA_SUBJECT

X. Exercise

1. Write a program to send email


package com.example.myapplication30;

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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener


{
Button sendEmail;
EditText toRec, subj, msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendEmail = (Button) findViewById(R.id.sendEmail);
toRec = (EditText)findViewById(R.id.to);
subj = (EditText)findViewById(R.id.subject);
msg = (EditText)findViewById(R.id.msg) ;
sendEmail.setOnClickListener(this);
}

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

You might also like