I am following a tutorial out of a book. I know how anceint right? Well anyways I have arrived to a point where we are making a simple application that allows for users to input text and press a button. When the button is pressed the text that is inputted is converted into all CAPS.
There is a step where we add a feature to the app so that when the user presses enter (after the desired text has been inputted) the text is converted. (No need to press the convert button)
I have ran my application and tried to debug it, but have gotten no where. From what logcat is saying (or more like not saying) onKey is never called.
Is there something that I am missing? Below is the java class and the layout for the Activity. Thanks in advance.
TipCal.java (don't ask why I named it that)
package com.njh.add.tipclaculator;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TipCal extends Activity implements OnClickListener, OnKeyListener{
private TextView tv = null;
private Button convertButton = null;
private Button quitButton = null;
private EditText et = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip_cal);
tv = (TextView)findViewById(R.id.my_TextView);
convertButton = (Button)findViewById(R.id.my_Button);
quitButton = (Button)findViewById(R.id.quit);
et = (EditText)findViewById(R.id.my_EditText);
convertButton.setOnClickListener(this);
quitButton.setOnClickListener(this);
et.setOnKeyListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tip_cal, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
Log.d(v.toString(),"In onClick");
if(v == convertButton){
//Take the text from the EditText and make it all CAPS
//Then update the TextView with the CAPS string
String editTextContent = et.getText().toString();
editTextContent = editTextContent.toUpperCase();
tv.setText(editTextContent);
et.setText("");
}else if(v == quitButton){
this.finish();
}
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//When the user press enter after typing in what they want to
//convert the conversion is done. No need to press any other button
Log.d(v.toString(),"In onKey");
if(event.getAction() == KeyEvent.ACTION_DOWN){
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
this.onClick(v); //Not sure this is legal but don't see why I couldn't do this.
}
}
return false;
}
}
Here is the layout XML for this Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/base"
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="com.njh.add.tipclaculator.TipCal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My First Android Application"
android:id="@+id/my_TextView" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Touch Me"
android:id="@+id/my_Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quit"
android:id="@+id/quit" />
</LinearLayout>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type something to get turned into CAPS"
android:id="@+id/my_EditText" />
</LinearLayout>
UPDATE
What I have found so far is that TextWatcher is actually hitting my code. Which is good. Now my app crashes when I hit enter.
BUT!
I will take that. I think the new issue is the three functions that you implement when you make a TextWatcher are called twice for each key you press. (on keydown and keyup). This is just a guess. Anyone know for sure?
UPDATE
TextWatcher
was not calling its methods multiple times per change. The soft keyboard was updating things like automatically adding spaces or autocorrecting. I noticed this when I inputted "ll" and my keyboard updated it to "lol " which would cause it to update for the change from "ll" to "lol" to "lol ".
I am about to do a bit more digging. If I conclude on a workable solution I will post to share the wealth.