Text to speech unusual behaviour in android - android

I am having a strange experience with text to speech.
see my code:
Button b;
TextView title, body;
TextToSpeech tts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popups);
b = (Button) findViewById(R.id.buttonok);
title = (TextView) findViewById(R.id.textViewtitle);
body = (TextView) findViewById(R.id.textViewbody);
b.setText("ok");
title.setText(Receive.address);
body.setText(Receive.body);
tts = new TextToSpeech(Popup.this, new TextToSpeech.OnInitListener() {
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
play();//this is not working??!!i don't know why
b.performClick();//even this is not working
/* but when i click this button it works??? how and why?*/
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
play(); // Popup.this.finish();
}
});
}
private void play() {
// TODO Auto-generated method stub
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);
}
My text to speech works fine only when i click the button but whenever i don't click this button and write the tts.speak() inside normal code it do not works...why?
regards charlie

You have to wait for onInit being called before you can start speak. In your code you call play() right after the declaration. onInit is a callback and it takes some time before it being called. If you click your button as soon as the button appears, sometime it will fail as onInit has not been called. You should have a class member boolean mIsReady and set it to true in onInit. And in your play() method
private void play() {
// TODO Auto-generated method stub
if (mIsReady) {
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);
}
}

b.performClick();//even this is not working
/* but when i click this button it works??? how and why?*/
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
play(); // Popup.this.finish();
}
});
You are performing a click with b.performClick() before setting its setOnClickListener. Also, you are better off making such calls on the onResume() method. OnCreate is meant to be used to bind views and prepare the activity. The onResume() method will be called before showing the view to the user on the foreground so that would be the best place to put this code.
Take a look at the activity lifecycle.

Related

1 Button 3 Command onclick android

How to include 3 Commands into one button?
I want insert open another activity, when button click.
This my code:
btnUpload = (Button) findViewById(R.id.btn_submit);
btnUpload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG, "submit clicked");
if (!Ultils.isConnectingToInternet(SubmitPropertiesActivity.this)) {
showMsg(getResources().getString(R.string.open_network));
} else {
doUpload();
}
}
});
Just add line in onClick method
startActivity(new Intent(getApplicationContext(),anotherActivity.class));
like
btnUpload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG, "submit clicked");
//other command
startActivity(new Intent(getApplicationContext(),anotherActivity.class));
}
});
Add start activity code in runOnUiThread after dismiss dialog and clear all data then add your start activity code.
runOnUiThread(new Runnable() {
public void run() {
try {
prgDialog.dismiss();
title.setText("");
price.setText("");
content.setText("");
address.setText("");
area.setText("");
lantai.setText("");
luasbangunan.setText("");
bathroom.setText("");
bedroom.setText("");
selected_amenities_list.clear();
marker_selected = 0;
//Here You add your start new activity code.
} catch (Exception e) {
e.printStackTrace();
}
}
});
Something like this could help you;
Write 3 Methods
1) Network check method
2) Upload image method
3) Start an Activity method
When the button is click;
Call Method (1)
if Network connection is there - then
Call Method (2) from Method (1)
Check if uploading method finishs, if it does -
Call Method (3) from there
This is a simple procedure, just giving you the idea I would use If I'm in the situation.

remove all listeners while performing some actions

I have a button (named button1) with an OnClickListener and after button is clicked, a task should be execute, and button should become unclickable until task is finished. This is my code:
#Override
public void onClick(View v) {
removeListeners();
executeMyTask();
addListeners();
}
private void disableListeners() {
button1.setOnTouchListener(null);
button2.setOnClickListener(null);
}
private void enableListeners() {
button1.setOnTouchListener(this);
button2.setOnClickListener(this);
}
Now it happen that, if I click button while executeMyTask is running (so when listener is disabled), when task finish, onClick is called again.
I would that all clicks performed while executeMyTask is running will be ignored.
how can I do ?
Why don't you just disable the whole button with:
button1.setEnabled(false);
button2.setEnabled(false);
And later just re-enable them:
button1.setEnabled(true);
button2.setEnabled(true);
It will be easier for the users to understand that the button is un-clickable at the moment.
There are 2 ways:
1) With disable all other buttons,
#Override
public void onClick(View v) {
setButtonEnabled(false);
executeMyTask();
setButtonEnabled(true);
}
private void setButtonEnabled(boolean enable) {
// TODO Auto-generated method stub
btn_1.setEnabled(enable);
btn_2.setEnabled(enable);
btn_3.setEnabled(enable);
}
2) Use a boolean to keep record of async task running or not:
Like:
#Override
public void onClick(View v) {
removeListeners();
executeMyTask();
addListeners();
}
// And in executeMyTask
onPreExecute(){
....
..
isAsyncRunning = true;
..
....
}
onPostExecute(){
....
..
isAsyncRunning = false;
..
....
}
//And in Button Click Listeners
{
if(!isAsyncRunning){
// Do something...
}
}

