Last active
December 31, 2016 21:06
-
-
Save milon87/ab11dcb5b68d12b1ddb6d3899421dc9f to your computer and use it in GitHub Desktop.
Preference Activity with appcompact
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AppCompatPreferenceActivity extends PreferenceActivity { | |
private AppCompatDelegate mDelegate; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
getDelegate().installViewFactory(); | |
getDelegate().onCreate(savedInstanceState); | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
protected void onPostCreate(Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
getDelegate().onPostCreate(savedInstanceState); | |
} | |
public ActionBar getSupportActionBar() { | |
return getDelegate().getSupportActionBar(); | |
} | |
public void setSupportActionBar(@Nullable Toolbar toolbar) { | |
getDelegate().setSupportActionBar(toolbar); | |
} | |
@Override | |
public MenuInflater getMenuInflater() { | |
return getDelegate().getMenuInflater(); | |
} | |
@Override | |
public void setContentView(@LayoutRes int layoutResID) { | |
getDelegate().setContentView(layoutResID); | |
} | |
@Override | |
public void setContentView(View view) { | |
getDelegate().setContentView(view); | |
} | |
@Override | |
public void setContentView(View view, ViewGroup.LayoutParams params) { | |
getDelegate().setContentView(view, params); | |
} | |
@Override | |
public void addContentView(View view, ViewGroup.LayoutParams params) { | |
getDelegate().addContentView(view, params); | |
} | |
@Override | |
protected void onPostResume() { | |
super.onPostResume(); | |
getDelegate().onPostResume(); | |
} | |
@Override | |
protected void onTitleChanged(CharSequence title, int color) { | |
super.onTitleChanged(title, color); | |
getDelegate().setTitle(title); | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
getDelegate().onConfigurationChanged(newConfig); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
getDelegate().onStop(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
getDelegate().onDestroy(); | |
} | |
public void invalidateOptionsMenu() { | |
getDelegate().invalidateOptionsMenu(); | |
} | |
private AppCompatDelegate getDelegate() { | |
if (mDelegate == null) { | |
mDelegate = AppCompatDelegate.create(this, null); | |
} | |
return mDelegate; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | |
<SwitchPreference | |
android:defaultValue="true" | |
android:key="example_switch" | |
android:summary="@string/pref_description_social_recommendations" | |
android:title="@string/pref_title_social_recommendations" /> | |
<EditTextPreference | |
android:capitalize="words" | |
android:defaultValue="@string/pref_default_display_name" | |
android:inputType="textCapWords" | |
android:key="example_text" | |
android:maxLines="1" | |
android:selectAllOnFocus="true" | |
android:singleLine="true" | |
android:title="@string/pref_title_display_name" /> | |
<ListPreference | |
android:defaultValue="-1" | |
android:entries="@array/pref_example_list_titles" | |
android:entryValues="@array/pref_example_list_values" | |
android:key="example_list" | |
android:negativeButtonText="@null" | |
android:positiveButtonText="@null" | |
android:title="@string/pref_title_add_friends_to_messages" /> | |
</PreferenceScreen> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SettingActivity extends AppCompatPreferenceActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setupActionBar(); | |
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit(); | |
} | |
private void setupActionBar() { | |
ActionBar actionBar = getSupportActionBar(); | |
if (actionBar != null) { | |
// Show the Up button in the action bar. | |
actionBar.setDisplayHomeAsUpEnabled(true); | |
//actionBar.setDisplayHomeAsUpEnabled(); | |
} | |
} | |
public static class PrefsFragment extends PreferenceFragment { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Load the preferences from an XML resource | |
addPreferencesFromResource(R.xml.pref_general); | |
} | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
onBackPressed(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment