Monday, January 23, 2012

Display an Alert Dialog

[sourcecode language="java"]
package com.my.option;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

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

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(OptionMenuActivity.this);
builder.setMessage("Do you really need to exit?");
builder.setCancelable(false);

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
OptionMenuActivity.this.finish();

}
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();

}

});

AlertDialog alert = builder.create();
alert.show();
}
});
}

}

[/sourcecode]

How to add a context menu in Android


  1. Create an Android XML file of Menu type

  2. Add menu options as you preferred. Each menu option should be indicated as <item> XML element.

  3. This is my context_menu.xml file.[sourcecode language="xml"]
    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/item1"   android:title="option1"></item>
    <item android:id="@+id/item2"   android:title="option2"></item>
    </menu>
    [/sourcecode]

  4. Override onCreateContextMenu and onContextItemSelected in your Activity.java file
    Add a button in main.xml to respond our context menu.
    [sourcecode language="java"]
    package com.my.option;

    import java.util.zip.Inflater;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.ContextMenu;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.widget.Button;

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

    Button b = (Button) findViewById(R.id.button1);
    registerForContextMenu(b);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {

    MenuInflater inflator = getMenuInflater();
    inflator.inflate(R.menu.context_menu, menu);

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.item1)
    Log.d("Edit", "Selected : Edit Option");
    if(item.getItemId() == R.id.item2)
    Log.d("Delete", "Selected : Delete Option");
    return super.onContextItemSelected(item);
    }
    }
    [/sourcecode]

Simple Options Menu in Android


  1. Create an Android XML file of Menu type

  2. Add menu options as you preferred. Each menu option should be indicated as <item> XML element.

  3. This is my options.xml file.[sourcecode language="xml"]
    <!--?xml version=<em--><?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/item1" android:title="Edit" android:icon="@android:drawable/ic_menu_edit"></item>
    <item android:id="@+id/item2" android:title="Delete" android:icon="@android:drawable/ic_menu_delete"></item>
    <item android:id="@+id/item3" android:title="Help" android:icon="@android:drawable/ic_menu_help"></item>
    <item android:id="@+id/item4" android:title="More" android:icon="@android:drawable/ic_menu_more"></item>


    </menu>
    [/sourcecode]

  4. My options menu is not just text-based. Each menu item is displayed with a relevant icon. Here I used the icons found in the Android.jar file of my project. You can find such icons using this path
    <app root > --> Android <Your Android platform>  --> android.jar --> res --> drawable-hdpi(icons for menu prefixed with ic_menu)

  5. Override onCreateOptionsMenu and onOptionsItemSelected in your Activity.java file
    [sourcecode language="java"]
    package com.my.option;

    import java.io.Console;
    import java.util.zip.Inflater;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflator = getMenuInflater();
    inflator.inflate(R.menu.options, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.item1)
    Log.d("Edit", "Selected : Edit Option");
    if(item.getItemId() == R.id.item2)
    Log.d("Delete", "Selected : Delete Option");
    return super.onOptionsItemSelected(item);
    }
    }
    [/sourcecode]

Saturday, January 7, 2012

Write Data in XML File Using C#

Sample XML File

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>

<DoctorInfo>

&nbsp;

<Doctor>

<id>01</id>

<name>Ranil Jayaweera</name>

<speciality>GI</speciality>

</Doctor>

&nbsp;

<Doctor>

<id>02</id>

<name>Aruna Gunathilaka</name>

<speciality>VP</speciality>

</Doctor>

&nbsp;

<Doctor>

<id>03</id>

<name>Jaya Palipana</name>

<speciality>VOG</speciality>

</Doctor>

&nbsp;

</DoctorInfo>
[/sourcecode]

The Code

[sourcecode language="csharp"]
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(Application.StartupPath + "\\DoctorsList.xml");

XmlElement subroot = xmlDoc.CreateElement("Doctor");

&nbsp;

XmlElement childElementId = xmlDoc.CreateElement("id");

XmlText xmlTextId = xmlDoc.CreateTextNode(txtid.Text);

childElementId.AppendChild(xmlTextId);

subroot.AppendChild(childElementId);

xmlDoc.DocumentElement.AppendChild(subroot);

&nbsp;

XmlElement childElementName = xmlDoc.CreateElement("name");

XmlText xmlTextName = xmlDoc.CreateTextNode(txtName.Text);

childElementName.AppendChild(xmlTextName);

