HTML ONLINE

Wellcome to html!

Thứ Sáu, 28 tháng 12, 2012

Get installed TTS engines, by calling getEngines() method.

Start from Android API level 14, List<TextToSpeech.EngineInfo> getEngines() method was provided to get a list of all installed TTS engines.

To display installed TTS Engines:

display installed TTS Engines


package com.example.androidtexttospeech;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnInitListener{

TextView tvDefaultTTS;

Spinner spInstalledEngines;
TextToSpeech tts;
List<TextToSpeech.EngineInfo> listInstalledEngines;
List<String> listInstalledEnginesName;
String defaultTTS;

private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvDefaultTTS = (TextView)findViewById(R.id.defaulttts);
spInstalledEngines = (Spinner)findViewById(R.id.installedengines);

tts = new TextToSpeech(this, this);
listInstalledEngines = tts.getEngines();
listInstalledEnginesName = new ArrayList<String>();

for(int i = 0; i < listInstalledEngines.size(); i++){
listInstalledEnginesName.add(listInstalledEngines.get(i).label);
}

adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, listInstalledEnginesName);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spInstalledEngines.setAdapter(adapter);

defaultTTS = tts.getDefaultEngine();
tvDefaultTTS.setText("Default TTS Engine: " + defaultTTS);
}

@Override
public void onInit(int status) {
// TODO Auto-generated method stub

}

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

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<TextView
android:id="@+id/defaulttts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Spinner
android:id="@+id/installedengines"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>


Next:
- Get features supported by TextToSpeech Engine

Thứ Năm, 27 tháng 12, 2012

Get device's Android ID

Secure.ANDROID_ID is a 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device. (The value may change if a factory reset is performed on the device.)

device's Android ID


package com.example.androidid;

import android.os.Bundle;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
Toast.makeText(getApplicationContext(), id, Toast.LENGTH_LONG).show();
}

}


Thứ Năm, 13 tháng 12, 2012

Read raw text file, and display in ScrollView.

This article demonstrate how to read raw text file from /res/raw/ folder, and display the text in a TextView inside ScrollView.

Read raw text file, and display in ScrollView.


Create raw text file at /res/raw/mytext.txt.
Santa Claus Is Coming To Town:

You better watch out
You better not cry
Better not pout
I'm telling you why
Santa Claus is coming to town

He's making a list,
And checking it twice;
Gonna find out Who's naughty and nice.
Santa Claus is coming to town

He sees you when you're sleeping
He knows when you're awake
He knows if you've been bad or good
So be good for goodness sake!

O! You better watch out!
You better not cry.
Better not pout, I'm telling you why.
Santa Claus is coming to town.
Santa Claus is coming to town.


Add a ScrollView with TextView inside, to our layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/mytextview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>

</LinearLayout>


Main code:
package com.AndroidTextResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView myTextView = (TextView)findViewById(R.id.mytextview);

InputStream inputStream = getResources().openRawResource(R.raw.mytext);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

String myText = "";
int in;
try {
in = inputStream.read();
while (in != -1)
{
byteArrayOutputStream.write(in);
in = inputStream.read();
}
inputStream.close();

myText = byteArrayOutputStream.toString();
}catch (IOException e) {
e.printStackTrace();
}

myTextView.setText(myText);
}

}



Thứ Năm, 6 tháng 12, 2012

Draw path on SurfaceView's canvas

Example to detect touch events and draw path on SurfaceView's canvas accordingly.

Draw path on SurfaceView's canvas


package com.TestSurefaceView;

import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity {

MySurfaceView mySurfaceView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}

class MySurfaceView extends SurfaceView{

Path path;

Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Random random;

public MySurfaceView(Context context) {
super(context);
surfaceHolder = getHolder();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(Color.WHITE);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_UP){
path.lineTo(event.getX(), event.getY());
}

if(path != null){
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawPath(path, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}

return true;
}
}
}



Related Post:
- Detect multi-touch, on SurfaceView

Thứ Tư, 5 tháng 12, 2012

Capture screen with getDrawingCache() repeatly

In the article "Create custom dialog with dynamic content, updated in onPrepareDialog()", it demonstrate how to capture screen with setDrawingCacheEnabled() and getDrawingCache() methods. Unfortunately, it cannot work as expected! The screen is captured in the first time only. After that, every time call getDrawingCache() return the same first screen.

To solve it call setDrawingCacheEnabled(false) and then setDrawingCacheEnabled(true) to re-load the cache bitmap.

Modify onClick() method of btnCaptureScreen's OnClickListener:
     btnCaptureScreen.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
screen.setDrawingCacheEnabled(false);
screen.setDrawingCacheEnabled(true);
bmScreen = screen.getDrawingCache();
showDialog(ID_SCREENDIALOG);
}});


Capture screen with getDrawingCache() repeatly