Saturday 22 October 2011

File Storage

External Storage:

package com.icapes.file;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class FileStorage extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


final EditText edit = (EditText) findViewById(R.id.EditText01);
Button buttonWrite = (Button) findViewById(R.id.Button02);


final TextView reader = (TextView) findViewById(R.id.TextView01);
Button buttonRead = (Button) findViewById(R.id.Button01);


buttonWrite.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
writeToFile(edit.getText().toString());


}
});


buttonRead.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
readToTextView(reader);


}
});


}


private void writeToFile(String text) {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath();
System.out.println("Storage root directory " + path);
path = path + "/icapes";
System.out.println("Application storage directory " + path);


File file = new File(path);
if (!file.exists()) {
file.mkdir();
}


path = path + "/myfile.txt";
System.out.println("Actual file path: " + path);


File myFile = new File(path);


FileWriter writer;
try {
writer = new FileWriter(myFile);
writer.write(text);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


private void readToTextView(TextView tv) {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path = path + "/icapes/myfile.txt";
StringBuilder text = new StringBuilder();
   String NL = System.getProperty("line.separator");
   Scanner scanner = null;
try {
scanner = new Scanner(new FileInputStream(new File(path)), "UTF-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   try {
     while (scanner.hasNextLine()){
       text.append(scanner.nextLine() + NL);
     }
   }
   finally{
     scanner.close();
   }
   
   tv.setText(text.toString());
}
}

File path:



File output:

File Storage

Internal Storage:



package file.com.kumar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class FileActivity extends Activity {
Button write,read;
EditText edit;
TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        write=(Button)findViewById(R.id.Button01);
        read=(Button)findViewById(R.id.Button02);
        edit=(EditText)findViewById(R.id.EditText01);
        tv=(TextView)findViewById(R.id.TextView01);
        
        
        write.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
writetofile(edit.getText().toString());

}
});    
        read.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
readtofile(tv);

}
}); 
      }
    public void writetofile(String write){
    String path=Environment.getDataDirectory().getAbsolutePath();
    path=path +"/data/file.com.kumar/newfile";
    File f=new File(path);
    if(!f.exists()){
    f.mkdir();
    }
    path=path +"/filetext.txt";
    File myfile=new File(path);
    FileWriter writer;
try {
writer = new FileWriter(myfile);
writer.write(write);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    
    public void readtofile(TextView tv){
    String path=Environment.getDataDirectory().getAbsolutePath();
    path=path +"/data/file.com.kumar/newfile/filetext.txt";
    StringBuilder text = new StringBuilder();
   String NL = System.getProperty("line.separator");
   Scanner scanner = null;
try {
scanner = new Scanner(new FileInputStream(new File(path)), "UTF-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   try {
     while (scanner.hasNextLine()){
       text.append(scanner.nextLine() + NL);
     }
   }
   finally{
     scanner.close();
   }
   
    
   tv.setText(text.toString());
}
}
   
File storage path:


    Output:


Shared Preference

First Class:





import android.os.Bundle;
import android.widget.Toast;


public class SharedActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Preference p=new Preference(this);
        p. Saved pref("KUMAR MLA COIMBATORE");
        Toast.makeText(this, p.Getprif(), Toast.LENGTH_LONG).show();
    }
}




Second Class:



import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class Preference {
private final static String PREF_FILE="pref";
SharedPreferences preference;
public Preference(Context context){
preference=context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
}
public void Savedpref(String str){
Editor et=preference.edit();
et.putString("username",str);
et.commit();
}
public String Getprif(){
return preference.getString("username","Not Exit");
}


}



Monday 10 October 2011

LinearLayout


LinearLayout organizes elements along a single line. You specify whether that line is verticle or horizontal using android:orientation. Here is a sample Layout XML using LinearLayout.
Ex:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <Button
      android:id="@+id/backbutton"
      android:text="Back"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <TextView
      android:text="First Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <EditText
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
<TextView
      android:text="Last Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <EditText
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>



ANDROID LAYOUTS


An Android layout is a class that handles arranging the way its children appear on the screen.  Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts.  You could also create your own custom layout by making a class that inherits from ViewGroup.
The standard Layouts are:
                         AbsoluteLayout
                         FrameLayout
                         LinearLayout
                         RelativeLayout
                         TableLayout

AbsoluteLayout
 AbsoluteLayout is based on the simple idea of placing each control at an absolute position.  You specify the exact x and y coordinates on the screen for each control.  This is not recommended for most UI development (in fact AbsoluteLayout is currently deprecated) since absolutely positioning every element on the screen makes an inflexible UI that is much more difficult to maintain.  Consider what happens if a control needs to be added to the UI. You would have to change the position of every single element that is shifted by the new control
Exaple:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
      android:id="@+id/backbutton"
      android:text="Back"
      android:layout_x="10px"
      android:layout_y="5px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <TextView
      android:layout_x="10px"
      android:layout_y="110px"
      android:text="First Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <EditText
      android:layout_x="150px"
      android:layout_y="100px"
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
   
</AbsoluteLayout>

FrameLayout:

FrameLayout is designed to display a single item at a time. You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. I have created a simple XML layout using FrameLayout that shows how this works.
ex:
<FrameLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <ImageView
  android:src="@drawable/icon"
  android:scaleType="fitCenter"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"/>
  <TextView
  android:text="Learn-Android.com"
  android:textSize="24sp"
  android:textColor="#000000"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:gravity="center"/>
</FrameLayout>




Saturday 1 October 2011

ANDROID INTRODUCTION

Android fundamental and System Requirement.
Software Installation
How to Create Android  Project :CLICK HERE

Android Listview Onclick

Listview OnClick Example -
Your Java code should look like
[sourcecode language="java"]
public class ListviewOnclickExample extends Activity
{
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople","J2ME", "Listview","ArrayAdapter","ListItem","Us","UK","India"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(ListviewOnclickExample.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = "+lv1.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
}
}
The output will look like
listviewonclickexamplelistviewonclickexample1

Android Listview


Today, we are going to see about a simple listview example. In Android, Listview is used to show a list of items in a vertically scrolling list.  Learn a listview of android array in this tutorial.
For instance, in a Registration form when we are selecting professions a list of items will be displayed. We can use Listview to display the list of items.
Your XML file should look like
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
[/sourcecode]
Your Java code looks like
[sourcecode language="java"]
public class ListviewExample extends Activity
{
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
}
}
The output will look like
listviewexample