My Blog List

Wednesday, 23 November 2011

How to Handle Menu in Android ?


Hello friends,

Today's post is about Menu in android, throughout this post you will be able to create menu .

let's start with some basic theory about menu. Android supports two types of menu system.
1.Option Menu
2.Context Menu
Option menu is the menu when user press hardware menu button of his device. Whereas Context menu appears when user long press on a View(it may be Button, EditText etc) .


So For creating option menu first thing you have to do is to create one folder named menu under res folder. Create menu.xml under menu.
res/menu/menu.xml
In menu.xml add your menu item(in this demo Home ,End)
Now  in the code you have to override   onCreateOptionsMenu(Menu menu)
and   onOptionsItemSelected()

For creating Context Menu  no need to create  menu.xml file
Just override onCreateContextMenu(Menu menu) and onContextItemSelected()


Here is the steps for Creating Option Menu
1. Create  new project with OptionMenuDemo Activity
2.Create menu/menu.xml under res folder


3.Create your item under optionmenu.xml.



3. Add this code in your OptionMenuDemo.java file.


package com.bp.menudemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class OptionMenuDemo 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 m=getMenuInflater();
      m.inflate(R.menu.optionmenu, menu);
      return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      if(item.getTitle().equals("Home"))
      {
            Toast.makeText(getApplicationContext(), "home is clicked", Toast.LENGTH_LONG).show();
      }
      else
      {
            Toast.makeText(getApplicationContext(), "End is clicked", Toast.LENGTH_LONG).show();
      }
           
     
      return true;
    }
}

4. Option Menu output in emulator
 
 

Now some steps for creating Context Menu
1.Create another project having ContextMenuDemo Activity
2.In ContextMenuDemo.java file add this code.


 package com.bp.contextmenudemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class ContextMenuDemo extends Activity {
      Button b;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button) findViewById(R.id.contextmenu);
        registerForContextMenu(b);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
     
      super.onCreateContextMenu(menu, v, menuInfo);
      menu.setHeaderTitle("Choose");
      menu.setHeaderIcon(R.drawable.icon);
      menu.add(0, v.getId(), 0, "Home");
      menu.add(0, v.getId(), 0, "End");
     
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
      if(item.getTitle().equals("Home"))
      {
            Toast.makeText(getApplicationContext(), "Home menu is clicked", Toast.LENGTH_LONG).show();
      }
      else
      {
            Toast.makeText(getApplicationContext(), "End menu is clicked", Toast.LENGTH_LONG).show();
      }
      return true;
    }
}

3. Context Menu  output in emulator.



That's it for this post.
for any query mail me : bpsingh216@gmail.com


Tuesday, 1 November 2011

Now DeveloperSite on your android mobile

Hello guyz.
hope you are ok.Today i am goona post a Android app for this site.
you can download   .apk file  from
http://www.appsgeyser.com/getwidget/AndroidDeveloperSite

do'nt worry its free!!!
download it and install it on your android device
now developer Site will be one touch access.
happy coding :-)

Wednesday, 12 October 2011

Android Text to Speech Project code

Hello Friend i was busy with my few Android Project .
Here is all about  Android Text to Speech
One of the many features that Android provides out of the box is the one of “speech synthesis”. This is also known as “Text-To-Speech” (TTS) and is mainly the capability of the device to “speak” text of different languages. This feature was introduced in version 1.6 of the Android platform and you can find an introductory article at the official Android-Developers blog.

In this tutorial I am going to show you how to quickly introduce TTS capabilities into your application.

Let's get started by creating a new Eclipse project under the name “AndroidTextToSpeechProject” as shown in the following image:



Note that Android 1.6 was used as the build target and that for the minimum SDK version I used the value 4, since this functionality is not provided by the previous versions.

