May 16, 2011

Detect "natural" orientation of screen, using Display.getRotation()

Detect

package com.AndroidOrientation;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidOrientation extends Activity{

TextView textOrientation;
MyOrientationEventListener myOrientationEventListener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textOrientation = (TextView)findViewById(R.id.textorientation);

myOrientationEventListener =
new MyOrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL);

if (myOrientationEventListener.canDetectOrientation()){
myOrientationEventListener.enable();
}else{
Toast.makeText(AndroidOrientation.this,
"Can't Detect Orientation!",
Toast.LENGTH_LONG).show();
}
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myOrientationEventListener.disable();
}

class MyOrientationEventListener extends OrientationEventListener{

String prevOrientation = "";
String curOrientation = "";

public MyOrientationEventListener(Context context, int rate) {
super(context, rate);
// TODO Auto-generated constructor stub
}

@Override
public void onOrientationChanged(int arg0) {
// TODO Auto-generated method stub

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
switch(display.getRotation()){
case(Surface.ROTATION_0):
curOrientation = "ROTATION_0";
break;
case(Surface.ROTATION_90):
curOrientation = "ROTATION_90";
break;
case(Surface.ROTATION_180):
curOrientation = "ROTATION_180";
break;
case(Surface.ROTATION_270):
curOrientation = "ROTATION_270";
break;
}

textOrientation.setText(curOrientation);

if (!curOrientation.equals(prevOrientation)){
Toast.makeText(AndroidOrientation.this, curOrientation, Toast.LENGTH_LONG).show();
prevOrientation = curOrientation;
}
}
}
}


May 12, 2011

Voice recognizer + Text to speech

Merge last two post; Start Android system's voice recognizer using Intent & Implement Android's Text to speech function.

Voice recognizer + Text to speech

Onec the recognized result selected, it will be speeched using TTS.

package com.AndroidRecognizer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class AndroidRecognizer extends Activity implements OnInitListener{

TextToSpeech tts;

Button startRecognizer;
Spinner spinnerResult;

private static final int RQS_RECOGNITION = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRecognizer = (Button)findViewById(R.id.startrecognizer);
startRecognizer.setEnabled(false);

spinnerResult = (Spinner)findViewById(R.id.result);

startRecognizer.setOnClickListener(startRecognizerOnClickListener);

tts = new TextToSpeech(this, this);

}

private Button.OnClickListener startRecognizerOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech to Recognize");
startActivityForResult(intent, RQS_RECOGNITION);
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if((requestCode == RQS_RECOGNITION) & (resultCode == RESULT_OK)){

ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerResult.setAdapter(adapter);

spinnerResult.setOnItemSelectedListener(spinnerResultOnItemSelectedListener);

}

}

private Spinner.OnItemSelectedListener spinnerResultOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String selectedResult = parent.getItemAtPosition(position).toString();
Toast.makeText(AndroidRecognizer.this, selectedResult, Toast.LENGTH_SHORT).show();
tts.speak(selectedResult, TextToSpeech.QUEUE_ADD, null);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}};

@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
startRecognizer.setEnabled(true);
}

}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startrecognizer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Recognizer"
/>
<Spinner
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

May 11, 2011

Start Android system's voice recognizer using Intent

Result of Android system's voice recognizer

package com.AndroidRecognizer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class AndroidRecognizer extends Activity {

Button startRecognizer;
Spinner spinnerResult;

private static final int RQS_RECOGNITION = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRecognizer = (Button)findViewById(R.id.startrecognizer);
spinnerResult = (Spinner)findViewById(R.id.result);

startRecognizer.setOnClickListener(startRecognizerOnClickListener);

}

private Button.OnClickListener startRecognizerOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech to Recognize");
startActivityForResult(intent, RQS_RECOGNITION);
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if((requestCode == RQS_RECOGNITION) & (resultCode == RESULT_OK)){

ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerResult.setAdapter(adapter);

}

}
}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startrecognizer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Recognizer"
/>
<Spinner
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


May 10, 2011

Implement Android's Text to speech function

Android's Text to speech function

package com.AndroidTTS;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTTS extends Activity implements OnInitListener{

TextToSpeech tts;
EditText textIn;
Button buttonSpeech;

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

tts = new TextToSpeech(this, this);

textIn = (EditText)findViewById(R.id.textin);
buttonSpeech = (Button)findViewById(R.id.speech);

buttonSpeech.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String textToBeSpeech = textIn.getText().toString();
tts.speak(textToBeSpeech, TextToSpeech.QUEUE_ADD, null);
}});
}

@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
//Make sure the Text-To-Speech engine is fully loaded
//It can be configured and used
buttonSpeech.setEnabled(true);
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
tts.shutdown();
}
}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/textin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/speech"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Speech"
android:enabled="false"
/>
</LinearLayout>


May 9, 2011

Make you App looked like a Dialog

To make your app looked like a dialog, you can use the Android pre-defined Theme.Dialog.

Modify AndroidManifest.xml, to add the code inside
android:theme="@android:style/Theme.Dialog"

example:
Make you App looked like a Dialog

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidDialog"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidDialog"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>


Infolinks In Text Ads