Monday 28 July 2014

XML Parsing In Android


Today, we are going to create a XML Parser in Android using java language . This tutorial is 100% working tutorial and it will work smoothly i have include the required xml already in the variable .
I have used a very simple code to parse a xml file and showed its data in table layout

Today while creating an Android Application I came to use XML Parsing and also required to use Table Layout to show its retrieved xml data . So i thought of making an article that shows easy to use method and code to parse the xml data into tabulated format

XML Parsing is very cool thing . We need to parse the xml in various situations . Like i am going to why i have used it . Xml parsing is used in situations when the data is going to be retrieved form web service . When we are using or quering the database or when we are dealing with Web Service like .asmx or WCF then the data returned by these webservices are in XML format so in order to view this data result to user we need to parse that xml according to our needs means which element or which node data we want to show and by using this code we can retrieve the required data and we can show it in view or layout of android to user .  


Steps To Parse XML File 

1. First Create New Instance of Document Builder 
2. Then Create a Document from instance of Document Builder Created in First Step
3. Get document elements
4. Get and parse data Node by Node

DOWNLOAD The Required Code Files For Xml Parsing


The Following Snapshot shows the process of parsing of XML data 

XML Data Parsing Process


package com.example.parserxml; 
import java.io.StringReader; i
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

 public class MainActivity extends ActionBarActivity 

 @Override protected void onCreate(Bundle savedInstanceState) 
{
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main);
 Button mybtn=(Button)findViewById(R.id.MyButton);
 String filename=" " + " Citibank " + " 100 " + " 1000 " + " "; 

try 
 { 
 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
 InputSource is=new InputSource(); 
 is.setCharacterStream(new StringReader(filename)); 

 Document doc; 
 doc=dBuilder.parse(is); 
 doc.getDocumentElement().normalize(); 
 NodeList nodes = doc.getElementsByTagName("STOCK"); 
 for (int i = 0; i < nodes.getLength(); i++) 

 Node node = nodes.item(i); 
 if (node.getNodeType() == Node.ELEMENT_NODE) 
{
//GETTING ALL ELEMENTS UNDER 'STOCK' IN ELEMENT AS ARRAY
 Element element = (Element) node; 

//GETTING VALUE OF SYMBOL UNDER STOCK ELEMENT THAT IS FILLED IN PREVIOUS STEP
 String val=getValue("symbol",element); 

//GETTING VALUE OF PRICE UNDER STOCK ELEMENT THAT IS FILLED IN PREVIOUS STEP
 String val1=getValue("price",element); 

//GETTING VALUE OF QUANTITY UNDER STOCK ELEMENT THAT IS FILLED IN PREVIOUS STEP
 String val2=getValue("quantity",element); 

//SETTING TEXTVALUE OF BUTTON
 mybtn.setText(val); 

//making instance of Table layout
 TableLayout tab=(TableLayout)findViewById(R.id.tab); 

//making instance of table row this refers to this current context
 TableRow tbr=new TableRow(this); 


//creating three textview for values and setting some properties to look cool
TextView Name=new TextView(this); 
TextView Name1=new TextView(this); 
TextView Name2=new TextView(this); 

Name.setPadding(9,9,9,9); 
Name1.setPadding(9,9,9,9); 
Name2.setPadding(9,9,9,9); 
Name.setText(val);         //setting text values 
Name1.setText(val1); 
Name2.setText(val2); 
tbr.addView(Name); 
tbr.addView(Name1); 
tbr.addView(Name2);
tab.addView(tbr); 
 } 
 } 
 } 
catch (Exception ex) 

 mybtn.setText("Exception occured"); 
 ex.printStackTrace();
 } 
 } 

 private static String getValue(String tag, Element element) 

 NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes(); 
 Node node = (Node) nodes.item(0); 
 return node.getNodeValue(); 
 } 

 @Override public boolean onCreateOptionsMenu(Menu menu) 

 // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); 
 return true; 
 } 

 @Override public boolean onOptionsItemSelected(MenuItem item) 
{
 // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. 
 int id = item.getItemId(); 
 if (id == R.id.action_settings) 

 return true;
 } 
 return super.onOptionsItemSelected(item); 
 } /** * A placeholder fragment containing a simple view. */ 

 public static class PlaceholderFragment extends Fragment 

 public PlaceholderFragment() { } 

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

 View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; 
 } 
 } 
 } 


