Thứ Sáu, 12 tháng 12, 2014

http://stackoverflow.com/questions/22574645/phonegap-android-create-file-if-not-exists-and-write-to-it

var savedFS;

function gotFS(fileSystem) {
    savedFS = fileSystem;
    fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
}

function gotFS2(fileSystem) {
    alert('trying again');
    fileSystem.root.getFile("test.txt", {create:true}, gotFileEntry, fail);
}

function fail(error) {
    if(error.code == 1) {
        alert('not found');
        gotFS2(savedFS);
    }
    alert(error.code);
}
http://stackoverflow.com/questions/11077303/how-to-create-a-directory-and-file-in-that-directory-using-phonegap-file-api

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
   fileSystem.root.getDirectory("data", {create: true}, gotDir);
}

function gotDir(dirEntry) {
    dirEntry.getFile("lockfile.txt", {create: true, exclusive: true}, gotFile);
}

function gotFile(fileEntry) {
    // Do something with fileEntry here
}

Worklight Create Folder Write File

http://stackoverflow.com/questions/15545112/combining-native-app-with-worklight-project

Here's a quick example of the File I/O API for Writing a file:
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);  
        writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample'");
            writer.seek(4);
            writer.write(" different text");
            writer.onwriteend = function(evt){
                console.log("contents of file now 'some different text'");
            }
        };
    };
    writer.write("some sample text");
}

function fail(error) {
    console.log(error.code);
}
Here's an example of Reading a file:
// Wait for Cordova to load
//
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

// Cordova is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.file(gotFile, fail);
}

function gotFile(file){
    readDataUrl(file);
    readAsText(file);
}

function readDataUrl(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as data URL");
        console.log(evt.target.result);
    };
    reader.readAsDataURL(file);
}

function readAsText(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as text");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
}

function fail(evt) {
    console.log(evt.target.error.code);
}

Thứ Sáu, 28 tháng 11, 2014

Creating Custom Views In Android

http://javatechig.com/android/creating-custom-views-in-android-tutorial

*http://www.vogella.com/tutorials/AndroidCustomViews/article.html

1. Different Android Layouts

A layout defines the visual structure for a user interface. It defines the way View’s can be placed on an Activity. Layouts can be declared in two ways.
  1. Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and sub classes, such as those for widgets and layouts.
  2. Instantiate layout elements at run time. Your application can create and manipulate View and ViewGroup objects programmatically.
Below are the ViewGroup classes that android supports
Below are some of the most common used Android View classes

2. Why Custom Views in Android ?

All the above View classes and ViewGroup classes are good enough to create an solid android app. However, sometimes your app may have unique needs that aren’t covered by the built-in views. Such cases you will end up creating an custom View in android. The below example shows you how to create your own views that are robust and reusable.

3. Creating your custom View class

Declaring a custom View is much similar to declaring any other class in Java. While designing an custom class keep the following things in mind
  1. It should provide some easy to use interface and should use the memory efficiently.
  2. A Custom View should conform to Android standards
  3. You can provide custom styleable attributes that can be configurable from Android XML layouts
  4. Your View class should be compatible with multiple Android platforms
All the default View classes that are available in android extends View. The same way your custom View can directly extend the View class or you may extend one of the default View controls such as TextView. Now, override the constructor that takes a Context and an AttributeSet object as parameters.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class LovelyView extends LinearLayout {

 public LovelyView(Context context) {
  super(context);  
 }

 public LovelyView(Context context, AttributeSet attrs) {
  super(context, attrs);  
 }

 public LovelyView(Context context, AttributeSet attrs, int defStyle) {
  this(context, attrs);  
 } 
}

4. Define Custom Attributes

You can provide custom styleable attributes that can be configurable from Android XML layouts. These attributes will control its appearance and behavior of View. You can create a values/attr.xml file in your project. To enable this behavior in your custom view, you can define custom attributes for your view in a resource element in attr.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="LovelyView">
        <attr name="leftLabel" format="string" />
        <attr name="rightLabel" format="string" />
        <attr name="leftLabelStyle" format="reference" />
        <attr name="rightLabelStyle" format="reference" />
    </declare-styleable>

