XML Version Encoding XMLNS: XMLNS: XMLNS: :layout - Width :layout - Height :context
XML Version Encoding XMLNS: XMLNS: XMLNS: :layout - Width :layout - Height :context
Design File :
<?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="com.example.myapplication.FileHandlingActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20dp"
android:text="Enter filename" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20dp"
android:text="Enter name" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#ff0000"
android:text="Enter the ID" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:onClick="save"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"
android:onClick="read"/>
</LinearLayout>
Logic File
package com.example.myapplication;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
EditText et1,et2,et3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 =findViewById(R.id.editText);
et2 =findViewById(R.id.editText2);
et3 =findViewById(R.id.editText3);
}
public void save(View view) {
String filename=et1.getText().toString();
String data1=et2.getText().toString();
String data2=et3.getText().toString();
FileOutputStream f;
try {
f = openFileOutput(filename, Context.MODE_APPEND);
f.write(data1.getBytes());
f.write(data2.getBytes());
f.close();
Toast.makeText(this,filename + " saved", Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void read(View view)
{
String filename=et1.getText().toString();
StringBuffer sb = new StringBuffer();
try
{
BufferedReader inputReader = new BufferedReader(new InputStreamReader(
openFileInput(filename)));
String inputString=inputReader.readLine();
while (inputString != null) {
sb.append(inputString + "\n");
inputString=inputReader.readLine()
}
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),sb.toString(),Toast.LENGTH_LONG).show();
}
}
II) Custom Grid View Using Base Adapters
Design File :
<LinearLayout
xmlns:android="http://schemas.android.com/ap
k/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gv2"
android:numColumns="3"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/ap
k/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv1"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
ma = new MyAdapter();
gv2.setAdapter(ma);
gv2.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Student st = al.get(position);
Toast.makeText(GridViewDemo2.this, "Rollno : " + st.rollno + "\nName : " +
st.name + "\nMarks : " + st.marks, Toast.LENGTH_SHORT).show();
}
});
}