EXPLANATION OF JAVA CODE

  • DocumentBuilder :- It is an API that parse the content of given xml document as input to document builder instance and after parsing it returns new DOM Document in which all the elements of xml are parsed according to the Document object model ( DOM )


DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 


  • In below this lines of code we are reading the XML Document using StringReader Class We are using InputSource class instance and reading the xml document and filling inputsource object with that data


 InputSource is=new InputSource(); 
 is.setCharacterStream(new StringReader(filename)); 



  • In below lines of code we are exploring XML Document code and getting the data by tagname and converted it into xml nodes by making instance of NodeList Class . Each Node of XML Document is given to node object of Nodelist and in this code we are giving it by tagname


NodeList nodes = doc.getElementsByTagName("STOCK"); 

  • Then now we are exploring the nodes by nodes.item( i ) so in these as we are exloring it by STOCK tagname so each item of nodes will contains the child node of STOCK Parent Node


 Node node = nodes.item(i); 


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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

   <TableLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/tab"
       >

   <TableRow
           android:id="@+id/tableRow1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >
       

   <TextView
               android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Column1"
             />


    <TextView
               android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Column2"
             />


     <TextView
               android:id="@+id/txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Column3"
             />
          
       </TableRow>
      
   </TableLayout>
 
   

</LinearLayout>

Thursday 24 July 2014

Install MonoAndroid Tools in Visual Studio 2012

Mono came as a Revolution in the IT Market for CSharp Application Development . Csharp was introduced by Microsoft and it was mainly used in Visual Studio IDE by Microsoft . As Csharp all came with .Net Framework and .Net Framework . So nothing works Cross-Platform neither in linux nor in OSx . So Then Came the Another remarkable solution to this problem That called "MONO"  . Mono introduced with several thing that can be used with .net framework .

One of these was Mono Android Tools . These Tools allows the Csharp Developers to develop Android Applications using Csharp Code . They can choose Any IDE Either it could be Microsoft Visual Studio or Monodevelop . This has Increased the popularity of .NET Technology in the Market and It asked the Developers to think more n more about .NET Technology As With Mono .Net Technology Can Be used in Any Platform Availailble It can be used in Microsoft Windows , Linux operating Systems like -Ubuntu and also it can be used in OSx . And it can also be used in Mobile Applications Development like for Developing Android Applications , Windows Phone Applications and IOS Applications .

Now We are going to install Mono Android in Visual Studio 2012 ( comment for any help below)

System Requirements 

Operating Systems
Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit)

Development tools
JDK 6 (JRE alone is not sufficient)

 Steps For Mono Android Installation

1. First Install Java Jdk ( Java development Kit )
2. Then Install Java Sdk ( software development Kit )
3. Install Microsoft Visual Studio ( If Visual Studio is previously installed then skip this step )
4. Install Mono Android Setup file To integrate it in Visual Studio
5. Place , Configure SDK and Create AVD ( emulator ) .
6. Configuring Visual Studio  For Android Use

Step 1 -- Install Java Jdk 

Java Development Kit contains set of tools that allows to run the java Development tools on the particular platform with jdk we cannot do java development and Android development on that particular machine .
In Order To install android Java jdk Go to link below and you can find different versions of java jdk availaible there I am installing jdk-6u31 from link below 

Download Android Jdk -- geeksprogrammings

Step 2 -- Install Java Sdk 

For Installation of Java SDK we must ensure that java Jdk is installed properly else Android SDK Will not install .

Java SDK provide the Set of tools that allows the software development using java To Download Java SDK you can follow the link on oracle website :-

Download Android SDK --geeksprogrammings
Go to link accept the license and Click on Download

After Download Android Sdk Zip file Now Extract that file and place the extracted folder in somewhere like :- c:\android-sdk

Step 3 -- Intall Microsoft Visual Studio  ( skip this step if you have already installed visual studio )


Download Visual Studio 2012 -- geeksprogrammings

After Downloading Visual studio you can a similar intallation procedure here :-

How To install Visual Studio


Step 4 :- Now We are going to install Mono Tools for Mono Android to integrate with Visual Studio 

Download the following file to mono android integration in visual studio 2012

Download mono-android-4.4.54.208556545

After Downloading it Install the msi file .After its Installation now you mono android is intall and your Visual Studio is ready for creating Mono Android or Android Applications

 


Step 5 :- Place , Configure SDK and Create AVD ( emulator ) .


Now Everything is installed now the game of configuration starts . First We remember that we have placed the android-sdk folder in C:\android-sdk



  1. So open Android Sdk folder you can see SDK Manager there Double click on it


  2. It will open Android SDK Manager it will show all Android Version with their respective API versions and you can see none of it will be installed by default you have to install it
  3. So I have installed Android 4.4 ( API 19 ) or you can also install Android 4.0 ( API 14 ) To install it just click on Check box at begin of it and then click on Install packages verify all other checkbox except these are unchecked .

  4. It may take more time according to your internet connection 
  5. After installation completes Click on Tools menu at top bar of SDK Manager

  6. Click on Manage AVD
  7. Then create an AVD like i have shown below :-

Then after creating AVD  select that AVD and click start button Then you can see AVD emulator that will appear on screen .


STEP 6 -- Configure Visual Studio For Android Application Development 


In Visual Studio We just need to configure one thing that is we have to configure proper path for android SDK Here is simple steps to configure or write proper sdk path .

Steps To configure Android SDK location :-

1. Open Visual Studio 2012
2. Click on Tools Menu 
3. Then click on Options
4. Then Options window appear if your Android SDK Location is not filled or not correct click on change button
5. Then click on browse button and choose android sdk path like :- C:\android-sdk and click OK If your path will be correct then you will see image as below in image my android sdk folder path is (c:\Program Files\Android\android-sdk ) and  it can be according to your sdk path . like C:\android-sdk

6. Then click ok to close options window
7. Now click on New Project
8. Then click on Mono For Android Then you will see following Screen This means Everything is good and Installed Correctly



In Next Article I will give introduction to App Development using CSharp in mono Android 






Sunday 20 July 2014

Install Monodevelop on Linux

