Implementing the game activity layout

So far, we have successfully created the layout for main activity. Before we conclude this chapter, it is imperative we create the layout for GameActivity as well. Go ahead and open activity_game.xml and add the following code to it:

<?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.mydomain.tetris.GameActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
android:background="#e8e8e8">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="32dp"
android:paddingBottom="32dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/current_score"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_current_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin_top"
android:text="@string/high_score"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_high_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
<Button
android:id="@+id/btn_restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_restart"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9">

</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Most view attributes used in this layout have already previously been used and as such do not need further explanation. The only exceptions are the android:background and android:layout_weightSum attributes.

The android:background attribute is used to set the background color of a view or view group. #e8e8e8 and #000 were passed as values in the two instances where android:background is used in the layout. #e8e8e8 is the hex color code for gray and #000 the hex code for black. 

android:layout_weightSum defines the maximum weight sum in a view group and is calculated as the sum of the layout_weight values of all child views in a view group. The first linear layout in activity_game.xml declares the weight sum of all child views to be 10. As such, the immediate children of the linear layout have layout weights of 1 and 9, respectively.

We made use of three string resources that we have not previously added to our string resources file. Go ahead and add the following string resources to strings.xml:

<string name="high_score">High score</string>
<string name="current_score">Current score</string>
<string name="btn_restart">Restart</string>

Finally, we have to add some simple logic to game activity for the population of the high score and current score text views, as follows:

package com.mydomain.tetris

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
import com.mydomain.tetris.storage.AppPreferences

class GameActivity: AppCompatActivity() {

var tvHighScore: TextView? = null
var tvCurrentScore: TextView? = null
var appPreferences: AppPreferences? = null

public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game)
appPreferences = AppPreferences(this)

val btnRestart = findViewById<Button>(R.id.btn_restart)
tvHighScore = findViewById<TextView>(R.id.tv_high_score)
tvCurrentScore = findViewById<TextView>(R.id.tv_current_score)

updateHighScore()
updateCurrentScore()
}

private fun updateHighScore() {
tvHighScore?.text = "${appPreferences?.getHighScore()}"
}

private fun updateCurrentScore() {
tvCurrentScore?.text = "0"
}
}

In the preceding code snippet, object references to layout view elements are created. In addition, we declare the updateHighScore() and updateCurrentScore() functions. These two functions are invoked on the creation of the view. They set the default scores displayed in the current score and high score text views declared in the layout file.

Save the changes made to the project and build and run the application. Click on the New Game button once the application starts to view the layout we just created:

The right-hand side of the layout that contains no content is the area in which the Tetris game play will happen. We will implement this in chapter 3: Implementing Tetris Logic and Functionality. The final thing we must understand before moving to the next chapter is the app manifest.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.135.249