So I have an android app (java in Eclipse) and I want to change the text of some buttons on a keyboard through a shift method. I implemented the same code as I did to change a textview text, the same as everyone says to on similar questions, but for some reason it WILL NOT work. For some reason, after testing other button functions, I've determined that there's something it doesn't like about me changing ANY property of the button. Tried cleaning the project, didn't help. Keep getting an invocation exception. Here is relevant code:
public class MainActivity extends Activity {
boolean shift = true;
static Vector<String> answer = new Vector<String>(1, 1);
static int ansLength = 0;
private TextView answerbox;
private Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeButtons();
setContentView(R.layout.activity_main);
answerbox = (TextView) findViewById(R.id.answerbox);
}
@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;
}
public void initializeButtons() {
a = (Button) findViewById(R.id.a);
}
public void typeKey(View sender) {
Button pressed = (Button) sender;
answer.add(ansLength, (String) pressed.getText());
//answerbox.setText("test string");
ansLength++;
StringBuilder stringBuilder = new StringBuilder();
for (String string : answer) {
stringBuilder.append(string);
}
answerbox.setText(stringBuilder.toString());
}
public void backSpace(View sender) {
answer.remove(ansLength - 1);
ansLength--;
StringBuilder stringBuilder = new StringBuilder();
for (String string : answer) {
stringBuilder.append(string);
}
answerbox.setText(stringBuilder.toString());
}
public void shift(View sender) {
if (shift == true) {
shift = false;
a.setText("l");
}
}
}
XML below:
<Button
android:id="@+id/a"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="A"
android:onClick="typeKey"/>
<Button
android:id="@+id/shift1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="^"
android:textSize="24sp"
android:onClick="shift" />