How To Create Pages Of An App Using Android Studio
This article shows how to create an android application to move from one activity to another.
Below are the steps for Creating a Simple Android Application to move from one activity to another activity.
Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
STEP-1: Create new project and your project screen looks like given below.
STEP-2: You will have xml and activity java file, path of both file given below.
STEP-3: Open your xml file and add the Button because after clicking this button we will move to second activity as shown below. Add TextView for Message. Assign ID to Button and TextView .
STEP-4: Now we have to create another activity (SecondActivity) to move from one activity to another. Create second activity and go to the android project > File >new > Activity > Empty Activity
STEP-5: Now open your second xml file, path of this file is same as first xml file. Add TextView for messages and add 2 Button one is for next activity and second for previous activity. assign ID to Textview and both Button. Second Activity is shown below:
STEP-6: Now, we have to create third activity same as second activity and path of this file is also same as another.("Now, you can create many activity like this") Here we add TextView for message and one Button for goto previous activity. It will be shown below
STEP-7: Now, open up the your first activity java file. define the Button (next_button or can be previous_button) and TextView variable, use findViewById() to get the Button and TextView.
STEP-8: We need to add the click listener to the all buttons (next_button or can be previous_button).
STEP-9: When the button has been clicked inside the onclicklistener method, create an Intent to start an activity called another activity.
STEP-10: Repeat step 7, 8, 9 for every activity.
STEP-11: Now run the app and click the button you can go to second activity.
In First Activity here only one Button and TextView
Complete code of OneActivity.java or activity_oneactivity.xml of Firstactivity is below.
Filename: activity_oneactivity.xml
XML
<? xml version = "1.0" encoding = "utf-8" ?>
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".Oneactivity"
tools:layout_editor_absoluteY = "81dp" >
< TextView
android:id = "@+id/question1_id"
android:layout_marginTop = "60dp"
android:layout_marginLeft = "30dp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = ""
android:textStyle = "bold"
android:textColor = "@android:color/background_dark"
/>
< Button
android:id = "@+id/first_activity_button"
android:layout_width = "150dp"
android:layout_height = "40dp"
android:layout_marginTop = "200dp"
android:layout_marginLeft = "90dp"
android:text = "Next"
android:textStyle = "bold"
android:textColor = "@android:color/background_dark"
android:textSize = "15sp" />
</ RelativeLayout >
Filename: OneActivity.java
Java
package org.geeksforgeeks.navedmalik.multiplescreenapp;
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.TextView;
public class Oneactivity extends AppCompatActivity {
TextView question1;
Button next_Activity_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_oneactivity);
next_Activity_button = (Button)findViewById(R.id.first_activity_button);
question1 = (TextView)findViewById(R.id.question1_id);
question1.setText( "Q 1 - How to pass the data between activities in Android?\n"
+ "\n"
+ "Ans- Intent" );
next_Activity_button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(Oneactivity. this , SecondActivity. class );
startActivity(intent);
}
});
}
}
Note: Here we will add Next Button and previous Button and textView for the message.
Complete code of SecondActivity.java or activity_second.xml of Second Activity is below.
Filename: activity_second.xml
XML
<? xml version = "1.0" encoding = "utf-8" ?>
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "org.geeksforgeeks.navedmalik.multiplescreenapp.SecondActivity" >
< TextView
android:id = "@+id/question2_id"
android:layout_marginTop = "60dp"
android:layout_marginLeft = "30dp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = ""
android:textStyle = "bold"
android:textColor = "@android:color/background_dark"
/>
< Button
android:id = "@+id/second_activity_next_button"
android:layout_width = "90dp"
android:layout_height = "40dp"
android:layout_marginTop = "200dp"
android:layout_marginLeft = "200dp"
android:text = "Next"
android:textStyle = "bold"
android:textColor = "@android:color/background_dark"
android:textSize = "15sp" />
< Button
android:id = "@+id/second_activity_previous_button"
android:layout_width = "110dp"
android:layout_height = "40dp"
android:layout_marginTop = "200dp"
android:layout_marginLeft = "50dp"
android:text = "previous"
android:textStyle = "bold"
android:textColor = "@android:color/background_dark"
android:textSize = "15sp" />
</ RelativeLayout >
Filename:SecondActivity.java
Java
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView question2;
Button next_button, previous_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
next_button = (Button)findViewById(R.id.second_activity_next_button);
previous_button = (Button)findViewById(R.id.second_activity_previous_button);
question2 = (TextView)findViewById(R.id.question2_id);
question2.setText( "Q 2 - What is ADB in android?\n"
+ "\n"
+ "Ans- Android Debug Bridge" );
next_button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(SecondActivity. this , ThirdActivity. class );
startActivity(intent);
}
});
previous_button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(SecondActivity. this , Oneactivity. class );
startActivity(intent);
}
});
}
}
Note: Here we add only Next Button and textView for message.
Complete code of ThirdActivity.java or activity_third.xml of Third Activity is below.
Filename: activity_third.xml
XML
<? xml version = "1.0" encoding = "utf-8" ?>
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "org.geeksforgeeks.navedmalik.multiplescreenapp.ThirdActivity" >
< TextView
android:id = "@+id/question3_id"
android:layout_marginTop = "60dp"
android:layout_marginLeft = "30dp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textStyle = "bold"
android:textColor = "@android:color/background_dark"
/>
< Button
android:id = "@+id/third_activity_previous_button"
android:layout_width = "110dp"
android:layout_height = "40dp"
android:layout_marginTop = "200dp"
android:layout_marginLeft = "100dp"
android:text = "previous"
android:textStyle = "bold"
android:textColor = "@android:color/background_dark"
android:textSize = "15sp" />
</ RelativeLayout >
Filename: ThirdActivity.java
Java
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
public class ThirdActivity extends AppCompatActivity {
TextView question3;
Button previous_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
previous_button = (Button)findViewById(R.id.third_activity_previous_button);
question3 = (TextView)findViewById(R.id.question3_id);
question3.setText( "Q 3 - How to store heavy structured data in android?\n"
+ "\n"
+ "Ans- SQlite database" );
previous_button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(ThirdActivity. this , SecondActivity. class );
startActivity(intent);
}
});
}
}
Output:
How To Create Pages Of An App Using Android Studio
Source: https://www.geeksforgeeks.org/android-creating-multiple-screen-app/
Posted by: landersseentrusted83.blogspot.com

0 Response to "How To Create Pages Of An App Using Android Studio"
Post a Comment