Xemega
Xemega

Reputation: 31

Button click is not working: button does nothing

I'm using the following class and ive been trying for the last 24 hours i cant figure out why its not working. when i press the button it does nothing.

I'm creating this so i can verify the use login information

My class

public class login extends AppCompatActivity  {
Button button11;
EditText usernameField, passwordField;

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


    usernameField = (EditText) findViewById(R.id.user);
    passwordField = (EditText) findViewById(R.id.password);


    Button clickButton = (Button) findViewById(R.id.loginButton);
    clickButton.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"it 
            clicked",Toast.LENGTH_LONG);
            TextView mt=(TextView) findViewById(R.id.messy);

            mt.setText("Please Wait");
        }
    });


}}

I Added my XML file on https://codeshare.io/5XWxOE

Upvotes: 1

Views: 243

Answers (5)

Chetan Kumar Patel
Chetan Kumar Patel

Reputation: 287

There is another way to write code for button clicking

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

        Button firstButton = (Button) findViewById(R.id.loginButton);
        firstButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginButton: {
                Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();

                TextView textView = (TextView) findViewById(R.id.messy);
                textView.setText("Your text here");
                break;
            }
            default: {
                Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }
}

I hope this will resolve your issue.

Upvotes: 0

Soo Chun Jung
Soo Chun Jung

Reputation: 595

When something is going wierd.. you can check basic log. try this one~

public class login extends AppCompatActivity  {

Button button11;
EditText usernameField, passwordField;

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


    usernameField = (EditText) findViewById(R.id.user);
    passwordField = (EditText) findViewById(R.id.password);


    Button clickButton = (Button) findViewById(R.id.loginButton);

    clickButton.setText("Button Found");

    clickButton.setOnClickListener( new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        clickButton.setText("Button Clicked");
        }
    });
}}

Upvotes: 0

Android Geek
Android Geek

Reputation: 9225

Try this ...

TextView mt; 
Button clickButton;      


mt=(TextView) findViewById(R.id.messy);
clickButton = (Button) findViewById(R.id.loginButton);

    clickButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             Toast.makeText(this, "Button click toast", Toast.LENGTH_SHORT).show();
             mt.setText("Your text here");
                }
            });

Upvotes: 0

Jonny
Jonny

Reputation: 328

Please try this solution

 Toast.makeText(login.this,"it 
        clicked",Toast.LENGTH_LONG).show;

Instead of the getApplicationContext() you have.

Upvotes: 0

A Farmanbar
A Farmanbar

Reputation: 4788

You are creating Toast but do not call it to show its content try code blew :

 @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"it 
            clicked",Toast.LENGTH_LONG).show();
            TextView mt=(TextView) findViewById(R.id.messy);

            mt.setText("Please Wait");
        }

you forgot to use show()

Upvotes: 1

Related Questions