Menu and Settings Activity

Menu

 
Adding menu buttons:
  • Add an xml file in res/menu/ that defines the buttons you are adding, their ordering and any other characteristics.
  • If the menu buttons are associated with a fragment, make sure to call setHasOptionsMenu(true) in the fragment's onCreate method.
  • Inflate the menu in the onCreateOptionsMenu with a line inflater.inflate(R.menu.forecastfragment, menu);
  • In onOptionsItemSelected you can check which item was selected and react appropriately. In the case of Refresh, this means creating and executing a FetchWeatherTask.
 

Share Menu in a Fragment

 Fragment {
    onCreate()                  { setHasOptionsMenu(); }
    onCreateOptionsMenu()       {
                                    inflate(menu.fragment);
                                    shareItem = menu.findItem(R.id.action_share);
                                    mShareActionProvider = MenuItemCompat.getActionProvider(menu.findItem(R.id.action_share));
                                    mShareActionProvider.setShareIntent(createShareForecastIntent());
                                }
    createShareForecastIntent() {
                                    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                                    shareIntent.setType("text/plain");
                                    shareIntent.putExtra(Intent.EXTRA_TEXT, shared_text_info);
                                }

    menu/detailfragment.xml:
    <item
        android:id="@+id/action_share"
        android:title="@string/action_share"
        support:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        support:showAsAction="always"
    />
}


menu
    main (.MainActivity):       Settings, Show Map
        forecastfragment:       Refresh
    detail (.DetailActivity)    Settings
        detailfragment          Share
 
 

Settings Activity

 
Android Studio => New => Activity => Settings Activity


Delete/edit examples to keep minimum settings xml/pref_general.xml, headers, and, strings_activity_settings containing key, values, defaults.




    SettingsActivity extends PreferenceActivity {
        onCreate()                  { // xml/pref_general; this ISN'T in menu/*.xml!
                                      addPreferencesFromResource(pref_general);
                                      setOnPreferenceChangeListener();
                                    }
        onPreferenceChange()        { setSummary(); }
    }

     MainActivity {
        onOptionsItemSelected()     { R.id.action_settings: startActivity(new Intent(this, SettingsActivity.class)); }

        // Apply new settings as app resume (just before becoming active) after being Stopped for showing settings:
        protected void onResume() {
            super.onResume();
            String location = Utility.getPreferredLocation( this );
            if (!location.equals(mLocation)) {
                ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentByTag(FORECASTFRAGMENT_TAG);
                ff.onLocationChanged();
                mLocation = location;
            }
        }
    }


    // Fetch value of currently selected preference settings:
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String prefTempUnit = sharedPref.getString(getString(R.string.pref_temp_unit_key),
                getString(R.string.pref_temp_unit_metric_value)); // "Metric"
        Boolean prefShowNotifications = sharedPref.getBoolean(context.getString(R.string.pref_show_notifications_key),
                Boolean.parseBoolean(context.getString(R.string.pref_show_notifications_default)));

    <ListPreference
        android:key="@string/pref_temp_unit_key"
        android:title="@string/pref_temp_unit_label"
        android:dialogTitle="@string/pref_temp_unit_label"
        android:defaultValue="@string/pref_temp_unit_metric_value"
        android:entries="@array/pref_temp_unit_entries"
        android:entryValues="@array/pref_temp_unit_entry_values" />
    <CheckBoxPreference
        android:key="@string/pref_enable_notifications_key"
        android:title="@string/pref_enable_notifications_label"
        android:defaultValue="@string/pref_enable_notifications_default"
        android:summaryOn="@string/pref_enable_notifications_summary_on"
        android:summaryOff="@string/pref_enable_notifications_summary_off" />

  strings.xml
    <string name="pref_temp_unit_key" translatable="false">temp_unit</string>
    <string name="pref_temp_unit_label">Pick Your Units</string>
    <string name="pref_temp_unit_metric_label">Metric</string>
    <string name="pref_temp_unit_imperial_label">Imperial</string>
    <string name="pref_temp_unit_metric_value" translatable="false">metric</string>
    <string name="pref_temp_unit_imperial_value" translatable="false">imperial</string>
    <string name="pref_enable_notifications_key" translatable="false">show_notifications_key</string>
    <string name="pref_enable_notifications_label">Show Notifications</string>
    <string name="pref_enable_notifications_default" translatable="false">false</string>
    <string name="pref_enable_notifications_summary_on">Enabled</string>
    <string name="pref_enable_notifications_summary_off">Disabled</string>

  array.xml
    <string-array name="pref_temp_unit_entries">
        <item>@string/pref_temp_unit_metric_label</item>
        <item>@string/pref_temp_unit_imperial_label</item>
    </string-array>
    <string-array name="pref_temp_unit_entry_values">
        <item>@string/pref_temp_unit_metric_value</item>
        <item>@string/pref_temp_unit_imperial_value</item>
    </string-array>
}



No comments:

Post a Comment