How to navigate from one screen to another screen

Viewed 76746

How to navigate from one Activity screen to another Activity screen? In the first screen I'm having one button if I click the button it has to move to another Activity screen.

18 Answers
OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(action));
    }
};

Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);
Button x.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) 
    {
        Intent i = new Intent(y.this, Activity.class);
        startActivity(i);                   
    }
});

Here we've defined a listener for Button x. The OS will call this method and start the Activity referenced in Intent i.

Here's the official tutorial example: http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html

public void onClick(View v) 
{
    Intent myintent = new Intent(currentclass.this, nextactivity.class);
    startActivity(myintent);                               
}

Use following code..I hope this will help you.

Button button = (Button)findViewById(R.id.xxx);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
        startActivity(intent);
    }
});

xxx is id from your xml of your Button.

Intent intentobj=new Intent(FromActivity.this,ToActivity.class);
startActivity(intentobj);

or you can simply use

startActivity(new Intent(FromActivity.this,ToActivity.class)); 

For Kotlin (if you are in an activity)

    buttonToClick.setOnClickListener{     startActivity(this,YourDestinationActivity::class.java)
        }

If you are in a fragment

        buttonToClick.setOnClickListener{
            startActivity(requireActivity, YourDestinationActivity::class.java)
        }

You can navigate to the next screen using these code snippets:

Kotlin

startActivity(Intent(this, LoginActivity::class.java))

Java

startActivity(new Intent(this, LoginActivity.class))

Here's a reference: Android Developers - Starting another activity

In your method fun onCreate(savedInstanceState: Bundle?) add this.

your_btn_id.setOnClickListener{

            val intent = Intent(this, yourpagename::class.java)
            startActivity(intent)
        }

Until now if it doesn't work then, check if these two files are added or not,

import android.content.Intent
import kotlinx.android.synthetic.main.activity_otp.*

Try this code:

 Button my_btn;
 my_btn = findViewById(R.id.submit_btn);
 my_btn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         setContentView(R.layout.activity_2);
     }
 });
Button navigate;
navigate = findViewById(R.id.button);
navigate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Navigate another Activity",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });

Just go to XML file and add Onclick = "opennewactivity" in button xml.

Then go to java code and create class opennewactivity you can just make it my clciking alt+Enter in xml code's "opennewactivity". in that just write

Intent intent = new Intent(this, newacivity.class);
startActivity(intent);

and if you want user to not get back to first activity again then just write this

Intent intent = new Intent(this, newactivity.class);
startActivity(intent);
finish();
 Cartoon_card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        
        {
            newActivity();
            
        }
    });
    
}
public void newActivity()
{
    Intent selectClass= new Intent(getApplicationContext(), com.example.fyp.videoplayer.class);
    startActivity(selectClass);
    
}
Related