edu.csh.coursebrowser.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for edu.csh.coursebrowser.SettingsActivity.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Andrew Hanes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
package edu.csh.coursebrowser;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class SettingsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        this.setTitle("Settings");
        final SharedPreferences sp = SchoolActivity.sp;
        LinearLayout ll = (LinearLayout) this.findViewById(R.id.settings_layout);
        ll.setGravity(Gravity.CENTER);
        final TextView tv = new TextView(this);
        if (!sp.contains("quarter")) {
            SharedPreferences.Editor e = sp.edit();
            e.putString("quarter", "20122");
            e.commit();
        }
        tv.setText("Current Quarter: " + sp.getString("quarter", "20122"));
        Button b = new Button(this);
        b.setText("Change Quarter");
        ll.addView(tv);
        ll.addView(b);
        ll.setPadding(10, 10, 10, 10);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                AlertDialog.Builder changeQuarter = new AlertDialog.Builder(SettingsActivity.this);
                changeQuarter.setTitle("Change Quarter");
                changeQuarter.setMessage("Select New Quarter");
                final RadioButton ld = new RadioButton(SettingsActivity.this);
                ld.setText("20121");
                ld.setId(1);
                final RadioButton s = new RadioButton(SettingsActivity.this);
                s.setText("20122 (Current)");
                s.setId(2);
                final RadioButton d = new RadioButton(SettingsActivity.this);
                d.setText("20123");
                d.setId(3);
                final RadioGroup rg = new RadioGroup(SettingsActivity.this);
                rg.addView(ld);
                rg.addView(s);
                rg.addView(d);
                changeQuarter.setView(rg);

                changeQuarter.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }

                });
                changeQuarter.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String q;
                        int n = rg.getCheckedRadioButtonId();
                        if (n == 1)
                            q = "20121";
                        else if (n == 2)
                            q = "20122";
                        else
                            q = "20123";
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putString("quarter", q);
                        edit.commit();
                        SettingsActivity.this.finish();
                    }
                });

                changeQuarter.show();
            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_settings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation
            // .html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}