</resources>
In the above xml file, LovelyView is the name of the style. and leftLabelrightLabelleftLabelStyle andrightLabelStyleare the style attributes defines.
Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to thehttp://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/(your package name)
Here’s how to use the attributes defined for LovelyView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.customview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <com.example.customview.LovelyView
        android:id="@+id/lovelyView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        custom:leftLabel=""
        custom:leftLabelStyle="@style/leftTextStyle"
        custom:rightLabel=""
        custom:rightLabelStyle="@style/rightTextStyle" />

    <com.example.customview.LovelyView
        android:id="@+id/lovelyView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        custom:leftLabel="Hello Mr:"
        custom:leftLabelStyle="@style/leftTextStyle"
        custom:rightLabel="Whats Up Buddy!"
        custom:rightLabelStyle="@style/rightTextStyle" />

</LinearLayout>
Notice that, In the above code snippet, we are using the fully qualified name of the custom view class. If your view class is an inner class, you must further qualify it with the name of the view’s outer class.

5. Apply Custom Attributes

In the above two steps, we have configured the View stylable attributes in the XML. When a view is created from an XML layout, all of the attributes in the XML tag are read from the resource bundle and passed into the view’s constructor as an AttributeSet. We can read the attributes by calling obtainStyledAttributes() method.
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
  R.styleable.LovelyView, 0, 0);
try {
 // get the text and colors specified using the names in attrs.xml
 leftLabel = a.getString(R.styleable.LovelyView_leftLabel);
 rightLabel = a.getString(R.styleable.LovelyView_rightLabel);
 leftStyle = a.getResourceId(R.styleable.LovelyView_leftLabelStyle, android.R.style.TextAppearance_DeviceDefault);
 rightStyle = a.getResourceId(R.styleable.LovelyView_rightLabelStyle, android.R.style.TextAppearance_DeviceDefault);

} finally {
 a.recycle();
}

LayoutInflater.from(context).inflate(R.layout.key_value_layout, this);

//left text view
leftTextView = (TextView) this.findViewById(R.id.leftTextView);
leftTextView.setText(leftLabel);
leftTextView.setTextAppearance(context, leftStyle);

//right text view
rightTextView = (TextView) this.findViewById(R.id.rightTextView);
rightTextView.setText(rightLabel);
rightTextView.setTextAppearance(context, rightStyle);
Below is the complete View class code
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LovelyView extends LinearLayout {
 private String leftLabel = "";
 private String rightLabel = "";
 private TextView leftTextView;
 private TextView rightTextView;
 private int leftStyle ;
 private int rightStyle;

 public LovelyView(Context context) {
  super(context);
  LayoutInflater.from(context).inflate(R.layout.key_value_layout, this);
 }

 public LovelyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initViews(context, attrs);
 }

 public LovelyView(Context context, AttributeSet attrs, int defStyle) {
  this(context, attrs);
  initViews(context, attrs);
 }

 private void initViews(Context context, AttributeSet attrs) {
  TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
    R.styleable.LovelyView, 0, 0);

  try {
   // get the text and colors specified using the names in attrs.xml
   leftLabel = a.getString(R.styleable.LovelyView_leftLabel);
   rightLabel = a.getString(R.styleable.LovelyView_rightLabel);
   leftStyle = a.getResourceId(R.styleable.LovelyView_leftLabelStyle, android.R.style.TextAppearance_DeviceDefault);
   rightStyle = a.getResourceId(R.styleable.LovelyView_rightLabelStyle, android.R.style.TextAppearance_DeviceDefault);

  } finally {
   a.recycle();
  }

  LayoutInflater.from(context).inflate(R.layout.key_value_layout, this);

  //left text view
  leftTextView = (TextView) this.findViewById(R.id.leftTextView);
  leftTextView.setText(leftLabel);
  leftTextView.setTextAppearance(context, leftStyle);

  //right text view
  rightTextView = (TextView) this.findViewById(R.id.rightTextView);
  rightTextView.setText(rightLabel);
  rightTextView.setTextAppearance(context, rightStyle);
 }

 public String getLeftLabel() {
  return leftLabel;
 }

 public void setLeftLabel(String leftLabel) {
  this.leftLabel = leftLabel;
  if(leftTextView!=null){
   leftTextView.setText(leftLabel);
  }
 }

 public String getRightLabel() {
  return rightLabel;
 }

 public void setRightLabel(String rightLabel) {
  this.rightLabel = rightLabel;
  if(rightTextView!=null){
   rightTextView.setText(rightLabel);
  }
 }
}