Home » Android » onclick – Android: Button OnClickListener does not working

onclick – Android: Button OnClickListener does not working

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have created this activity that should allow me to open a new activity once a button has been pressed.

However the OnClickListener does not seem to be working.

Am I declaring the buttons wrong?

Can someone me out?

public class Menu extends Activity {


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

    View loginbutton = findViewById(R.id.butlogin);
    loginbutton.setOnClickListener(this);

    View recordbutton = findViewById(R.id.butrecordts);
    recordbutton.setOnClickListener(this);

    View viewbutton = findViewById(R.id.butviewts);
    viewbutton.setOnClickListener(this);

    View projectsbutton = findViewById(R.id.butprojects);
    projectsbutton.setOnClickListener(this);

}

public void onClick(View v){

    switch(v.getId())
    {
    case R.id.butlogin:
    {
        //open login screen
        Intent i = new Intent(this, login.class);
        startActivity(i);
        break;
    }
    case R.id.butrecordts:
    {
        break;
    }
    case R.id.butviewts:
    {
        break;
    }
    case R.id.butprojects:
    {
        break;
    }

    }
}
How to&Answers:

Yes, The Problem is in Declaration of button, write below code instead of your code, it will solve your problem.

public class Menu extends Activity implements OnClickListener{

    Button loginbutton, recordbutton, viewbutton, projectsbutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        loginbutton = (Button) findViewById(R.id.butlogin);
        loginbutton.setOnClickListener(this);

        recordbutton = (Button) findViewById(R.id.butrecordts);
        recordbutton.setOnClickListener(this);

        viewbutton = (Button) findViewById(R.id.butviewts);
        viewbutton.setOnClickListener(this);

        projectsbutton = (Button) findViewById(R.id.butprojects);
        projectsbutton.setOnClickListener(this);

    }

    public void onClick(View v){

        switch(v.getId())
        {
            case R.id.butlogin:
            {
                //open login screen
                Intent i = new Intent(this, login.class);
                startActivity(i);
                break;
            }
            case R.id.butrecordts:
            {
                break;
            }
            case R.id.butviewts:
            {
                break;
            }
            case R.id.butprojects:
            {
                break;
            }

        }
    }
}

Answer:

You forget to implement onClickListener in your Activity.

Implement it and then try 🙂

public class Menu extends Activity implements onClickListener

Answer:

    View.OnClickListener myhandler1 = new View.OnClickListener() {
    public void onClick(View v) {
     // the 1st button
     }
    }
    View.OnClickListener myhandler2 = new View.OnClickListener() {
    public void onClick(View v) {
     //the 2nd button
     }

Answer:

You have to declare your buttons like this:

Button loginbutton = (Button)findViewById(R.id.butlogin);
    loginbutton.setOnClickListener(this);

    Button recordbutton = (Button)findViewById(R.id.butrecordts);
    recordbutton.setOnClickListener(this);

    Button viewbutton = (Button)findViewById(R.id.butviewts);
    viewbutton.setOnClickListener(this);

    Button projectsbutton = (Button)findViewById(R.id.butprojects);
    projectsbutton.setOnClickListener(this);

Answer:

You have just forgotten to implement the onClickListener in your activity:

public class Menu extends Activity implements OnClickListener { <—-Check out this line
……………………….
}

Answer:

Try changing this line

 View loginbutton = findViewById(R.id.butlogin);

to

 Button  loginbutton = (Button)findViewById(R.id.butlogin);