0% found this document useful (0 votes)
112 views25 pages

SDP Record

The document describes 10 experiments conducted on smart device programming. The experiments include creating apps that: 1. Toast a message when a button is clicked 2. Toast username and password when a button is clicked 3. Calculate and toast the factorial of a number 4. Determine if a number is odd or even and toast the result 5. Find the largest of 3 numbers and toast the result The document provides the code and screenshots for each experiment conducted on May 10, 2021.

Uploaded by

arjun aju
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)
112 views25 pages

SDP Record

The document describes 10 experiments conducted on smart device programming. The experiments include creating apps that: 1. Toast a message when a button is clicked 2. Toast username and password when a button is clicked 3. Calculate and toast the factorial of a number 4. Determine if a number is odd or even and toast the result 5. Find the largest of 3 numbers and toast the result The document provides the code and screenshots for each experiment conducted on May 10, 2021.

Uploaded by

arjun aju
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/ 25

Smart Device Programming

INDEX
SI No NAME OF EXPERIMENT DATE PAGE No SIGN

1 Toast Message of Button Click 05/10/2021 2-3

2 Toast Username & Password 05/10/2021 4-5

3 Factorial of a Number 05/10/2021 6-7

4 Odd or Even number checking 05/10/2021 8-9

5 Largest among 3 numbers 05/10/2021 10-12

6 Demonstration of Intent 05/10/2021 13-14

7 Username & Password second activity 05/10/2021 15-17

8 Demonstration of Checkbox 05/10/2021 18-20

9 Demonstration of Radio Button 05/10/2021 21-22

10 Store & Retrieve data to a file 05/10/2021 23-25

1
Smart Device Programming
Expt No : 1
Date : 05/10/2021

TOAST A MESSAGE ON BUTTON CLICK


AIM

To create an android application that toast a message on button click.

PROGRAM

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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="click me"/>

MainActivity.java
package com.google.toast1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.toast1.R;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"GPTCKOTHAMANGALAM",Toast.LENGTH_LONG).
show();
}
});
}
}
2
Smart Device Programming

OUTPUT

RESULT
The required output is obtained and verified.

3
Smart Device Programming
Expt No : 2
Date : 05/10/2021

TOAST USERNAME AND PASSWORD


AIM

To create an android application that toast both username and password on button click.

PROGRAM

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/user"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="@+id/btn"
android:text="SUBMIT"/>
</LinearLayout>

MainActivity.java
package com.google.user;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
4
Smart Device Programming
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
EditText user,pass;
String usr,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
user=findViewById(R.id.user);
pass=findViewById(R.id.pass);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usr= user.getText().toString();
pwd=pass.getText().toString();
Toast.makeText(getApplicationContext(),"Username:"+usr,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),"Password:"+pwd,Toast.LENGTH_LONG).show();
}
});
}
}

OUTPUT

RESULT
The required output is obtained and verified.

5
Smart Device Programming
Expt No : 3
Date : 05/10/2021

FACTORIAL OF A NUMBER
AIM

To create an android application to find factorial of a number

PROGRAM

Activity_main.xml

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


<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "Enter number"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/n"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial"
android:id="@+id/btn"/>
</LinearLayout>

MainActivity.java

package com.google.facto;
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 {
Button btn;
EditText n;
@Override
protected void onCreate(Bundle savedInstanceState) {
6
Smart Device Programming
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
n=findViewById(R.id.n);
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
int num=Integer.parseInt(n.getText().toString());
int fact=1;
for(int i=1;i<=num;i++)
{
fact=fact*i;
}
Toast.makeText(getApplicationContext(),"Factorial="+fact,Toast.LENGTH_LONG).show();
}
});
}
}

OUTPUT

RESULT
The required output is obtained and verified.

7
Smart Device Programming
Expt No : 4
Date : 05/10/2021

ODD OR EVEN
AIM
To create an android application to find give number is even or odd.

PROGRAM

Activity_main.xml

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


<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number"
android:textAlignment="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/n"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:textAlignment="center"
android:id="@+id/btn"
android:text="Click me"/>
</LinearLayout>

MainActivity.java

package com.google.evenandodd;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
8
Smart Device Programming
String res;
Button btn;
EditText n;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
n=findViewById(R.id.n);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = Integer.parseInt(n.getText().toString());
if (num % 2 == 0) {
res = "Number is even";
} else {
res = "Number is odd";
}
Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
}
});
}
}

OUTPUT

RESULT
The required output is obtained and verified.

9
Smart Device Programming
Expt No :5
Date :05/10/2021
LARGEST AMONG 3 NUMBER
AIM
To create an android application to find largest among three numbers.

PROGRAM

Activity_main.xml

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


<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
android:background="#000"
tools:context=".MainActivity">
<EditText
android:id="@+id/num1"
android:hint="Enter the First Number"
android:textColorHint="#fff"
android:textColor="#fff"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/num2"
android:hint="Enter the Second Number"
android:textColorHint="#fff"
android:textColor="#fff"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/num3"
android:hint="Enter the Third Number"
android:textColorHint="#fff"
android:textColor="#fff"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn"
android:text="CHECK"
android:textColor="#fff"
android:background="#009688"
android:layout_width="match_parent"
10
Smart Device Programming
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/res"
android:textColor="#fff"
android:textSize="20dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
EditText num1, num2, num3;
TextView res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
num3 = findViewById(R.id.num3);
res = findViewById(R.id.res);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
int n3 = Integer.parseInt(num3.getText().toString());
int n;
if ((n1>n2) && (n1>n3)){
n = n1;
res.setText("Largest Number is " + n);
} else if(n2>n3){
n=n2;
res.setText("Largest Number is " + n);
}else{
n=n3;
res.setText("Largest Number is " + n);
}
}
});
}
11
Smart Device Programming
}

