comparing Text from Speech To Text API to text in a TextView - android

Brief description of Application thus far:
The user sees a TextView that has a word. The user will click on an ImageButton that will trigger the Speech To Text API and say the word given on the TextView. If the text from the Speech To Text and text in TextView match, then the TextView will change text to, "correct!" etc.
The issue is comparing the string text values. The TextView has "hello." The SpeechToText api returns "Hello" when I say "Hello," but the text values are considered different.
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Main"
android:orientation="vertical" >
<ImageButton
android:id="#+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"/>
<TextView
android:id="#+id/givenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:layout_gravity="center" />
</LinearLayout>
Main.Java
package com.example.speechtotext;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView inputText;
private TextView givenText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = (TextView)findViewById(R.id.inputText);
givenText = (TextView)findViewById(R.id.givenText);
btnSpeak = (ImageButton)findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try{
startActivityForResult(intent, RESULT_SPEECH);
inputText.setText("");
}catch(ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"your device does not support Speech to Text!",Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
inputText.setText(text.get(0));
if(givenText.getText().toString().equals(inputText.getText().toString())){
givenText.setText("Correct!");
}
else{
givenText.setText("Incorrect!");
}
}
break;
}
}
}
}
Any Ideas?

The problem you are facing can be solved easily by changing one line in your code, i.e:
if(givenText.getText().toString().equals(inputText.getText().toString())){
Change that to:
if(givenText.getText().toString().equalsIgnoreCase(inputText.getText().toString())){
Also, a general observation that I made when I was working on a similar app, I noticed that the Speech Recognition API isn't perfect and will not work well unless you/your user has an accent that matches the one that the API was trained on. So, in order to fix this you should consider going through the ArrayList of strings returned and checking ALL of them to see if any one of them matches the text you showed the user.

Related

I created an Android application with two activities, but cannot switch back from the second activity to the first

I created an Android application with two activities, but somehow the buttons on the second activity do not seem to work properly. The button on the first activity (a map activity and a button to add infromat which takes users to the second activity with an information entry screen) works fine, neither button on the second activity seems to have any effect... I can't even get an OnClick Toast message to work.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback , LocationListener {
public void AddMapInfo(View view) {
Intent intent = new Intent(this, InformationInputActivity.class);
LatLng providedLocation = current;
Bundle args = new Bundle();
args.putParcelable("provided_location", providedLocation);
intent.putExtra("bundle", args);
startActivityForResult(intent, 1);
}
public void onActivityResult (int requestCode, int resultCode, Intent data){
Toast.makeText(this, "back with cheese!", Toast.LENGTH_LONG).show();
if (requestCode != 1){
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();
}
else{
setContentView(R.layout.activity_maps);
InformationPoint informationPoint = (InformationPoint) data.getSerializableExtra("information_point");
informationPoint.display(mymap);
}
}
}
The second activity brings on the layout for the screen, and it sets the proper options for the EditText field, so I know it gets control... but that's about it. There is no Toast message from any of the methods (I still get the Toasts when the location update info is received on the first activity instead), and neither the radio button or submit buttons seem to work, I'm also not getting the result and control back in the first activity. (Previously, I used to be able to get a Toast on ever on checked option on the radio button, but now that doesn't seem to work either). Trying to send the activity result aborts the application (since I added finish() to the second activity).
/* java */
package com.example.m.googlemapappfirst;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
import org.w3c.dom.Text;
public class InformationInputActivity extends ActionBarActivity {
EditText editText;
String content = "";
LatLng providedLocation;
public InfoType infoType = InfoType.NO_INFO;
InformationPoint informationPoint;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_input);
Intent intent = getIntent();
Bundle bundle = getIntent().getParcelableExtra("bundle");
providedLocation = bundle.getParcelable("provided_location");
String message = String.valueOf(providedLocation);
editText = (EditText)findViewById(R.id.description);
editText.setHorizontallyScrolling(false);
editText.setMaxLines(5);
Toast.makeText(this, "enter cheese info", Toast.LENGTH_LONG);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
Toast.makeText(this,"you selected your cheese", Toast.LENGTH_LONG);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_information_input, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void AddPictureHandler(View view){
Toast.makeText(this, "i like to take pictures of cheese", Toast.LENGTH_LONG).show();
}
public void SubmitHandler(View view){
content = editText.getText().toString();
Toast.makeText(this, "add cheese info", Toast.LENGTH_LONG).show();
if(infoType == InfoType.NO_INFO){
Toast.makeText(this, "Please select an Information type", Toast.LENGTH_LONG).show();
}
else if(content.equals("")){
Toast.makeText(this, "Please write a description", Toast.LENGTH_LONG).show();
}
informationPoint = new InformationPoint(providedLocation, infoType, content);
Intent returnIntent = new Intent(this, MapsActivity.class);
returnIntent.putExtra("information_point", informationPoint);
this.setResult(1, returnIntent);
finish(); /* added after original post, this makes app crash consistently */
}
}
Here's the xml layout file for the second activity:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.m.googlemapappfirst.InformationInputActivity">
<TextView android:text="#string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_swiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swiss"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_american"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/american"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_gruyere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gruyere"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_cheddar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cheddar"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<TextView android:text="#string/instructions2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:hint="#string/type_here"
android:inputType="text"
android:imeOptions="actionDone"
android:maxLines ="4"
android:maxLength ="2000"
android:scrollHorizontally="false"
/>
<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="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<Button
android:id="#+id/take_picture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Picture"
android:onClick="AddPictureHandler" />
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Report Information"
android:onClick="SubmitHandler" />
</LinearLayout>
I didn't see any problem but missing invocation of show() after toast creation, just like this:
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();

how to give an audio file as input to the google speech to text api?

currently i made a 'speechtotext' app using the google speech to text api.
It takes whatever i speak near the mic as an input and displays the text form of it by sending it to google and getting the result from it.
Well,instead of speaking,i want to give an audio file as the input(which i have previously recorded using 'sound recorder' app) to my "speech to text" app.
This audio file is present in the sdcard of the phone.
Google speech to text api should choose this audio file as the input and then give its text form as the output.
I have pasted my "100% working" code of the xml and the java files for your references.
my xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/textView1"
android:background="#drawable/superim"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/tap_on_the_button_to_speak_"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<ImageButton
android:id="#+id/btnSpeak"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/speak"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/txtText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="0.38"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
/>
</LinearLayout>
my java file:
package com.prann.speechtotextdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
//import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"YOUR DEVICE DOESNT SUPPORT SPEECH TO TEXT \n install google vocie search ",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
}
Is it possible to do what i want to do ?? if so,how can it be done ?????
A detailed explanation would be very appreciated.
Thank you in advance. :-)

