I am trying to make a simple application with one layout and two buttons which are going to change the layout text. But I cant find a solution to why the android:onClick doesn't execute. And after trying with an OnClickListener which didn't work but as they did here: How exactly does the android:onClick XML attribute differ from setOnClickListener?
I have read and followed the tutorial in the ANdroid SDK website here: http://developer.android.com/reference/android/widget/Button.html
This is my XML code:
<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"
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=".MainActivity" >
<TextView
android:id="@+id/maintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
Here is my Java code:
public class MainActivity extends Activity implements OnClickListener{
TextView label1;
int stage;
Button button1, button2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label1 = new TextView(this);
TextView label1 = (TextView) findViewById(R.id.maintext);
stage = 1;
label1.setText("hello " + stage);
Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT"); //working
button1 = new Button(this);
button2 = new Button(this);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1_click(v);
Log.d("button1 working", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button2_click(v);
Log.d("button2 working", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
});
}
public void button1_click(View v) {
stage = 2;
label1.setText("hello " + stage);
Log.d("b1click", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
public void button2_click(View v) {
stage = 3;
label1.setText("hello " + stage);
Log.d("b2click", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
@Override
public void onClick(View v) {
Log.d("wrong method", "THIS IS DEBUG OUTPUT TO LOGCAT"); // not appearing
}}
So the only Logcat message i get is the one in the OnCreate method. The stage variable always stays at 1 and doesn't change when i click at the buttons. The buttons however animates the click so it must be either the OnClick or the label itself which is not working.
Image of the emulator running: https://i.stack.imgur.com/ioUoD.png
Thanks in advance!