MonoDevelop is a revolution in the era of Cross development by using Microsoft .NET programming language like Csharp ( C# ) , Visual Basic and Jsharp ( J# ) . Monodevelop has removed blame that Microsoft .NET software can only run on Windows platforms This makes possible to run .net applications and to build a .Net Application on any platform

MonoDevelop is a cross-platform IDE  developed for Csharp ( C# ) programming language  and other .NET programming languages. MonoDevelop allows programmers to program desktop softwares and ASP.NET Web applications and websites  on Linux, Windows  and Mac OSX. MonoDevelop  makes possible for .net programmers to run , create and deploy their .net software applications on cross-platforms Any  .NET application developed  with Visual Studio can be used in linux or Mac with the help of MonoDevelop

Windows platform is fully supported  for running MonoDevelop and it is fully accepted. In past there were many issues regarding Mono Development but now  Many Windows specific issues have been fixed, and ome add-ins such as debugging and subversion support have been written specifically for Windows and it is fully supported by windows

By developing with mono specifically, you will be able to run your executable on any platform that has mono available for it. That in and of itself is mono's biggest advantage over developing on MSFT's .Net platform. Said differently: If you build you assembly with mono, you're guarantee cross-platform support

Features Of MonoDevelop


  1. Multi-platform support

    It Supports Linux ( ubuntu etc ) , Windows( xp,7,8 etc ) and Mac OS X operating systems
  2. Advanced Text Editing Facilities

    It provides Code completion support for Csharp  4 that is lastest version for csharp
    It also provides  code templates and  code folding.
  3. Configurable workbench

    Fully customizable window layouts, user defined key bindings, external tools
  4. Multi-language programming Facilities

    Monodevelop supports various programming languages like - C#, Visual Basic.Net, C/C++, Vala

GTK# Visual Designer

Easily build GTK# applications

Installation Steps for MonoDevelop in Ubuntu 

 

You can also see installation of monodevelop on Windows Here :-
So, when you are going to start installation of monodevelop on ubuntu , i am trying it on ubuntu version 12.04 one thing you want to remember is you can install monodevelop on ubuntu with various methods it includes various packages and you should never install them one by one you should also go for a complete package for installation of monodevelop on ubuntu .
1. Press Ctrl + Alt + T and it will Opens Terminal Screen 

2. Type in the following command to install monodevelop :-

sudo apt-get install monodevelop
3. Then Hit Enter

4. If you got the following error :-

Error :- Unable to locate the package monodevelop

This is common error that number of users that are installing monodevelop on ubuntu face . So don't fear from this error this is simple error as it is unable to locate the monodevelop so it means there is need to update the software center or software repository of ubuntu to locate the monodevelop product

5. So simple solution of this error is open Terminal and fire the following command :-

sudo apt get update



and then Hit enter

6. This command requires internet connection and it may take some time to get completed and if this command completes successfully then now monodevelop would be located

7. Now run the following command to install monodevelop

sudo apt-get install monodevelop

8. First it will  prompt you for installation procedure of monodevelop Press y to conform 'Yes'

9. Then It will automatically install and files , packages required for mono
Now you will se a monodevelop icon on your ubuntu sidebar like below -- the blue monodevelop icon



10. click on blue monodevelop icon and you will be presented with monodevelop IDE





Sunday 13 July 2014

Getting Started and Deploy Asp.net App with Appharbor -- Cloud Platform as a service

CLOUD - So first thing we need to know here is what is a Cloud ? and how it is useful to us to use Cloud. Cloud is long network of interconnected computers that allow the avaliability of data over internet all the time .Cloud help to make our data secure and allow the user to run its application on cloud with full scalability and availaibility . We say it provides avalaibility of 100% because Cloud follows the approach of Replication of data to the other nodes to make ensuring the full availaibility of data even when the server that is actually assumed to have data or application is not availaible .

If we are accessing an Application on Cloud and at that particular time that server is down so how our Application Deployed on Server could be accessed Cloud Computing gives answer of this question By Cloud Computing when we deploy our application on cloud then cloud computing will replicate your application to other connected nodes also so that your application will be availabile when one of node is off and unable to provide the access to your application

AppHarbor is a fully hosted .NET Platform as a Service. AppHarbor can deploy and scale any standard .NET application to the cloud. AppHarbor is easy and simple to use . It provides number of additional addons for database and other applications to add it to your application. Here I am going to tell all the procedure to create and deploy asp.net application to appharbor cloud service.

Step1

Start your Browser and Put this Url in your address bar and Navigate to AppHarbor Website :- https://appharbor.com/ and Hit Enter

Step2
Click on  Get started Button . Then do simple signup procedure .Then Login to your AppHarbor Account.

Step3
After login Click on Your Applications Menu item on Top Menu Bar and Create a New application by supplying a name for the application Then Finally click on Create New Button


Step4
Now New application is successfully created on cloud . Now for deploying a .Net application with Sql Server database We require to add Sql server as a Addon to Our cloud App

Step5
Now click on Add Ons on sidebar Menu Then Scroll down and find Sql Server and Click on it.


Step6
Now SQL  SERVER Add details are shown Click on Free Install button . Then SQL SERVER add on will be installed for your application to use sql server database with .net application in cloud


Step7
Now on SQL SERVER Installation Success screen click on  sql server



Step8
Then click on Go To  Sql Server . Now your sql server Instance Details will be shown . The most important thing we need is connection string of database instance

Step9
Copy this connection string we will require it in Further steps

Step10
Now open your Visual Studio Command Prompt . It will be availaible in Microsoft visual studio 2010/2012 folder , if not found you can comment here on this post

Step11
Write the following command on this :-
 aspnet_regsql -A all -C "place your copied connection string here"     Now hit enter

 

Step12
Now Open Visual studio Click on File Click on New Project Choose ASP.NET WEB APPLICATION give it a Name and give proper save path like I give "C:/WindowsApplication3" and Hit Ok


Step13
Now Visual Studio will Present you with practice project already having some coded files .

Step14
Now open Web.Release.config file Uncomment the connectionstring tag and Replace the name of connection add tag to  "ApplicationServices" and Paste the connection we have copied earlier here and finally Press Ctrl + s to save all content . Now all work with visual studio is done.  Just  Run you application on localhost to know its working fine or not and it also compile all the code that is must required to deploy app on appharbor


 Step15
Now Open Git Bash If you have not installed it yet then wait for next article in which i will tell how to install Git Bash

Step16
After opening Git Bash Navigate to directory where you have saved your .net project like I have saved it in "C:/WebApplication3" . Use this command to go to this directory

cd c:/WebApplication3             Then Hit enter



Step17
Now Run following command to initialize Git Repository
 git init



Step18
Run following command to add repository to Git
git add .



Step19
Run following command to commit your data to Git repository
git commit -m "Added Project To Git:"



Step20
Sometime It may ask you for your Email Address pass it using following command
git config --global user.email "yourmail@example.com"

Step21
Now Clone your git Repository for that we require Repository Url for that Go to Appharbor.com then click on your applications Then Click on Application you have created Then in Sidebar click on   Repository Url then it will be automatically copied to your clipboard

 Step22
Now run the following command  :-
git remote add appharbor "paste copied url here"    ...........then hit enter


Step23
Now push your data or application to cloud or Git Repository by using following command
git push appharbor master

Step24
Now it will ask for password then enter password of your appharbor account and Hit Enter Now it will copy all files to Git Repository



Step25
Now your Application is successfully deployed to cloud To check it online you can first refresh your webpage where you have opened Appharbor account Then go to application we have created on appharbor and click on Hostnames and you can see a url there click on it or open it in new  tab it will show your website that you have uploaded

Tuesday 3 June 2014

create you first app in Monodevelop csharp


  1. After Successfull Installation of MonoDevelop We will start with making our first Hello World App in MonoDevelop . If you have not Installed MonoDevelop you can get Installation Guide here

    Installation Guide For MonoDevelop
  2. Start you M onoDevelop that you have installed already. It opens with following screen
      Gtk# version 2.12.9 or greater must be installed

  3. Now to create a simple app in MonoDevelop Click on File Menu --> Then click on Solution
    Gtk# version 2.12.9 or greater must be installed

  4. Then a window appears Click on Gtk# project and give Project a Name and click oK
    Gtk# version 2.12.9 or greater must be installed

  5. Now your project open with a code window with Main.cs file as below
    Gtk# version 2.12.9 or greater must be installed

  6. Now right click on your proect in sidebar then click on Add and in Submenu click on New File
    Gtk# version 2.12.9 or greater must be installed

  7. Then click on Window give it a name and click ok
    Gtk# version 2.12.9 or greater must be installed

  8. You can see you have two views Designer and source view .Designer view will be used for Designing GUI for your application and source will be used for coding
    Gtk# version 2.12.9 or greater must be installed

  9. Now open new window file you have added and goto design view
  10. now from Toolbar in right sidebar Go to containers section and double click on Fixed Container to get it on your window
    Gtk# version 2.12.9 or greater must be installed

  11. Now take a button from Toolbar
    Gtk# version 2.12.9 or greater must be installed

  12. select the button Now click on properties window and click on Signals Tab
    Gtk# version 2.12.9 or greater must be installed

  13. Now click on Clicked property and give a valid name for click event function and Hit Enter
    Gtk# version 2.12.9 or greater must be installed

  14. Now first add reference to system.windows.forms Namespace for that Right click on project in sidebar and click on Edit Reference
    Gtk# version 2.12.9 or greater must be installed

  15. Search for system.windows.forms in search box and click check mark on check Box and click ok
    Gtk# version 2.12.9 or greater must be installed

  16. Now come to Souce view and you will see code like below

    protected void clickevent (object sender, EventArgs e)
    {
    throw new System.NotImplementedException ();
    }
  17. Remove " throw new System.NotImplementedException ();" line and Add line as below

    MessageBox.Show("Hello Welcome To Mono World");
  18. Now Save All your project change by pressing Ctrl + s
  19. Now Press F5 To Run your project and It will show output below
    Gtk# version 2.12.9 or greater must be installed