askIT

Android Moving from one activity to another activity in Android

media-floppy.png Download android_moving_from_one_activity_to_another_activity_in_android.zip

So you have two diffrent classes in Android and you want to navigate from one class to another.

The classes that enables you to navigate between them are called Activities. This is because they extend the Android class AppCompatActivity.

activity_main.png the_three_billy_goats_gruff.png

res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nettport.fairytales.fairytales.MainActivity">

    <Button
        android:id="@+id/buttonTheThreeBillyGoatsGruff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="The Three Billy Goats Gruff"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="16dp" />

</android.support.constraint.ConstraintLayout>

res\layout\the_three_billy_goats_gruff.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="The Three Billy Goats Gruff" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Once upon a time there were three billy goats, who were to go up to the hillside to make themselves fat, and the name of all three was Gruff." />

    <Button
        android:id="@+id/buttonHome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Home" />
</LinearLayout>

manifest\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nettport.fairytales.fairytales">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TheThreeBillyGoatsGruff"></activity>
    </application>

</manifest>

java\com.nettport.fairytales.fairytales\MainActivity.class

package com.nettport.fairytales.fairytales;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

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


                /* SubmitButton listener */
                Button buttonTheThreeBillyGoatsGruff = (Button)findViewById(R.id.buttonTheThreeBillyGoatsGruff);
                buttonTheThreeBillyGoatsGruff.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                navigateToTheThreeBillyGoatsGruff();
                        }
                });

        }

        /*- buttonTheThreeBillyGoatsGruff pressed ------------------------- */
        public void navigateToTheThreeBillyGoatsGruff(){

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

}

java\com.nettport.fairytales.fairytales\TheThreeBillyGoatsGruff.class

package com.nettport.fairytales.fairytales;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

/**
* Created by bruker on 17.06.2017.
*/


public class TheThreeBillyGoatsGruff extends AppCompatActivity {

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


                /* SubmitButton listener */
                Button buttonHome = (Button)findViewById(R.id.buttonHome);
                buttonHome.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                navigateToHome();
                        }
                });
        }


        /* NavigateToHome ------------------------------------------------------- */
        public void navigateToHome(){
                Intent i = new Intent(TheThreeBillyGoatsGruff.this, MainActivity.class);
                startActivity(i);
        }

}

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.