Back to project page android-component-location.
The source code is released under:
MIT License
If you think the Android project android-component-location listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yingchn.android.fragment; /*from ww w .j a v a 2 s . c o m*/ import android.annotation.SuppressLint; import android.app.Dialog; import android.os.Build; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceScreen; import android.support.v4.preference.PreferenceFragment; import android.support.v7.app.ActionBarActivity; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewParent; import android.widget.FrameLayout; import android.widget.LinearLayout; import com.yingchn.android.location.R; public class SettingFragment extends PreferenceFragment { @Override public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences_setting); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ((ActionBarActivity) getActivity()).getSupportActionBar() .setDisplayHomeAsUpEnabled(true); ((ActionBarActivity) getActivity()).getSupportActionBar().setTitle( "Setting"); } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { super.onPreferenceTreeClick(preferenceScreen, preference); // If the user has clicked on a preference screen, set up the action bar if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB && preference instanceof PreferenceScreen) { initializeActionBar((PreferenceScreen) preference); } return false; } /** Sets up the action bar for an {@link PreferenceScreen} */ @SuppressLint("NewApi") public static void initializeActionBar(PreferenceScreen preferenceScreen) { final Dialog dialog = preferenceScreen.getDialog(); if (dialog != null) { // Inialize the action bar dialog.getActionBar().setDisplayHomeAsUpEnabled(true); // Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow // events instead of passing to the activity // Related Issue: https://code.google.com/p/android/issues/detail?id=4611 View homeBtn = dialog.findViewById(android.R.id.home); if (homeBtn != null) { OnClickListener dismissDialogClickListener = new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }; // Prepare yourselves for some hacky programming ViewParent homeBtnContainer = homeBtn.getParent(); // The home button is an ImageView inside a FrameLayout if (homeBtnContainer instanceof FrameLayout) { ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent(); if (containerParent instanceof LinearLayout) { // This view also contains the title text, set the whole view as clickable ((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener); } else { // Just set it on the home button ((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener); } } else { // The 'If all else fails' default case homeBtn.setOnClickListener(dismissDialogClickListener); } } } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: getActivity().finish(); return true; default: return super.onOptionsItemSelected(item); } } }