Implementing an About Box
When the user select the About button (touch or navigate with D-pad or traceball and press the selection button), we want to pop up a window with some information. After scrolling through the text, the user can press the Back button to dismiss the window.
We can accomplish this in several ways,
1.Define a new Activity, and start it
2. Use the AlertDialog class, and show it
3. Subclass Android`s Dialog class, and show that
For this application, let`s define a new activity. Like the main Sudoku activity.
STEP 1. : Now we need About activity layout file. We will name it res/layout/about.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<ScrollView
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”10dip” >
<TextView
android:id=”@+id/about_content”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/about_text”/>
</ScrollView>
We need only one version of layout, because this will work both portrait and landscape modes.
STEP 2 : Add strings for the title of the About dialog box and the text it contains to res/values/strings.xml
<string name=”about_title”>About Android Sudoku</string>
<string name=”about_text”> Sudoku is a logic-based number placement puzzle. Starting with a partially completed 9×9 grid, the objective is to fill the grid so that each row, each column, and each of the 3×3 boxes (also called <i>blocks</i>) contains the digits 1 to 9 exactly once.
</string>
– is used to prevents an extra blank from appearing before the first word.
STEP : 3 Define the About activity at About.java . Create new java class at /src/androidsl.raaz.sudoku/Activity.java
package androidsl.raaz.sudoku;
import android.app.Activity;
import android.os.Bundle;
public class About extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
STEP 4 : Wire all this up to the About button in the SudokuQuiz class /src/androidsl.raaz.sudoku/SudokuQuizActivity.java
1. Add few imports
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
2. In the onCreate( ) method, add code to call findViewById( ) to look up an Android view given its resource ID, and add code to call setOnClickListener() to tell Android which object to tickle when the user touches or clicks the view:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
3. The setOnClickListener( ) method needs to be passed an object that implements the OnClickListener Java interface. We’re passing it the this variable, so we had better make the current class (Sudoku) implement that interface, or we’ll get a compiler error. OnClickListener has one method in it called onClick( ), so we have to add that method to our class as well. [If you’re a Java expert, you may be wondering why we didn’t use an anonymous inner class to handle the clicks. You could, but according to the Android developers, every new inner class takes up an extra 1KB of memory.]
/src/androidsl.raaz.sudoku/SudokuQuizActivity.java
public class Sudoku extends Activity implements OnClickListener {
// …
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
// More buttons go here (if any) …
}
}
}
To start an activity in Android, we first need to create an instance of the Intent class. There are two kinds of intents:
(a) public (named) intents that are registered with the system and can be called from any application
(b) private (anonymous) intents that are used within a single application.
For here when we run this program and select About button this will get an error.
Solution : We forgot one important step: every activity needs to be declared in AndroidManifest.xml. To do that, double-click the file to open it, switch to XML mode if necessary by selecting the AndroidManifest.xml tab at the bottom, and add a new <activity> tag after the closing tag of the first one
<activity android:name=”.About” android:label=”@string/about_title” >
</activity>
Now run again and press About button, this will give the following screen.