Android Open Source - AndroidWeatherBuoyDemo Settings Fragment






From Project

Back to project page AndroidWeatherBuoyDemo.

License

The source code is released under:

Apache License

If you think the Android project AndroidWeatherBuoyDemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kevinrschultz.weatherbuoy.ui;
//from w  ww  . j  av  a2s. c  om
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.kevinrschultz.weatherbuoy.R;
import com.kevinrschultz.weatherbuoy.model.UnitSystem;
import com.kevinrschultz.weatherbuoy.preferences.WeatherBuoyPreferences;

/**
 */
public class SettingsFragment extends Fragment implements RadioGroup.OnCheckedChangeListener{
    
    private WeatherBuoyPreferences preferences;

    private RadioGroup unitSystemRadioGroup;

    public static SettingsFragment newInstance() {
        return new SettingsFragment();
    }

    public SettingsFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.preferences = new WeatherBuoyPreferences(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_settings, container, false);
        unitSystemRadioGroup = (RadioGroup) v.findViewById(R.id.settings_unit_system);
        unitSystemRadioGroup.setOnCheckedChangeListener(this);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        setCurrentChecked(preferences.getUserUnitSystem());
    }

    public void setCurrentChecked(UnitSystem system) {
        switch(system) {
            case IMPERIAL:
                ((RadioButton) getView().findViewById(R.id.settings_radio_imperial)).setChecked(true);
                break;
            case METRIC:
                ((RadioButton) getView().findViewById(R.id.settings_radio_metric)).setChecked(true);
                break;
            case NAUTICAL:
                ((RadioButton) getView().findViewById(R.id.settings_radio_nautical)).setChecked(true);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.settings_unit_system:
                updateUnitSystemPreference(checkedId);
                break;
        }
    }

    private void updateUnitSystemPreference(int checkedId) {
        switch(checkedId) {
            case R.id.settings_radio_imperial:
                preferences.setUserUnitSystem(UnitSystem.IMPERIAL);
                break;
            case R.id.settings_radio_metric:
                preferences.setUserUnitSystem(UnitSystem.METRIC);
                break;
            case R.id.settings_radio_nautical:
                preferences.setUserUnitSystem(UnitSystem.NAUTICAL);
                break;
        }
    }

    /**
     * For test dependency injection
     */
    private void setPreferences(WeatherBuoyPreferences preferences) {
        this.preferences = preferences;
    }
}




Java Source Code List

com.kevinrschultz.weatherbuoy.Constants.java
com.kevinrschultz.weatherbuoy.customviews.compass.CompassViewTest.java
com.kevinrschultz.weatherbuoy.customviews.compass.CompassView.java
com.kevinrschultz.weatherbuoy.customviews.compass.Compass.java
com.kevinrschultz.weatherbuoy.data.FakeBuoyListingGenerator.java
com.kevinrschultz.weatherbuoy.json.GsonSingleton.java
com.kevinrschultz.weatherbuoy.model.Advisory.java
com.kevinrschultz.weatherbuoy.model.BuoyDescription.java
com.kevinrschultz.weatherbuoy.model.Region.java
com.kevinrschultz.weatherbuoy.model.UnitSystem.java
com.kevinrschultz.weatherbuoy.model.WaveCondition.java
com.kevinrschultz.weatherbuoy.model.WindCondition.java
com.kevinrschultz.weatherbuoy.preferences.WeatherBuoyPreferences.java
com.kevinrschultz.weatherbuoy.sandbox.ActivityLaunchingListItem.java
com.kevinrschultz.weatherbuoy.sandbox.CompassViewActivity.java
com.kevinrschultz.weatherbuoy.sandbox.MainActivity.java
com.kevinrschultz.weatherbuoy.ui.BaseActivity.java
com.kevinrschultz.weatherbuoy.ui.BaseArrayAdapter.java
com.kevinrschultz.weatherbuoy.ui.BuoyDescriptionAdapter.java
com.kevinrschultz.weatherbuoy.ui.BuoyDetailActivity.java
com.kevinrschultz.weatherbuoy.ui.BuoyDetailFragment.java
com.kevinrschultz.weatherbuoy.ui.BuoyDetailViewModel.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingActivity.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingFragment.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingPresenter.java
com.kevinrschultz.weatherbuoy.ui.BuoyListingView.java
com.kevinrschultz.weatherbuoy.ui.SettingsActivity.java
com.kevinrschultz.weatherbuoy.ui.SettingsFragment.java
com.kevinrschultz.weatherbuoy.util.UnitConverter.java
com.kevinrschultz.weatherbuoy.views.AdvisoryBannerView.java
com.kevinrschultz.weatherbuoy.views.InstrumentView.java
com.kevinrschultz.weatherbuoy.views.Instrument.java
com.kevinrschultz.weatherbuoy.views.OptionalTextView.java