package com.jamesfroggatt.geoquiz.app; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class QuizActivity extends Activity { private Button mTrueButton; private Button mFalseButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView INFLATES a layout and puts it on the screen // each widget in the layout view is instantiated as defined // by its attributes in activity_quiz.xml // R.layout.activity_quiz refers to the generated R.java class setContentView(R.layout.activity_quiz); // we can now get a reference to an inflated widget by calling // the findViewById method. This method accepts a resource ID of // a widget and returns a View object mTrueButton = (Button)findViewById(R.id.true_button); mFalseButton = (Button)findViewById(R.id.false_button); // set up onClickListener for the True Button // the setOnClickListener(OnClickListener) method takes a // listener as an argument, it takes an object that implements // OnClickListener. This listener is implemented as an anonymous // inner class. mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do a toast Toast.makeText(QuizActivity.this, R.string.incorrect_toast,Toast.LENGTH_SHORT).show(); } }); // set listener for the mFalseButton mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do a toast Toast.makeText(QuizActivity.this, R.string.correct_toast,Toast.LENGTH_SHORT).show(); } }); // A note on Context: to make Toast (above), we call the following method // from the Toast class // public static Toast makeText(Context context, int resId, int Duration) // The Context is typically an instance of Activity (Activity is the // subclass of Context). // After we have created toast, we call Toast.show() on it to get it on screen } /* @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_quiz, menu); return true; }*/ }
layout/activity_quiz.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <!-- "@string/question_text" we are referencing a string --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" android:text="@string/question_text" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- "@+id/true_button" we are CREATING the IDs hence the + --> <Button android:id="@+id/true_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button" /> <!-- "@+id/false_button" we are CREATING the IDs hence the + --> <Button android:id="@+id/false_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button" /> </LinearLayout> </LinearLayout>
values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">James</string> <string name="app_name">GeoQuiz</string> <string name="question_text">Constantinople is the largest city in Turkey.</string> <string name="true_button">True</string> <string name="false_button">False</string> <string name="menu_settings">Settings</string> <string name="correct_toast">Correct!</string> <string name="incorrect_toast">Incorrect!</string> </resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jamesfroggatt.geoquiz.app" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.jamesfroggatt.geoquiz.app.QuizActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>