In: Computer Science
Android Studio Code:
Provide a working android studio code i.e java and xml code for the activity below:
Develop an application that is capable to turn pages back and forth.
Detailed Instructions:
For the main activity, create a layout that represents a fictitious title and author. The activity should include a clickable button on the bottom right that allows you to go forward to the next activity. The next activity will simply be an image above simple text that can say anything.
Once in the next activity, below the text there should be two clickable buttons. On the left, a button that will take us back to our original title page. The right button does not need to go anywhere.
Java Code
Adding a new activity to the project
Next, we need to create the activity ( the screen ) where the
button will take us.
Menu -> File -> New -> Activity -> Empty
Activity.
Another method
App -> Java -> right click on packagename.projectname ->
New -> Activity -> Empty Activity
package com.example.intentbasic;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Moveto(View view) {
Intent i = new Intent(MainActivity.this,
Main2Activity.class);
MainActivity.this.startActivity(i);
}
}
//Second activity
package com.example.intentbasic;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
public void BackHome(View view){
Intent i2 = new Intent(SecondActivity.this,
MainActivity.class);
SecondActivity.this.startActivity(i2);
}
}
//xml code
<?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.example.intentbasic.FirstActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Author Name"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="179dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="189dp"
android:layout_marginLeft="18dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="18dp"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="35dp"
android:onClick="Moveto"
android:text=">>"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>
//xml code of second activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:id="@+id/myLayout">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<"
android:onClick="BackHome"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">>"
android:onClick="Movefwd"/>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp"
android:contentDescription="@string/imgDesc"
android:src="@drawable/image1"/>
</LinearLayout>