RSS

Monthly Archives: January 2012

Display an Alert Dialog

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();
 }
 });
 }

}

 
Leave a comment

Posted by on January 23, 2012 in Android

 

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.
    <?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>
    
  4. Override onCreateContextMenu and onContextItemSelected in your Activity.java file
    Add a button in main.xml to respond our context menu.

    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);
    }
    }
    
 
Leave a comment

Posted by on January 23, 2012 in Android

 

Tags:

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.
    <!--?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>
    
  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
    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);
    }
    }
    
 
Leave a comment

Posted by on January 23, 2012 in Android

 

Tags:

Write Data in XML File Using C#

Sample XML File

<?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>

The Code

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");
 
Leave a comment

Posted by on January 7, 2012 in C#.Net, XML