subroot.AppendChild(childElementName);

xmlDoc.DocumentElement.AppendChild(subroot);

&nbsp;

XmlElement childElementSpeciality = xmlDoc.CreateElement("speciality");

XmlText xmlTextSpeciality = xmlDoc.CreateTextNode(cbspecial.Text);

childElementSpeciality.AppendChild(xmlTextSpeciality);

subroot.AppendChild(childElementSpeciality);

xmlDoc.DocumentElement.AppendChild(subroot);

&nbsp;

xmlDoc.Save(Application.StartupPath + "\\DoctorsList.xml");
[/sourcecode]

Read Data from XML File Using C#

Sample XML File

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>

<PatientInfo>

<Patient id=”1”>

<app-no>03</app-no>

<fname>Brian</fname>

<lname>Marsh</lname>

<docId>03</docId>

</Patient>

<Patient id=”2”>>

<app-no>02</app-no>

<fname>Bron</fname>

<lname>Marini</lname>

<docId>01</docId>

</Patient>

</PatientInfo>

[/sourcecode]

The Code

[sourcecode language="csharp"]
XmlDocument doc = new XmlDocument();

doc.Load("files/sampleXML.xml");

&nbsp;

XmlNodeList patientList = doc.GetElementsByTagName("Patient ");

&nbsp;

foreach (XmlNode node in patientList)

{

XmlElement patientElement = (XmlElement) node;

&nbsp;

string first_name = patientElement.GetElementsByTagName("fname")[0].InnerText;

string last_name = patientElement.GetElementsByTagName("lname")[0].InnerText;

string patient_id = "";

&nbsp;

if (patientElement.HasAttributes)

{

patient_id  = patientElement.Attributes["id"].InnerText;

}

&nbsp;

Console.WriteLine("{0} ({1})  {2}\n", first_name, last_name, patient_id);

}

[/sourcecode]

Delete Data from XML File Using C#

Sample XML File

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>

<DoctorInfo>

<Doctor>

<id>01</id>

<name>Ranil Jayaweera</name>

<speciality>GI</speciality>

</Doctor>

<Doctor>

<id>02</id>

<name>Aruna Gunathilaka</name>

<speciality>VP</speciality>

</Doctor>

<Doctor>

<id>03</id>

<name>Jaya Palipana</name>

<speciality>VOG</speciality>

</Doctor>

</DoctorInfo>
[/sourcecode]


The Code

[sourcecode language="csharp"]
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(Application.StartupPath + "\\DoctorsList.xml");

XmlNode rootNode = xmlDoc.SelectSingleNode("//DoctorInfo");

XmlNodeList doctorList = rootNode.SelectNodes("Doctor");

for (int i = 0; i < doctorList.Count; i++)

{

if (doctorList[i].SelectSingleNode("id").InnerText.Equals(“03”))

{

rootNode.RemoveChild(doctorList[i]);

xmlDoc.Save(Application.StartupPath + "\\DoctorsList.xml");

}

}
[/sourcecode]

Adding audio with HTML

[sourcecode language="html"]
<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="200" height="16">

<param name="src" value="fileName.mp3" />
<param name="controller" value="true" />
<param name="autoplay" value="false" />
<param name="autostart" value="0" />
<param name="pluginspage" value="http://www.apple.com/quicktime/download/" />

<!--[if !IE]> <-->

<object type="audio/x-mpeg" data="fileName.mp3" width="200" height="16">
<param name="src" value="fileName.mp3" />
<param name="controller" value="true" />
<param name="autoplay" value="false" />
<param name="autostart" value="0" />
<param name="pluginurl" value="http://www.apple.com/quicktime/download/" />

</object>

<!--> <![endif]-->

</object>

[/sourcecode]

IE and non-IE browsers treat audio objects in a web page two different ways. Therefore <Object> should be used carefully in order to play the audio in all browsers perfectly. In the code, there are two sections for IE and other browsers like FF.

IE looks for class Id before playing audio.  But other browsers do not use this attribute. They rely on the MIME type of the audio object. In the above code commented section is used by non IE  browsers to play audio.

Some Other sound formats

  • au (type="audio/basic")

  • wav (type="audio/wav" or "audio/x-wav")

  • ra (type="audio/x-pn-realaudio")

  • mp3 (type="audio/x-mpeg")

  • wma (type="audio/x-ms-wma")


