Programs 3,10,6
Programs 3,10,6
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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"
android:id="@+id/btn_create" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OPEN"
android:id="@+id/btn_open" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:height="200dp"
android:id="@+id/txt_inp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@+id/btn_save" />
</LinearLayout>
MainActivity.java
package com.example.textbox;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btncreate=findViewById(R.id.btn_create);
btncreate.setOnClickListener(this);
btnopen=findViewById(R.id.btn_open);
btnopen.setOnClickListener(this);
btnsave=findViewById(R.id.btn_save);
btnsave.setOnClickListener(this);
txtinp=findViewById(R.id.txt_inp);
@Override
public void onClick(View v) {
if(v.equals(btncreate)) {
String text=txtinp.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
txtinp.setText(" ");
Toast.makeText(this, "File Saved "+ getFilesDir().getName()+ "
"+FILE_NAME, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
else if(v.equals(btnsave)) {
String text=txtinp.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
txtinp.setText(" ");
Toast.makeText(this, "File Saved"+ getFilesDir().getName()
+FILE_NAME, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
else if(v.equals(btnopen)) {
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.textbox"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Textbox"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
Json 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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_1"
android:text="Parse JSON" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textColor="@color/black"
android:id="@+id/txt_result" />
</LinearLayout>
Mainactivity.java
package com.example.json_prg;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnjson = findViewById(R.id.btn_1);
btnjson.setOnClickListener(this);
txtresult = findViewById(R.id.txt_result);
}
}
}
}
example.json
[
{
"name":"Mysore" ,
"latitude": "92",
"longitude": "74",
"temperature":"24"
},
{
"name":"Bangalore" ,
"latitude": "36",
"longitude": "69",
"temperature":"20"
},
{
"name":"Udupi" ,
"latitude": "24",
"longitude": "42",
"temperature":"25"
},
{
"name":"Goa" ,
"latitude": "23",
"longitude": "63",
"temperature":"22"
}
]
Message program
Activity_main
[
{
"name":"Mysore" ,
"latitude": "92",
"longitude": "74",
"temperature":"24"
},
{
"name":"Bangalore" ,
"latitude": "36",
"longitude": "69",
"temperature":"20"
},
{
"name":"Udupi" ,
"latitude": "24",
"longitude": "42",
"temperature":"25"
},
{
"name":"Goa" ,
"latitude": "23",
"longitude": "63",
"temperature":"22"
}
]
MainActivity.java
package com.example.message_num;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtnum=findViewById(R.id.txt_num);
txtmsg=findViewById(R.id.txt_msg);
Bundle b = getIntent().getBundleExtra("data");
if(b!=null)
{
String s1= b.getString("num");
String s2= b.getString("msg");
txtnum.setText(s1);
Toast.makeText(this, "Message Received",
Toast.LENGTH_LONG).show();
txtmsg.setText(s2);
}
}
}
MySMSReceiver
package com.example.message_num;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.message_num"
android:versionCode="1"
android:versionName="1.0"
xmlns:tools="http://schemas.android.com/tools">
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="32" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Message_num"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<receiver android:name=".MySMSReceiver"
android:exported="true">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>