The first step to use the TTS API is to check if it is actually supported by the device. For that purpose there is a special action named ACTION_CHECK_TTS_DATA which is included in the TextToSpeech.Engine. As the Javadoc states, this is used by an Intent to “verify the proper installation and availability of the resource files on the system”. In case the resourses are not available, another action named ACTION_INSTALL_TTS_DATA is used in order to trigger their installation. Note that the SDK's emulator supports TTS with no configuration needed.

Before using the TTS engine, we have to be sure that it has properly been initialized. In order to get informed on whether this has happened, we can implement an interface called OnInitListener. The method onInit will be invoked when the engine initialization has completed with the accompanying status.

After initialization, we can use the TextToSpeech class to make the device speak. The relevant method is named speak, where the text, the queue mode and some additional parameters can be passed. It is important to describe queue mode. It is the queuing strategy to be used by the TTS engine, i.e. what to do when a new text has been queued to the engine. There are two options:

  • QUEUE_ADD: the new entry is added at the end of the playback queue
  • QUEUE_FLUSH: all entries in the playback queue (media to be played and text to be synthesized) are dropped and replaced by the new entry
All the above are translated to the code provided below:
01package com.javacodegeeks.android.tts;
02 
03import android.app.Activity;
04import android.content.Intent;
05import android.os.Bundle;
06import android.speech.tts.TextToSpeech;
07import android.speech.tts.TextToSpeech.OnInitListener;
08import android.view.View;
09import android.view.View.OnClickListener;
10import android.widget.Button;
11import android.widget.EditText;
12import android.widget.Toast;
13 
14public class TtsActivity extends Activity implements OnInitListener {
15     
16    private int MY_DATA_CHECK_CODE = 0;
17     
18    private TextToSpeech tts;
19     
20    private EditText inputText;
21    private Button speakButton;
22     
23 @Override
24 public void onCreate(Bundle savedInstanceState) {
25     
26  super.onCreate(savedInstanceState);
27  setContentView(R.layout.main);
28   
29  inputText = (EditText) findViewById(R.id.input_text);
30  speakButton = (Button) findViewById(R.id.speak_button);
31   
32  speakButton.setOnClickListener(new OnClickListener() {           
33   @Override
34   public void onClick(View v) {
35       String text = inputText.getText().toString();
36       if (text!=null && text.length()>0) {
37    Toast.makeText(TtsActivity.this, "Saying: " + text, Toast.LENGTH_LONG).show();
38    tts.speak(text, TextToSpeech.QUEUE_ADD, null);
39       }
40   }
41      });
42   
43  Intent checkIntent = new Intent();
44      checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
45      startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
46         
47    }
48protected void onActivityResult(int requestCode, int resultCode, Intent data) {
49        if (requestCode == MY_DATA_CHECK_CODE) {
50            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
51                // success, create the TTS instance
52                tts = new TextToSpeech(this, this);
53            }
54            else {
55                // missing data, install it
56                Intent installIntent = new Intent();
57                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
58                startActivity(installIntent);
59            }
60        }
61 
62    }
63 
64    @Override
65    public void onInit(int status) {       
66        if (status == TextToSpeech.SUCCESS) {
67            Toast.makeText(TtsActivity.this,
68                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
69        }
70        else if (status == TextToSpeech.ERROR) {
71            Toast.makeText(TtsActivity.this,
72                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
73        }
74    }
75     
76}

The engine support is checked in the onActivityResult method. If there is support for TTS, we initialize the engine, else a new Intent is launched in order to trigger the relevant installation. After initialization, we used a Toast in order to notify the user about the operation status. Finally, we use a text field where the text is provided by the user and a button which feeds the engine with the provided text.

In order to run the project, an appropriate emulator device is needed. If you have none available, use the following configuration in the AVD manager (note that “Android 1.6 – API Level 4” or higher is needed):



Provide the text you wish to hear into the text field, hit the button and listen to emulator speak to you!



That's all folks, nice and simple. Now, make your devices speak!


enjoy coding :-)