user7192096
user7192096

Reputation:

Button Click handler is not working in Android Studio

Directory structure of my activity is like below

enter image description here

That's why to get the button id, I wrote below code

View rootView = getLayoutInflater().inflate(R.layout.activity_login, null, false);

btnLogin = (Button)rootView.findViewById(R.id.btnLogin);

But, due to some reasons, when I click button, it does not come inside hander. Am I missing something? My code is below.

btnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    }
});

Complete code of Activity

package com.example.pankajgarg.android.Activities.UserManagement.Auth.Login;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.pankajgarg.android.R;

public class LoginActivity extends AppCompatActivity {

    Button btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        View rootView = getLayoutInflater().inflate(R.layout.activity_login, null, false);

        btnLogin = (Button)rootView.findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();
            }
        });

        setContentView(R.layout.activity_login);
    }
}

Upvotes: 1

Views: 8407

Answers (6)

Meghna
Meghna

Reputation: 537

I changed

Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();

to

Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();

it worked forme

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83597

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    View rootView = getLayoutInflater().inflate(R.layout.activity_login, null, false);

    btnLogin = (Button)rootView.findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();
        }
    });

    setContentView(R.layout.activity_login);
}

The line setContentView(R.layout.activity_login); inflates the layout and displays the views defined in your XML. These are completely different instances than those created by the previous call to inflate(). You seem to be mixing the way we inflate a view for a fragment and the way we do it for an activity. In Activities, we just call setContentView(). This means you should change your code to the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    btnLogin = (Button)findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

Now the code to set the OnClickListener will find the correct button.

Note that the first two lines of onCreate() will always be as shown here. The only difference will be the name of the layout used in setContentView(). You will avoid a lot of headache if you always follow this pattern.

Upvotes: 1

Pztar
Pztar

Reputation: 4779

Remove View rootView = getLayoutInflater().inflate(R.layout.activity_login, null, false); and replace it with setContentView

public class LoginActivity extends AppCompatActivity {

    Button btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Upvotes: 3

Alberto Méndez
Alberto Méndez

Reputation: 1064

First of all, in an Activity you don't need to inflate the layout, you are linking it with the setContentView method:

setContentView(R.layout.activity_login);

Secondly, if you want to do it the anonymous way you are ready to go, but add something to the onClick method:

btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // do whatever like...
        Log.d("MyActivity", "Click!");
        Toast.makeText(this, "Click!", Toast.LENGTH_SHORT);
    }
});

Or you can do it this way:

btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);  

If you do it this way you must implement the View.OnClickListener in your Activity with:

public class LoginActivity extends Activity implements View.OnClickListener {
     // Your Activity code
}

And finally, again, if you don't do anything in your click method Android won't do it, so:

@Override
public void onClick(View view) {
    // do whatever like...
    Log.d("MyActivity", "Click!");
    Toast.makeText(this, "Click!", Toast.LENGTH_SHORT);
}

Upvotes: 2

landrykapela
landrykapela

Reputation: 472

The best way you can go about it is by using setContentView(R.layout.activity_login) in your onCreate() method and then pull the button with findViewById(R.id.btnLogin).

protected void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   //insert this line here
   setContentView(R.layout.activity_login);

  //Pull the button with the id
  btnLogin = (Button) findViewById(R.id.btnLogin);

  //set onclicklistener
  btnLogin.setOnClickListener(new View.OnClickListener(){
   public void onClick(View view){
     Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();
   }
 });
}

Upvotes: 2

Beppi's
Beppi's

Reputation: 2129

Assuming that you did what @Code-Apprentice says (put something to execute), there are other things that can happen:

  • Is the Button clickable? Check it in the layout or set it with

    btnLogin.setClickable(true);

  • is the Button enabled? Same as before.

  • is there an other View overlapping the button that catches the click before it?

Upvotes: 0

Related Questions