Samuel Georgeszusz
Samuel Georgeszusz

Reputation: 131

Have to click a button twice for it to work in Android Studio

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

Upvotes: 2

Views: 5709

Answers (5)

Marc P&#233;rez
Marc P&#233;rez

Reputation: 293

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.

Upvotes: 0

marktani
marktani

Reputation: 7808

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.

Upvotes: 2

lifesoft
lifesoft

Reputation: 21

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

Upvotes: 0

OBX
OBX

Reputation: 6114

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));
    }
});

Upvotes: 0

Spirrow
Spirrow

Reputation: 1150

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));
    }
});

Upvotes: 1

Related Questions