OUTPUT

RESULT
The required output is obtained and verified.

12
Smart Device Programming
Expt No:6
Date :05/10/2021
DEMONSTRATION OF INTENT
AIM
To create an android application to navigate to another page on Button click.

PROGRAM
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:text="SUBMIT"
android:textSize="25dp"
android:background="#000"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java
package com.example.intentapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}

13
Smart Device Programming
Activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".Second"><TextView
android:text="SECOND ACTIVITY"
android:textSize="25dp"
android:textColor="#009688"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

Second.java
package com.example.intentapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Second extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
OUTPUT

RESULT
The required output is obtained and verified.
14
Smart Device Programming
Expt No :7
Date :05/10/2021

USERNAME & PASSWORD SECOND ACTIVITY


AIM
To create an android application to toast the username & password entered on first activity on
second activity on button click.

PROGRAM
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/user"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click Me!"
android:id="@+id/btn"/>
</LinearLayout>

MainActivity.java

package com.google.toast2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
15
Smart Device Programming
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button btn;
EditText user,pass;
String usr,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
user=findViewById(R.id.user);
pass=findViewById(R.id.pass);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usr=user.getText().toString();
pwd=pass.getText().toString();
Intent intent=new Intent(MainActivity.this,second1.class);
intent.putExtra("username",usr);
intent.putExtra("password",pwd);
startActivity(intent);
}
});
}
}

Activity_main.xml

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


<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".second1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textSize="50dp"/>
</LinearLayout>

Second1.java

package com.google.toast2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class second1 extends AppCompatActivity {
16
Smart Device Programming
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second1);
Intent intent=getIntent();
String data=intent.getStringExtra("username");
String data1=intent.getStringExtra("password");
Toast.makeText(getApplicationContext(), "username
is:"+data,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "password is"+data1,
Toast.LENGTH_SHORT).show();
}
}

OUTPUT

RESULT
The required output is obtained and verified.

17
Smart Device Programming
Expt No :8
Date :05/10/2021

DEMONSTRATION OF CHECKBOX

AIM
To create an android application that demonstrate checkbox.

PROGRAM
Activity_main.xml

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


<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/cb1"
android:checked="false"
android:text="IOS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/cb2"
android:checked="false"
android:text="Android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/cb3"
android:checked="false"
android:text="Windows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn"
android:text="SUBMIT"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity.java

package com.google.check;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
18
Smart Device Programming
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
CheckBox cb1, cb2, cb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
cb1 = findViewById(R.id.cb1);
cb2 = findViewById(R.id.cb2);
cb3 = findViewById(R.id.cb3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String d1 = cb1.getText().toString();
String d2 = cb2.getText().toString();
String d3 = cb3.getText().toString();
if (cb1.isChecked()){
Toast.makeText(getApplicationContext(), d1, Toast.LENGTH_SHORT).show();
}
if (cb2.isChecked()){
Toast.makeText(getApplicationContext(), d2, Toast.LENGTH_SHORT).show();
}
if (cb3.isChecked()){
Toast.makeText(getApplicationContext(), d3, Toast.LENGTH_SHORT).show();
}
}
});
}
}

19
Smart Device Programming
OUTPUT

RESULT
The required output is obtained and verified.

20
Smart Device Programming

Expt No :9
Date :05/10/2021

DEMONSTRATION OF RADIO-BUTTON
AIM
To create an android application the demonstrate radio-button.

PROGRAM
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<RadioGroup
android:gravity="center"
android:id="@+id/radioid"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:text="Male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/female"
android:text="Female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/other"
android:text="Other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<Button
android:id="@+id/btn"
android:text="SUBMIT"
android:background="#03A9F4"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity.java
package com.google.radio;
21
Smart Device Programming
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
RadioButton radiobutton;
RadioGroup radioid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
radioid = findViewById(R.id.radioid);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = radioid.getCheckedRadioButtonId();
radiobutton = findViewById(id);
String data = radiobutton.getText().toString();
Toast.makeText(getApplicationContext(), "Selected is " + data,
Toast.LENGTH_SHORT).show();
}
});
}
}
OUTPUT

RESULT
The required output is obtained and verified.
22
Smart Device Programming
Expt No :10
Date :05/10/2021

STORE AND RETRIEVE DATA TO A FILE


AIM
To create an android application that store and retrieve data to a file.

PROGRAM
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="top"
android:layout_marginTop="20dp"
android:hint="Enter the text"
android:id="@+id/ed1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="write data to file"
android:onClick="WriteMessage"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="read data from file"
android:onClick="ReadMessage"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Hello World!"
android:id="@+id/t1"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:textAppearance="?android:textAppearanceLarge"/>
</LinearLayout>

23
Smart Device Programming
MainAvtivity.java

package com.google.internal;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelStoreOwner;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText ed1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.ed1);
t1 = findViewById(R.id.t1);
t1.setVisibility(View.GONE);
}
public void WriteMessage(View view) throws IOException {
String Message = ed1.getText().toString();
String file_name = "hello_file";
FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_PRIVATE);
try {
fileOutputStream.write(Message.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Message saved",
Toast.LENGTH_LONG).show();
ed1.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
public void ReadMessage(View view)
{
try {
String Message;
FileInputStream fileIntputStream=openFileInput("hello_file");
InputStreamReader inputStreamReader = new InputStreamReader(fileIntputStream);
StringBuffer stringBuffer = new StringBuffer();
BufferedReader br=new BufferedReader(inputStreamReader);
while ((Message = br.readLine())!= null) {
stringBuffer.append(Message + "\n");
24
Smart Device Programming
}
t1.setText(stringBuffer.toString());
t1.setVisibility(View.VISIBLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

OUTPUT

RESULT
The required output is obtained and verified.

25

You might also like