0% found this document useful (0 votes)
25 views

PB2 Program 4

The document describes an Android application that allows users to enter text into a text field, save the text to a file by clicking a "Save File" button, and load previously saved text by clicking an "Open File" button. The application layout is defined using XML code and the functionality is implemented through Java code. When the "Save File" button is clicked, the text from the text field is written to an internal file. When the "Open File" button is clicked, any text previously saved to the file is loaded and displayed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

PB2 Program 4

The document describes an Android application that allows users to enter text into a text field, save the text to a file by clicking a "Save File" button, and load previously saved text by clicking an "Open File" button. The application layout is defined using XML code and the functionality is implemented through Java code. When the "Save File" button is clicked, the text from the text field is written to an internal file. When the "Open File" button is clicked, any text previously saved to the file is loaded and displayed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1 Program 11: File Application

XML Code:
<?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">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/save_file"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25"
app:layout_constraintVertical_chainStyle="packed">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/text_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:hint="Enter some text"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/save_file"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Save File"
app:layout_constraintBottom_toTopOf="@+id/open_file_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_input_layout" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/open_file_text"
style="@style/Widget.MaterialComponents.TextView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
Mobile Application Development – 18CSMP68
2 Program 11: File Application

android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/purple_500"
app:layout_constraintBottom_toTopOf="@+id/open_file"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/save_file" />

<com.google.android.material.button.MaterialButton
android:id="@+id/open_file"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Open File"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/open_file_text" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package `in`.edu.REC.fileapplication

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import java.io.*

class MainActivity : AppCompatActivity() {

private lateinit var text_input: EditText


private lateinit var saveBtn : Button
private lateinit var openBtn : Button
private lateinit var open_text : TextView

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

text_input = findViewById(R.id.text_input)
open_text = findViewById(R.id.open_file_text)

openBtn = findViewById(R.id.open_file)
saveBtn = findViewById(R.id.save_file)

Mobile Application Development – 18CSMP68


3 Program 11: File Application

saveBtn.setOnClickListener {
var data : String = text_input.text.toString()
writeToFile(data, this)
Toast.makeText(this, "File Saved !", Toast.LENGTH_SHORT).show()
}

openBtn.setOnClickListener {
var data = readFromFile(this)
open_text.text = data
}

private fun writeToFile(data: String, context: Context) {


try {
val outputStreamWriter =
OutputStreamWriter(context.openFileOutput("file.txt",
Context.MODE_PRIVATE))
outputStreamWriter.write(data)
outputStreamWriter.close()
} catch (e: IOException) {
Log.e("Exception", "File write failed: " + e.toString())
}
}

private fun readFromFile(context: Context): String? {


var str = ""
try {
val inputStream: InputStream =
context.openFileInput("file.txt")
if (inputStream != null) {
val inputStreamReader = InputStreamReader(inputStream)
val bufferedReader = BufferedReader(inputStreamReader)
var receiveString: String? = ""
val stringBuilder = StringBuilder()
while (bufferedReader.readLine().also { receiveString = it
} != null) {
stringBuilder.append("\n").append(receiveString)
}
inputStream.close()
str = stringBuilder.toString()
}
} catch (e: FileNotFoundException) {
Log.e("Main Activity", "File not found: " + e.toString())
} catch (e: IOException) {
Log.e("Main Activity", "Can not read file: $e")
}
return str
}
}

Mobile Application Development – 18CSMP68


4 Program 11: File Application

Output:

Mobile Application Development – 18CSMP68


5 Program 11: File Application

Mobile Application Development – 18CSMP68

You might also like