Button OnClickListener not working

I am new to Android. I am trying to get input from the EditText and displaying it in TextView with the help of button. When I click the button nothing happens. Please help, here is my MainActivity.java code -
package com.example.addname;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1;
TextView v1;
EditText t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
t1 = (EditText) findViewById(R.id.editText1);
v1 = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and here is my activity_main.xml
<RelativeLayout 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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="48dp"
android:text="Button" />
</RelativeLayout>
Instead of
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
You can use
if(t1.getText().isEmpty()) // isEmpty() is only available from API 9 and Above
{
v1.setText(t1.getText().toString());
}
or this
if(t1.getText().trim().length == 0)
{
v1.setText(t1.getText().toString());
}
Don't compare strings using ==. You're actually comparing reference of an object. To compare strings, use equals() method.
I'd recommend comparing strings with .equals() since Objects compared with == usually fails (== compares references, not Object content).
if(t1.getText().toString().equals("")))
in your case though,since you're comparing an empty String, you can also use
if (t1.getText().toString().length == 0)
Then note that once you set the text, you're setting v1's text to an empty string since your logic indicates that only upon an empty string you set v1's text. So I'd recommend getting rid of the if altogether:
#Override
public void onClick(View arg0) {
v1.setText(t1.getText().toString());
}

What's wrong with using int variable ? Making the application to crash

