0

I have a simple, but annoying problem:

In my fragment, I have a button which should open another activity (intent) on click. However I have to click the button twice, and only the second time it's opening the activity. Here is the xml layout of the button:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go!"
        android:onClick="login"
        android:id="@+id/bt_SignIn"
        android:layout_below="@+id/pass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp" />

And here is the code for onClick:

public void login(View view){
bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
     bt_SignIn.setOnClickListener(new View.OnClickListener() {
     @Override
            public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(), Frontpage.class);
                    startActivity(i);
         }
     }
}
Darek Kay
  • 15,827
  • 7
  • 64
  • 61
just_deko
  • 1,024
  • 1
  • 14
  • 29
  • You have defined `onClick` in XML as well as registering button `onClick` inside event function ? Use one option and as @Mattia suggested in the answer register it programmatically. – Jibran Khan May 03 '15 at 17:16

5 Answers5

2

You should avoid to use onClick directly in the XML, it's a bad idea.

Remove onClick from the XML:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Go!"
    android:id="@+id/bt_SignIn"
    android:layout_below="@+id/pass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="70dp" />

put this code in the onCreate of your Activity:

bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View view) {
        Intent i = new Intent(getApplicationContext(), Frontpage.class);
        startActivity(i);
    }
}
Community
  • 1
  • 1
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
2

Why are you defining Onclick listener for btn_signIn again? As you already define in your xml login method will call on button click.

Use only below code.

public void login(View view) {
    Intent i = new Intent(getApplicationContext(), Frontpage.class);
    startActivity(i);
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Jay Shah
  • 732
  • 4
  • 16
2

That is because of thee fact that in the function defined in xml you are setting the listener for the button. Do it from either the JAVA code or from the xml code.

This shall suffice

public void login(View view) {
    Intent i = new Intent(getApplicationContext(), Frontpage.class);
    startActivity(i);
}

What was happening earlier was that on the first click the function defined in the xml was getting invoked which inturn was setting the listener for the button, and so on the next click your activity was getting triggered

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Msk
  • 857
  • 9
  • 16
2

You're setting the onClick listener for the button twice. You're declaring the onClick in the XML so your method can just be.

public void login(View view){
     Intent i = new Intent(getApplicationContext(), Frontpage.class);
     startActivity(i);
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Joe Birch
  • 371
  • 2
  • 7
1

Change your login(View view) method as follows

public void login(View view){
    Intent i = new Intent(getApplicationContext(), Frontpage.class);
    startActivity(i);
}

Or much better put your code inside onCreate() method

bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View view) {
        Intent i = new Intent(getApplicationContext(), Frontpage.class);
        startActivity(i);
    }
}

and remove onClick from your layout

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Go!"
    android:id="@+id/bt_SignIn"
    android:layout_below="@+id/pass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="70dp" />
MChaker
  • 2,610
  • 2
  • 22
  • 38