Wednesday, December 10, 2014

Developing MyQuiz Android Apps Part 4


---
Developing MyQuiz Apps Part 4
Using ADT 18 On Win 7

CREATING HELP SCREEN

0) Continue from previous tutorial

Or download startup file

1) Edit Layout File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bckgrnd">
   
<TextView
            android:id="@+id/TextView01"
            android:layout_height="wrap_content"
            android:text="@string/help"
            android:textSize="40dp"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:layout_width="wrap_content"
            android:layout_gravity="fill_horizontal|center"
            android:shadowColor="@android:color/white"
            android:textColor="#f0f0f0">
</TextView>   
<TextView
        android:id="@+id/TextView_HelpText"
        android:textColor="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:autoLink="all"
        android:isScrollContainer="true"
        android:textStyle="italic"
        android:drawablePadding="5dp"
        android:textColorLink="#FFFF0F"
        android:linksClickable="true"
        android:fadingEdgeLength="25dp"
        android:fadingEdge="vertical"
        android:scrollbars="vertical"
        android:padding="20dp"
        android:textSize="7pt"
        android:scrollbarStyle="outsideOverlay"
        android:bufferType="spannable"></TextView>
</LinearLayout>
File name: MyQuiz/res/layout/help.xml

2) Edit Java File

package com.example.myquiz;
import android.app.Activity;
public class QuizActivity extends Activity {
    public static final String GAME_PREFERENCES = "GamePrefs";
   
    public static final String DEBUG_TAG = "MYQUIZ";
}
File name: QuizActivity.java
package com.example.myquiz;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class QuizHelpActivity extends QuizActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
       
       
        // Read raw file into string and populate TextView
        InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
        try {
            TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
            String strFile = inputStreamToString(iFile);
            helpText.setText(strFile);
        } catch (Exception e) {
            Log.e(DEBUG_TAG"InputStreamToString failure", e);
        }
    }
   
    /**
     * Converts an input stream to a string
     *
     * @param is
     *            The {@code InputStream} object to read from
     * @return A {@code String} object representing the string for of the input
     * @throws IOException
     *             Thrown on read failure from the input
     */
    public String inputStreamToString(InputStream is) throws IOException {
        StringBuffer sBuffer = new StringBuffer();
        DataInputStream dataIO = new DataInputStream(is);
        String strLine = null;
        while ((strLine = dataIO.readLine()) != null) {
            sBuffer.append(strLine + "\n");
        }
        dataIO.close();
        is.close();
        return sBuffer.toString();
    }
}
File name: QuizHelpActivity.java

3) Create Raw Text File

3.1) Add a new folder raw to res folder.
3.2) Add a text file
MyQuiz is a quiz app dedicated to those who love travel and adventure.
By answering a series of Yes or No questions, you gain points toward being the most well-traveled and field-tested person on the planet. Share your experiences and compete with your friends.
MyQuiz was developed by Me.
Phone: 012-3456789
Email: support@me.com
Website: http://www.me.com
Address: 1060 Beach Street, Penang Island.
COPYRIGHT (c) 2014, ME.
All rights reserved.
File name: res/raw/quizhelp.txt

4) Run

REFERENCE

DOWNLOAD

---

Tuesday, December 9, 2014

Developing MyQuiz Android Apps Part 3


---
Developing MyQuiz Apps Part 3
Using ADT 18 On Win 7

CREATING LISTVIEW MENU

0) Continue from previous tutorial

or download startup file

1) Prepare the string values to be used in this App.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string
        name="app_name">MyQuiz</string>
    <string
        name="help">HELP</string>
    <string
        name="settings">SETTINGS</string>
    <string
        name="game">GAME</string>
    <string
        name="scores">SCORES</string>
    <string
        name="menu">MENU</string>
    <string
        name="menu_item_settings">Settings</string>
    <string
        name="menu_item_play">Play Game</string>
    <string
        name="menu_item_scores">View Scores</string>
    <string
        name="menu_item_help">Help</string>
</resources>
File name: MyQuiz/res/values/strings.xml

2) Prepare Layout files to be used in this App

2.1) The menu item file

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:shadowRadius="5"
    android:gravity="center"
    android:shadowDy="3"
    android:shadowDx="3"
    android:textSize="32sp"
/>
File name: MyQuiz/res/layout/menu_item.xml

2.2) The menu file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bckgrnd">
   
        <ListView
        android:layout_height="wrap_content"
        android:id="@+id/ListView_Menu"
        android:layout_width="match_parent"
        android:layout_centerHorizontal="true">
        </ListView>
</LinearLayout>

3) Edit QuizMenuActivity.java

Replace the content of QuizMenuActivity.java with the following codes.
package com.example.myquiz;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class QuizMenuActivity extends QuizActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
       
        ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
        String[] items = { getResources().getString(R.string.menu_item_play),
                getResources().getString(R.string.menu_item_scores),
                getResources().getString(R.string.menu_item_settings),
                getResources().getString(R.string.menu_item_help) };
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
        menuList.setAdapter(adapt);
       
        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
                // Note: if the list was built "by hand" the id could be used.
                // As-is, though, each item has the same id
                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();
                if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_play))) {
                    // Launch the Game Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_help))) {
                    // Launch the Help Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_settings))) {
                    // Launch the Settings Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_scores))) {
                    // Launch the Scores Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
                }
            }
        });
       
       
    }
   
}

4) Check that all Activity Class have been specified in the Android Manifest.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myquiz"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myquiz.QuizSplashActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="QuizMenuActivity"></activity>
        <activity
            android:name="QuizHelpActivity"></activity>
        <activity
            android:name="QuizScoresActivity"></activity>
        <activity
            android:name="QuizSettingsActivity"></activity>
        <activity
            android:name="QuizGameActivity"></activity>           
    </application>
</manifest>

5) Run.


REFERENCES

DOWNLOAD

---