This is my MainActivity.java
I used // to mark as not using on some lines and found that the line start with int t =
Is the problem that make the application to crash and force closing.
package com.example.camera_test;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 1337;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
#SuppressLint("NewApi") #Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, CAMERA_PIC_REQUEST );
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#SuppressLint("NewApi") protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}
I added a button when I click on it it will open the camera on my device after I take a photo it will show the photo I took on a small window. It's working.
Then I added this 3 lines:
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
I wanted to show also the ByteCount of the image on screen in my device.
Once I add the line: int t = thumbnail.getByteCount(); there was an error so I did automatic fix and it added #SuppressLint("NewApi")
Then I marked with // this 3 lines once I unmarked and used the line int t = thumbnail.getByteCount(); so after I took a photo it crashed and told me it need to force close.
Why does it crash on this line ?
This is the file activity_main.xml
<RelativeLayout 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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="45dp"
android:layout_marginTop="62dp"
android:text="Activate The Camera" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
I can't figure out why it crash with the int t line.
I added a breakpoint on the int t line but it never stop there and never stop anywhere on my application when I add a breakpoint why ?
The getByteCount() is available from API level 12 (android 3.1+).
Are you sure you are building/running this version or above?
Also please add LogCat in the feature, much easier to find the error.

EditText.getText() returns text from different view in onPause after rotating device

I have a strange problem here with an EditText view. In onPause() after an orientation change, the EditText view returns text via getText() that was never assigned to it.
In fact, the EditText object in question is only assigned an empty string.
In the error situation, it returns part(!) of a text that was assigned to a different TextView.
This does however not happen if onPause is triggered by pressing the "back" key.
Can anybody give me a clue? I do not understand what is happening here:
My activity:
package com.example.rotationtest;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String LOG_TAG = "Test";
private EditText mEditText;
private TextView mTextView;
private EditOk mEditOk;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.title);
mEditText = (EditText) findViewById(R.id.editText);
mEditText.setText("");
mEditOk = new EditOk() {
#Override
public void ok(String result) {
mTextView.setText(result);
}
};
editTextDialog(R.string.dialog_title, (CharSequence)getString(R.string.dialog_title), mTextView.getText().toString(), mEditOk);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
abstract class EditOk {
abstract public void ok(String result);
}
void editTextDialog(int titleId, CharSequence message, String text,
final EditOk ok) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.edittext_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView messageView = (TextView) layout.findViewById(R.id.text);
messageView.setText(message);
final EditText input = (EditText) layout.findViewById(R.id.editTextDialog);
Log.d(LOG_TAG, "input = " + input);
input.setTransformationMethod(android.text.method.SingleLineTransformationMethod.getInstance());
input.setText(text);
new AlertDialog.Builder(this)
.setTitle(titleId)
.setView(layout)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String newName = input.getText().toString();
if (newName.length() != 0) {
ok.ok(newName);
}
}})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_CANCELED);
finish();
}
})
.create().show();
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_TAG, "onPause: editText is " + mEditText.getText());
}
}
and layout:
<RelativeLayout 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" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="textMultiLine" />
</RelativeLayout>
There is an AlertDialog involved which's layout is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingBottom="20dp" android:paddingTop="15dp" android:layout_gravity="top">
<TextView android:id="#+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#FFF"
android:gravity="top" android:textSize="16sp" android:paddingBottom="20dp"/>
<EditText
android:id="#+id/editTextDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
Now the steps are these:
Start activity in portrait orientation. A dialog pops up containing an EditText, prefilled with "Hello World!"
Append "zzz" to "Hello world!"
Press OK. "Hello world!zzz" is now assigned to the TextView on the Activity.
Now rotate the device to landscape. In onPause, mEditText.getText() now returns "zzz" although mEditText was not touched at all.
Any ideas? My expectation is that mEditText.getText() always returns "". If you repeat these steps but trigger onPause() by pressing back instead of rotating the device, getText() indeed does return "" as expected. Why not when rotating the device?
Additional note: I noticed that the soft keyboard seems to be necessary for the issue to appear. On an emulator with "Keyboard support = yes", the issue doesn't show up.
I had the same kind of issue with EditText. Looking in forums, I found that setting android:inputType="textNoSuggestions" fixes the issue, I don't know exactly how it works but, it worked fine on the Motorola Et1 tablet(Android 2.3).

Categories

Resources