Alternatives for class Id

  •  Quicktime player: classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"

  • Windows media Player: classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/ nsmp2inf.cab#Version=6,0,02,902"

  • Real Audio: id=RVOCX classid="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA"

  • Flash Shockwave Player : classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
    codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/ swflash.cab#version=8,0,22,0"

Monday, December 5, 2011

Merge Sort

The merge sort algorithm closely follows the divide-and-conquer paradigm. Intuitively, it operates as follows.
Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each.
Conquer: Sort the two subsequences recursively using merge sort.
Combine: Merge the two sorted subsequences to produce the sorted answer.

Merge sort procedure
Input : A an array in the range 1 to n.
Output : Sorted array A.
Procedure MERGESORT (A,l,r)
1. if l < r
2. then q ← (l+r )/2
3. MERGESORT (A,l,q)
4. MERGESORT (A,q+1, r)
5. MERGE (A,l,q,r)

Merge procedure (Algorithm 1)
Procedure MERGE (A,l,q,r)
1. i ← l
2. j ← q+1
3. k ← 0
4. while (i ≤ q) and ( j ≤ r) do
5. k ← k+1
6. if A[i] ≤ A[j] then
7. TEMP [k] ← A [i]
8. i ← i +1
9. else
10. TEMP [k] ← A [j]
11. j ← j +1
12. if j > r then
13. for t ←0 to q – i do
14. A [r-t] ← A [q-t]
15. for t ← 0 to k-1 do
16. A [l +t] ← TEMP [t+1]

Merge procedure (Algorithm 2)
MERGE(A, p, q, r)
1 n1 ← q - p + 1
2 n2 ← r - q
3 create arrays L[1.. n1 + 1] and R[1.. n2 + 1]
4 for i ← 1 to n1
5 do L[i] ← A[p + i - 1]
6 for j ← 1 to n2
7 do R[j] ← A[q + j]
8 L[n1 + 1] ← ∞
9 R[n2 + 1] ← ∞
10 i ← 1
11 j ← 1
12 for k ← p to r
13 do if L[i] ≤ R[j]
14 then A[k] ← L[i]
15 i ← i + 1
16 else A[k] ← R[j]
17 j ← j + 1

Analysis of Merge Sort
To find the middle of the sub array will take D(n)=(1).
To recursively solve each sub problem will take 2T(n/2).
To combine sub arrays will take (n).

Therefore T(n)=T(n/2)+ (n) + (1)
We can ignore (1) term.

The operation of merge sort on the array A = 〈5, 2, 4, 7, 1, 3, 2, 6〉.

QuickSort Algorithm

Left
Middle(only one element)
Right


Procedure QUICKSORT (A,p,r)
1 if p < r
2 then q ← PARTITION(A,p,r)
3 QUICKSORT (A,p,q-1)
4 QUICKSORT (A,q+1,r)


Partition Algorithm 01

Procedure PARTITION(A,p,r)
1 x ← A[p]
2 i ← p -1
3 j ← r +1
4 while TRUE
5 do repeat j ← j - 1
6 until A[j] ≤ x
7 repeat i ← i + 1
8 until A[i] ≥ x
9 if i < j
10 then exchange A[i] ↔A[j]
11 else return j

Partition Algorithm 02
PARTITION(A, p, r) 

1 x ← A[r]
2 i ← p - 1
3 for j ← p to r - 1
4 do if A[j] ≤ x
5 then i ← i + 1
6 exchange A[i] ↔ A[j]
7 exchange A[i + 1] ↔ A[r]
8 return i + 1

Analysis of Quick sort
The running time of quick sort depends on the partitioning of the sub arrays:
(a) Worst case partitioning(Unbalanced)



 
nOccurs when the sub arrays are completely unbalanced.

nHave 0 elements in one sub array and n − 1 elements in the other sub array.

nTime taken for partitioning is  Q(n) and T(0)= Q(1).

 
nTherefore Recurrence Equation is

T(n) = T(n-1) +T(0)+ Q(n)

Divide and Conquer Method

Divide: Break the problem into subproblems recursively.

Conquer: Solve each sub problems.

Combine: All the solutions of sub problems are combined to get the solution of the original problem.

Advantages

  • Increase the speed.


By running sub problems in parallel.

  • Increase the Cache Performance.


Applications

More work on divide phase.
Less work for others.

Less work on divide phase.
More work for others.

Wednesday, November 30, 2011

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...