Amazon Gift Cards

Saturday, September 19, 2009

Using the official TextToSpeech in Android 1.6 tutorial

Update There is a official post from android team on this on http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html

Android 1.6 SDK had been released and previously if you want TTS on your app, you have to use the one from eyes-free, while this one was the whole inspiration of having TTS by default on android, an official API is official. lol

If you have used the one from eyes-free, its mostly the same way except you'll be using android.speech.tts.TextToSpeech

Main
public class Main extends Activity implements TextToSpeech.OnInitListener {
  TextToSpeech tts;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tts = new TextToSpeech(this,this);
  }

  public void onInit(int status) {
    Locale loc = new Locale("es", "","");
    if(tts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE){
      tts.setLanguage(loc);
    }
    tts.speak("hola mundo", TextToSpeech.QUEUE_FLUSH, null);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    tts.shutdown();
  }
}


Explanation
We implement the OnInitListener for us know that the TextToSpeech is ready
public class Main extends Activity implements TextToSpeech.OnInitListener

Create a TextToSpeech instance where the first param is the context and second is the init function (the one we implemented)
tts = new TextToSpeech(this,this);

Function that we implemented, this function will be called after the TextToSpeech is ready to be used
public void onInit(int status) {

Determine if the language is available for us to use ( > TextToSpeech.LANG_AVAILABLE coz higher value means the language is available, use LANG_COUNTRY_AVAILABLE if you want to use UK english or Portuguese spanish)
if(tts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE){

Set new language if the language data is present
tts.setLanguage(loc);

Speak the words, and TextToSpeech.QUEUE_FLUSH means speak it right away.
tts.speak("hola mundo", TextToSpeech.QUEUE_FLUSH, null);

Hope this helps

References
TextToSpeech
TextToSpeech GIT snapshot
Eyes free

Update History
   Jan 17, 2012 - Visual Update

Friday, September 4, 2009

Extending and Customizing GridView with Custom Listener in Android

After customizing ListView now we will see how to customize and extend your GridView then we will add Custom Listener to it as well.

First we need to create our custom GridView, we used the package com.almondmendoza.myGridView
public class MyCustomGridView extends GridView implements OnItemClickListener{
  private Listener mListener;

  public MyCustomGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOnItemClickListener(this);
  }

  public void onItemClick(AdapterView parent, View v, int position, long id) {
    if (mListener != null) {
      mListener.onClick(position);
    }
  }

  public void setListener(Listener l){
    mListener = l;
  }

  public interface Listener{
    void onClick(int position); 
  }   
}



Explanation
Let us extend GridView and implement OnItemClickListener so that we could have a click even on each item
public class MyCustomGridView extends GridView implements OnItemClickListener

Use this constructor so we could use this class on our xml
public MyCustomGridView(Context context, AttributeSet attrs) { ... }

Create an Listener interface for us to use in other classes
public interface Listener{ void onClick(int position); }

Function which will be called when an item is clicked (implementation of OnItemClickListener)
public void onItemClick(AdapterView parent, View v, int position, long id) {

Call our onClick function from the instance that uses our Listener interface
if (mListener != null) { mListener.onClick(position); }







Main Class
public class Main extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MyCustomGridView mGrid = (MyCustomGridView) findViewById(R.id.my_custom_grid_view);
    mGrid.setListener(new ItemListener());
    mGrid.setAdapter(new ImageAdapter(this)); 
  }

  private class ItemListener implements MyCustomGridView.Listener{
    public void onClick(int position) {
      Log.d("almond","test");
    }
  }

  public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public ImageAdapter(Context c) {
      mContext = c;
    }

    public int getCount() {
      return mThumbIds.length;
    }

    public Object getItem(int position) {
      return null;
    }

    public long getItemId(int position) {
      return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
      } else {
        imageView = (ImageView) convertView;
      }
      imageView.setImageResource(mThumbIds[position]);
      return imageView;
    }

    private Integer[] mThumbIds = {
      R.drawable.icon,R.drawable.icon,R.drawable.icon
    };
  }
}



Explanation
We had used xml with our MyCustomGridView and here we get that view
MyCustomGridView mGrid = (MyCustomGridView) findViewById(R.id.my_custom_grid_view);

Create a class that uses the Listener interface we used in MyCustomGridView
private class ItemListener implements MyCustomGridView.Listener{ ... }

Set an instance of the ItemListener class
mGrid.setListener(new ItemListener());

As GridView is an extension of a AbsListView, we need a ListAdapter and we extend BaseAdapter (Please read this for the explanation of the codes inside - Hello GridView)
public class ImageAdapter extends BaseAdapter { ... }

We then set an instance of ImageAdaptor to be our adapter to our GridView
mGrid.setAdapter(new ImageAdapter(this));






Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
  <com.almondmendoza.myGridView.MyCustomGridView
    android:id="@+id/my_custom_grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:listSelector="@android:color/transparent"
    android:numColumns="auto_fit"
    android:columnWidth="160px"
  />
</LinearLayout>



Explanation
Use our class with our XML
<com.almondmendoza.myGridView.MyCustomGridView ... >


References
Launcher app
Browser App
Hello GridView

Update History
   Jan 17, 2012 - Visual Update

Tuesday, September 1, 2009

Speak Up!!!

The pass few weeks I had been in silent for the reason that I was preparing to submit an application for ADC2 (besides the time i had for my sister's wedding). And now I'm breaking the silence and getting myself to Speak Up!!!

Speak Up!!! is an android application designed to test your English speaking skills, it has 3 levels (easy, medium and hard) to choose from, and over 100+ words to randomized. On each stage you will have N randomized words (you could change N on the preference menu) and you have to speak each words correctly. In the end it will calculate the correct and wrong words that you have spoken.

From the technology standpoint, the app uses the Speech-to-text function of the phone (abuses it, lol) and used the database to store the words that you need to say. It's designed to be simple and designed for non native English speakers though native English Speakers are fully welcome to try it out.

At the end, either we win or not, we hope to change the world, to make it a better place through technology and through the gifts that had been given to us. We sincerely hope that our application would help you all in any way possible.

And I would like to thank Gary Law for this wonderful designs and layout.