Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect two activities

Tags:

android

I have made one screen with two images and I would like to add a button lower on the page which will navigate to a second page when I click it. Do you know how to code this? I know how to create a button but I don't know how to connect the two screens!

like image 791
menu_on_top Avatar asked Feb 26 '23 17:02

menu_on_top


2 Answers

This task is accomplished with the startActivity(); method using Intents.

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

In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.

Make sure that you add your second Activity to the manifest also (it resides in the tag)!

<activity android:name=".ToActivity"
          android:label="@string/app_name">
</activity>
like image 189
Knossos Avatar answered Feb 28 '23 07:02

Knossos


To sum it up:

ImageView myImage = (ImageView) findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
        @Override
        onClick(View v) {
            Intent intent = new Intent(FromActivity.this, ToActivity.class);
            startActivity(intent);
        }
    }
);
like image 38
LambergaR Avatar answered Feb 28 '23 07:02

LambergaR