Ajay Sainy
Ajay Sainy

Reputation: 379

How to start new activity on button click in android?

I am trying to go from one activity to another when clicking the 'Login' button, here is my code for button click-

public void onClick(View v) {
                // TODO Auto-generated method stub

                if(id.getText().toString().equals("ajay")&&pass.getText().toString().equals("sainy"))
                {
                    Intent i = new Intent(MainActivity.this,adminhome.class);
                    MainActivity.this.startActivity(i);

                }
                else if(id.getText().toString().equals("aj")&&pass.getText().toString().equals("sa"))
                {
                    Intent i = new Intent(MainActivity.this,userhome.class);
                    MainActivity.this.startActivity(i);                 
                }
                else
                    res.setText("Incorrect Credentials...Retry");
            }
        });

but when I enter correct credentials 'adminhome' or 'userhome' activity is not starting. The same 'MainActivity' is opening again. I think I have problem in following code -

Intent i = new Intent(MainActivity.this,adminhome.class);
                    MainActivity.this.startActivity(i);

or in,

Intent i = new Intent(MainActivity.this,userhome.class);
                    MainActivity.this.startActivity(i);                 

What is the problem? Please help, I am learning android.

Upvotes: 0

Views: 2491

Answers (3)

user1918566
user1918566

Reputation: 221

Implement the View.OnClickListener interface and override the onClick method.

ImageView btnWatchVideo;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search1);
        ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(this);
    }
 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSearch: {
                Intent intent = new 
             Intent(Search.this,SearchFeedActivity.class);
                startActivity(intent);
                break;
            }
}

Upvotes: 0

Hossa The Coder
Hossa The Coder

Reputation: 85

you can check and see if the new activity is put inside the manifest or not, as i did below

        <activity
        android:name="com.hossa.multitask.activity2"
        android:label="@string/app_name" >
        </activity>

Upvotes: 0

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

The answer is in the comments, but anyway, when anyone of you encounters such behavior:

  • Make sure the other activities do not have the same layout, and, so, you will think that the same activity is being open when it is the other activity. Just double check the layout of the activities.

Upvotes: 1

Related Questions