2

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!

Community
  • 1
  • 1
Taumich
  • 23
  • 4

3 Answers3

2

It's because the buttons your are clicking, actually do not refer to those defined in XML layout file. With this declaration:

button1 = new Button(this);

you create a new button, but don't refer to those declared in XML layout.

The correct way is to get a reference to button declared in XML layout:

button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
Andy Res
  • 15,963
  • 5
  • 60
  • 96
2

Don't use

Button button1 = new Button(this);

as that creates a new button, rather than tying it to the button you added to your XML. Instead, use

Button button1 = (Button) findViewById(R.id.button1);

Similarly for button2.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
1

problem is in here

    button1 = new Button(this);
    button2 = new Button(this);

instead of using existing buttons you are creating new ones. use like

    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);

An additional info

no need to implement onClickListener here. you are already setting the listeners separately. So it is not needed here

Some More Info

It seems you are new to use onClickListener. here are different ways of using this.

  1. use annonymouse inner class in setOnClickLIstener method like you did

    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
    }
    

    });

  2. Implement OnClickListener and override onClick method. but in that case don't forget to set the listener like

    button.setOncliClistener(this);
    

or if you use another class for listener use like

     button.setOnclickListener(thatclass);
  1. my favourite way is. use android:onClick method in xml layout and declare the method like following

    android:onClick="onClick"

in class

    public void onClick(View v){
        // do your tasks
    }
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Thank you for the information! Yes I'm pretty new to most methods in Java. I'll try to stick with the android:onClick for now though :) – Taumich Jun 20 '13 at 17:00