Wednesday, 28 January 2015

Building an Android Application Using Parse

PART 1

1. Create Account in Parse.com

2. Create New application from Dashboard console
3. Go to - >"Settings" - > "Keys" can see Application ID and Clint ID .




PART 2

1. Download External Libraries from  https://parse.com/downloads/android/Parse/latest


PART 3

1. Create new application in Eclipse with min SDK version 14

2. Put External all .jar files (will get from Parse-1.5.2.zip) inside libs folder. then build application.



3. Add permission on application manifest file.

 <uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



4. Create one new java Class . Here Task.java

import com.parse.ParseClassName;

import com.parse.ParseObject;

@ParseClassName("Task")

public class Task extends ParseObject{
  public Task(){

  }

  public boolean isCompleted(){
      return getBoolean("completed");
  }
  public void setCompleted(boolean complete){
      put("completed", complete);
  }
  public String getDescription(){
      return getString("description");
  }
  public void setDescription(String description){
      put("description", description);
  } }



5. Create one new java class to fetch data from server . Here TaskAdapter

import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TaskAdapter extends ArrayAdapter<Task> {

  private Context mContext;
  private List<Task> mTasks;

  public TaskAdapter(Context context, List<Task> objects) {

      super(context, R.layout.task_row_item, objects);
      this.mContext = context;
      this.mTasks = objects;
  }
  public View getView(int position, View convertView, ViewGroup parent){
      if(convertView == null){
          LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
          convertView = mLayoutInflater.inflate(R.layout.task_row_item, null);
      }

      Task task = mTasks.get(position);


      TextView descriptionView = (TextView) convertView.findViewById(R.id.task_description);


      descriptionView.setText(task.getDescription());


      if(task.isCompleted()){

          descriptionView.setPaintFlags(descriptionView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
      }else{
          descriptionView.setPaintFlags(descriptionView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
      }
      return convertView;
  }
}



6. Create activity . Here "Todoactivity.java"  & Activity_todo.xml





7. Replace Activity_todo.xml file with given structure .

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:weightSum="2"
    tools:context=".TodoActivity" >

    <ListView

        android:id="@+id/task_list"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:stackFromBottom="true"
        android:layout_weight="1" />

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <EditText

            android:id="@+id/task_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="Enter a Task"
            android:inputType="text" >
            <requestFocus />
        </EditText>

        <Button

            android:id="@+id/submit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="createTask"
            android:text="Submit" />
    </LinearLayout>

</LinearLayout>




8. Create new xml file to show DB data in listview . Here "task_row_item.xml"

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="horizontal" >

    <TextView

        android:id="@+id/task_description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>







9. Add below functions in OnCreate method of TodoActivity.java

 Parse.initialize(this, //Application ID, //Client ID);

        ParseAnalytics.trackAppOpened(getIntent());
        ParseObject.registerSubclass(Task.class);
       
        TaskInput = (EditText) findViewById(R.id.task_input);
        ListView = (ListView) findViewById(R.id.task_list);
            
        mAdapter = new TaskAdapter(this, new ArrayList<Task>());
        ListView.setAdapter(mAdapter);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
          @Override
         public void run() {
          // TODO Auto-generated method stub
           runOnUiThread(new  Runnable() {
           public void run() {
            if(questionInterval == 5) {
             questionInterval = 0;
            }       
            questionInterval++;
            updateData();
           
           }
          });
     
         }
        }, 1000, 1000);


10. Add below function after OnCreate method

 public void createTask(View v) {

        if (TaskInput.getText().length() > 0){
            Task t = new Task();
            t.setDescription(name +":" +TaskInput.getText().toString());
            t.setCompleted(false);
            t.saveEventually();
            TaskInput.setText("");
        }
    }
   
    public void updateData(){
       ParseQuery<Task> query = ParseQuery.getQuery(Task.class);
       query.findInBackground(new FindCallback<Task>() {
             
           @SuppressWarnings("static-access")
   @Override
           public void done(List<Task> tasks, ParseException error) {
               if(tasks != null){
                   mAdapter.clear();
                   mAdapter.addAll(tasks);
                   ListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
               }
           }
       });
     }
   
===================================








No comments:

Post a Comment

How to do text writing animation?  In Android Development we do not have the option to use any other custom font in our default Tex...