2

So I am currently creating an app and one of the small things that have been bothering me is the fact that I have to click a button twice for it to work.

This is my code and I can't see anything wrong with it:

public void signUpButtonClickAction(View v){
    Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
    signUpButtonClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Signup.class));
        }
    });
}

xml code for my button:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/signUps"
    android:id="@+id/signUpButton"
    android:layout_marginBottom="38dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="signUpButtonClickAction"/>

It is probably a small fix but even I can't spot this bug

E_net4
  • 27,810
  • 13
  • 101
  • 139

5 Answers5

2

Solution

Remove the line android:onClick="signUpButtonClickAction" and add

Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, Signup.class));
    }
});

to the onCreate method of your activity or the onCreateView method of your fragment.

Alternative Solution

Alternatively, change the code to this

public void signUpButtonClickAction(View v) { 
    startActivity(new Intent(MainActivity.this, Signup.class));
}

Explanation

The line android:onClick="signUpButtonClickAction" in the xml is causing an internal call to signUpButtonClick.setOnClickListener(), so you don't have to set up an onClickListener in the signUpButtonClickAction again.

Initializing multiple buttons

private void initializeButtons() {
    Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
    signUpButtonClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Signup.class));
        }
    });

    Button anotherButton = (Button) findViewById(R.id.anotherButton);
    anotherButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("TAG", "Clicked on another button!");
        }
    });
}

Now simply call initializeButtons() from the onCreate method of your activity.

marktani
  • 7,578
  • 6
  • 37
  • 60
  • Hi, why did you remove the `onClickListener`? I think it works otherwise but now I am confused as to why there is no listener :/ – Samuel Georgeszusz Feb 12 '16 at 23:54
  • What you were doing is to set a new onClickListener upon first click on the button. If you are using `android:onClick="signUpButtonClickAction"` in your xml, Android already sets the onClickListener internally to the method you specified - signUpButtonClickAction in this case. – marktani Feb 12 '16 at 23:58
  • @SamuelGeorgeszusz A listener is set by the framework and calls the method you mentioned in `android:onClick`. – Eugen Pechanec Feb 12 '16 at 23:58
  • @SamuelGeorgeszusz The line 'android:onClick="signUpButtonClickAction"' makes sure that your listener function is called when the signup button is clicked – chaitanya Feb 12 '16 at 23:58
  • I see. Thanks for the explaination - it makes sense. I never knew that the `android:onClick="signUpButtonClickAction"` makes a function call to listener. Now I know, and as you have explained it well and weren't rude therefore I have upvoted and accepted your answer :) – Samuel Georgeszusz Feb 13 '16 at 00:00
  • No problem, glad to help you out! I updated the post, and now list using `android:onClick` in the xml as the alternative solution, as I think manually setting the onClickListener is cleaner and less confusing. – marktani Feb 13 '16 at 00:03
  • Thanks, but what if I have multiple methods like `addUserButtonClickAction()`? Like how would I manually add the `onClickListener` to those? – Samuel Georgeszusz Feb 13 '16 at 00:06
  • Are you talking about initializing multiple buttons? What I usually do is to use a private helper function in `onCreate` that initializes every view element in my activity. – marktani Feb 13 '16 at 00:09
  • @mcwise do you mind please showing me an example of this private helper function in `onCreate`? Thanks – Samuel Georgeszusz Feb 13 '16 at 00:13
  • see my edit - did I correctly understand your question? – marktani Feb 13 '16 at 00:24
1

The problem is that you are setting two times a onClick action. In your xml code you have just asign an onClick() to your button, you don't need to setOnClickListener() inside the signUpButtonClickAction(View v). You have two options:

Leave the xml file like it is and inside signUpButtonClickAction(View v) do :

public void signUpButtonClickAction(View v){
    startActivity(new Intent(MainActivity.this, Signup.class));
}

OR

Remove the onClick of your xml file:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/signUps"
android:id="@+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>

And do this in your Activity:

Button yourButton = (Button) findViewById(R.id.signUpButton);
yourButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, Signup.class));
    }
});
Spirrow
  • 1,120
  • 6
  • 18
  • Thank you for answering my question. I will still upvote your answer as I have already accepted the previous answer. Just out of interest, how do I program a button so that it asks the user if they still want to go back? – Samuel Georgeszusz Feb 13 '16 at 00:02
  • I am not sure if I understand your question, whatever action you would like to be carried out when a button is pressed you have to write it inside the onClick(). It will depend on what you are trying to do exactly, but just as an example, you can create a LinearLayout with visibility `View.GONE`, and when you click on the button you change the visibility of the LinearLayout to `View.VISIBLE` which contains two buttons, one for going back and another one for moving to the next activity, – Spirrow Feb 13 '16 at 00:09
0

The cause of the problem is : onclick() and onClickListener are literally the same! And you are implementing both, the end result is you'll need to press the button twice to start the Activity!

FIX:

The solution to your problem is :

1:

public void signUpButtonClickAction(View v)
{
    startActivity(new Intent(MainActivity.this, Signup.class));
}

2:

Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, Signup.class));
    }
});
Community
  • 1
  • 1
OBX
  • 6,044
  • 7
  • 33
  • 77
0

as mcwise said android:onClick="signUpButtonClickAction" and signUpButtonClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, Signup.class)); } }); does the same thing. so you have to go with one of them. Having the two is causing the problem

lifesoft
  • 21
  • 2
0

For whom it may concern: I had the same issue but none of the solutions above solved it. For some reason I cannot understand, I had in my button this line of code:

android:textIsSelectable="true"

Deleting this attribute from the button makes it work.

This obviously made the first click to select the text, and the second click triggered the onClick button.

Marc Pérez
  • 293
  • 3
  • 9