Daniel Kaplan
Daniel Kaplan

Reputation: 721

implements View.OnClickListener on MainActivity doesn't work

Still in my learning phase and either I misunderstand the implements keyword. Am at the chapter where the app's MainActivity is supposed to handle all click related functions.

I don't know if it's the book missing something, or I misread something in the book. But when I debug the app I never get the OnClick call. Otherwise the app runs with no errors.

While I already know how to assign a new button listener am stuck on this chapter and would love to be able to move on.

Here's the entire code, it's very short.

Would appreciate knowing what I am missing here.

Thank you

package ted.com.eventhandling;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


public class MainActivity2 extends AppCompatActivity
        implements View.OnClickListener
{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onClick(View view)
    {
        switch(view.getId())
        {
            case R.id.button1:
                show("Button One");
                break;
            case R.id.button2:
                show("Button Two");
                break;
            case R.id.button3:
                show("Button Three");
                break;
            default:
                show("Oh shit");
                break;
        }
    }
    void show(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        Log.i(getClass().getName(), message);
    }


}

Upvotes: 1

Views: 574

Answers (1)

Sachin Varma
Sachin Varma

Reputation: 2235

Use the below code,

package ted.com.eventhandling;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


public class MainActivity2 extends AppCompatActivity
        implements View.OnClickListener
{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);

    }

    @Override
    public void onClick(View view)
    {
        switch(view.getId())
        {
            case R.id.button1:
                show("Button One");
                break;
            case R.id.button2:
                show("Button Two");
                break;
            case R.id.button3:
                show("Button Three");
                break;
            default:
                show("Oh shit");
                break;
        }
    }
    void show(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        Log.i(getClass().getName(), message);
    }


}

You implemented the onClick but forgot to give clickListeners for your buttons, so none of buttons were not linked.

Upvotes: 3

Related Questions