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

XML Version Encoding XMLNS: XMLNS: XMLNS: :layout - Width :layout - Height :context

The document discusses two topics: 1) File reading and writing to internal storage in Android. It includes code for an activity layout with edit texts and buttons to input a file name, name, and ID. The code shows how to write this data to a file and read it back with a toast notification. 2) Creating a custom grid view with a base adapter in Android. It includes layout code for the activity and individual grid items. The code shows how to populate the grid view with an ArrayList of student objects using a custom adapter class. Tapping a grid item shows the student details in a toast.

Uploaded by

moli
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

XML Version Encoding XMLNS: XMLNS: XMLNS: :layout - Width :layout - Height :context

The document discusses two topics: 1) File reading and writing to internal storage in Android. It includes code for an activity layout with edit texts and buttons to input a file name, name, and ID. The code shows how to write this data to a file and read it back with a toast notification. 2) Creating a custom grid view with a base adapter in Android. It includes layout code for the activity and individual grid items. The code shows how to populate the grid view with an ArrayList of student objects using a custom adapter class. Tapping a grid item shows the student details in a toast.

Uploaded by

moli
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

I) File Reading and Writing into internal Storage

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;

public class MainActivity extends AppCompatActivity {

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>

Design File for custom GridView (single_student_record_design.xml)


Create this file in res folder : right click layout folder -> layout Resource File->
File_name

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

Logic File of GridViewDemo2.java


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class GridViewDemo2 extends AppCompatActivity
{
GridView gv2;
MyAdapter ma;
ArrayList<Student> al = new ArrayList<Student>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view_demo2);
gv2 = (GridView) findViewById(R.id.gv2);

al.add(new Student(1, "John", 11.11));


al.add(new Student(2, "Tom", 22.12));
al.add(new Student(3, "Paul", 33.13));

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

class MyAdapter extends BaseAdapter


{
@Override
public int getCount()
{
return al.size();
}
@Override
public Object getItem(int position)
{
return al.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView =
inflater.inflate(R.layout.single_student_record_design,parent,false);
Student st = (Student)getItem(position);
return convertView;
}
}
class Student
{
int rollno;
String name;
double marks;

public Student(int rollno, String name, double marks)


{
this.rollno = rollno;
this.name = name;
this.marks = marks;
}
}
}

You might also like