Code after ProgressDialog (Android) gets executed before the ProgressDialog's timer runs out

When I click a button BUTTON, I'm showing a ProgressDialog popup, which shows the round circling progress meter thing. Now that's supposed to run for N seconds, and after that, there's some code which executes only after the temporarily created ProgressDialog instance vanishes (when N seconds are up). The code shows some text in a TextView, and the text changes on every click of BUTTON, and an image which should change too. But the change is to be visible only after the ProgressDialog has ended
But the problem is, as soon as I click BUTTON, the text already changes, and the ProgressDialog circling dialog plays on for N seconds, which is not supposed to happen - the text should change only after the ProgressDialog finished circling for its N seconds, and the app View is back in focus, and the text gets changed then.
Here's the code:
BUTTON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
imageView.setVisibility(ImageView.INVISIBLE);
//Choosing MESXs type to show =================================
if(mTSFlag==1)
mTS=holA;
else
mTS=holB;
// ProgressDialog =================================
final ProgressDialog progressRing=ProgressDialog.show(MainActivity.this, "Please wait..", "This takes time..", true);
progressRing.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try
{
Thread.sleep(3000);
}catch(Exception e){}
progressRing.dismiss();
}
}).start();
//Image choosing logic =================================
imageView.setVisibility(ImageView.VISIBLE);
if(pTOGGLE)
imageView.setImageResource(R.drawable.IMG_XXT);
else
imageView.setImageResource(R.drawable.IMG_XYT);
wIHM_result.setText(mTS);
}
});
The problem is, that it's as if the Image Logic part (marked by a header comment), is run before the ProgressDialgo part of the code. The appropriate image shows up, the text is changed, I can see all that in the dimmed app screen while the ProgressDialog is making it's progress to the end of N seconds.
I want it to show the changed image and text (objects here in code - imageView and wIHM respectively) after the Progress Dialog circling thing has finished circling and the focus is back on the app's view.
Use AsynTask class.
Write the button click in it and use progress Dialog in AsynTask..
AsynTask have method like initBackground() , preExecute() , postExecute() ..
Start the progress Dialog in preExecute... write the logic in initBackground with ending the logic last line with ProgressDialog.dismiss(); and write the textchange code and imagechange logic in postExecute();
BUTTON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new ABC.Execute();
}
});
public class ABC extends AsyncTask<Void,Void,Void>
{
ProgressDialog downloadProgressDialog =new ProgressDialog(Accounts.this);
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
//Set your text and image object here....
}
#Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
downloadProgressDialog.setMessage("Message");
downloadProgressDialog.setCancelable(false);
downloadProgressDialog.show();
}
#Override
protected Void doInBackground(Void... arg0)
{
//Write your code here......
downloadProgressDialog.dismiss();
}
}
You'd better use the AsyncTask to do. If you want to fix the issue based on your code, you should do like these:
BUTTON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
imageView.setVisibility(ImageView.INVISIBLE);
//Choosing MESXs type to show =================================
if(mTSFlag==1)
mTS=holA;
else
mTS=holB;
// ProgressDialog =================================
final ProgressDialog progressRing=ProgressDialog.show(MainActivity.this, "Please wait..", "This takes time..", true);
progressRing.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try{
Thread.sleep(3000);
//Don't run your UI method in non-Ui thread.
//move progressRing.dismiss() to runOnUiThread();
yourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
progressRing.dismiss();
//Image choosing logic =================================
imageView.setVisibility(ImageView.VISIBLE);
if(pTOGGLE)
imageView.setImageResource(R.drawable.IMG_XXT);
else
imageView.setImageResource(R.drawable.IMG_XYT);
wIHM_result.setText(mTS);
}
}catch(Exception e){
}
}
}).start();
}
});
progressRing.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
// Image choosing logic codes should be here for processing after dialog showed
}
});
setOnShowListener sets a listener to be invoked when the dialog is shown

Any listener is possible in if condition

i do for this code but not supported for ths issue and what i do for solution this like as if codition inside any listener.
if (btn.isEnabled()) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
But why???
any button's onClickListener will only be called if its "enabled".
You dont need to bother about onClickListeners which are assigned to disabledButtons.
suppose your button is disabled at the launch of activity then this listener wont be applied to your button.
Now after some time if you enable this button (may be after some event etc.)
"THEN ALSO THIS LISTENER WONT WORK" as you did not set the listener at the first place...
so IMO dont put that in if...
this code successfully.
if (button.isEnabled()) {
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Test", 10).show();
}
});
}

Android Button Problem

i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.

Categories

Resources