package com.quiz;
import java.util.Calendar;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class QuizSettingsActivity extends QuizActivity {
SharedPreferences mGameSettings;
static final int DATE_DIALOG_ID = 0;
static final int PASSWORD_DIALOG_ID = 1;
private int mYear;
private int mMonth;
private int mDay;
private TextView dob;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Spinner spinner = (Spinner) findViewById(R.id.Spinner_Gender);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final EditText nick_name = (EditText) findViewById(R.id.nick_nameET);
TextView dob = (TextView)findViewById(R.id.dobstatusTV);
EditText email = (EditText) findViewById(R.id.emailET);
Button pwd_btn = (Button) findViewById(R.id.pwd);
Button dob_btn = (Button) findViewById(R.id.dob);
String strNickname = nick_name.getText().toString();
String strEmail = email.getText().toString();
/*Editor editor = mGameSettings.edit();
editor.putString("GAME_PREFERENCES_NICKNAME", strNickname);
editor.putString("GAME_PREFERENCES_EMAIL", strEmail);
editor.commit();*/
nick_name.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String strNicknameToSave = nick_name.getText().toString();
Editor editor = mGameSettings.edit();
editor.putString("GAME_PREFERENCES_NICKNAME", strNicknameToSave);
editor.commit();
return true;
}
return false;
}
});
pwd_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(PASSWORD_DIALOG_ID);
}
});
dob_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDisplay();
}
private void updateDisplay() {
TextView dob = (TextView)findViewById(R.id.dobstatusTV);
dob.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
case PASSWORD_DIALOG_ID:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.password_dialog,
(ViewGroup) findViewById(R.id.root));
final EditText p1 = (EditText) layout
.findViewById(R.id.EditText_Pwd1);
final EditText p2 = (EditText) layout
.findViewById(R.id.EditText_Pwd2);
final TextView error = (TextView) layout
.findViewById(R.id.TextView_PwdProblem);
p2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String strPass1 = p1.getText().toString();
String strPass2 = p2.getText().toString();
if (strPass1.equals(strPass2)) {
error.setText("pwd_equal");
} else {
error.setText("pwd_not_equal");
}
}
// ... other required overrides need not be implemented
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
// Now configure the AlertDialog
builder.setTitle("pwd setting button");
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// We forcefully dismiss and remove the Dialog, so
// it
// cannot be used again (no cached info)
QuizSettingsActivity.this
.removeDialog(PASSWORD_DIALOG_ID);
}
});
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView passwordInfo = (TextView) findViewById(R.id.pwd_status_TV);
String strPassword1 = p1.getText().toString();
String strPassword2 = p2.getText().toString();
if (strPassword1.equals(strPassword2)) {
/*Editor editor = mGameSettings.edit();
editor.putString("GAME_PREFERENCES_PASSWORD",
strPassword1);
editor.commit();*/
passwordInfo.setText("pwd set");
} else {
//Log.d(DEBUG_TAG,
// "Passwords do not match. Not saving. Keeping old password (if set).");
}
// We forcefully dismiss and remove the Dialog, so
// it
// cannot be used again
//passwordInfo.setText("pwd not set");
QuizSettingsActivity.this
.removeDialog(PASSWORD_DIALOG_ID);
}
});
// Create the AlertDialog and return it
AlertDialog passwordDialog = builder.create();
return passwordDialog;
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
// Handle any DatePickerDialog initialization here
/* DatePickerDialog dateDialog = (DatePickerDialog) dialog;
int iDay,
iMonth,
iYear;
// Check for date of birth preference
if (mGameSettings.contains("GAME_PREFERENCES_DOB")) {
// Retrieve Birth date setting from preferences
long msBirthDate = mGameSettings.getLong("GAME_PREFERENCES_DOB",
0);
Time dateOfBirth = new Time();
dateOfBirth.set(msBirthDate);
iDay = dateOfBirth.monthDay;
iMonth = dateOfBirth.month;
iYear = dateOfBirth.year;
} else {
Calendar cal = Calendar.getInstance();
// Today's date fields
iDay = cal.get(Calendar.DAY_OF_MONTH);
iMonth = cal.get(Calendar.MONTH);
iYear = cal.get(Calendar.YEAR);
}
// Set the date in the DatePicker to the date of birth OR to the
// current date
dateDialog.updateDate(iYear, iMonth, iDay);
return;
case PASSWORD_DIALOG_ID:
// Handle any Password Dialog initialization here
// Since we don't want to show old password dialogs, just set new
// ones, we need not do anything here
// Because we are not "reusing" password dialogs once they have
// finished, but removing them from
// the Activity Dialog pool explicitly with removeDialog() and
// recreating them as needed.*/
return;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/a" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/menu"
android:textColor="@color/white" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/b" />
</RelativeLayout>
<ScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/nick_nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nick Name" />
<EditText
android:id="@+id/nick_nameET"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/emailTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
<EditText
android:id="@+id/emailET"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/passwordTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/pwd_status_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password Status" />
</LinearLayout>
<TextView
android:id="@+id/dobTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOB" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/dobstatusTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOB Status" />
</LinearLayout>
<TextView
android:id="@+id/genderTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender" />
<Spinner
android:id="@+id/Spinner_Gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/planet_prompt"
>
</Spinner>
</LinearLayout>
</ScrollView>
</LinearLayout>