Guest User

Untitled

a guest
May 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.88 KB | None | 0 0
  1. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  2. <PreferenceCategory
  3. android:title="@string/AddingItems"
  4. android:key="pref_key_storage_settings">
  5.  
  6. <ListPreference
  7. android:key="pref_key_new_items"
  8. android:title="@string/LocationOfNewItems"
  9. android:summary="@string/LocationOfNewItemsSummary"
  10. android:entries="@array/new_items_entry"
  11. android:entryValues="@array/new_item_entry_value"
  12. android:defaultValue="1"/>
  13.  
  14. </PreferenceCategory>
  15. </PreferenceScreen>
  16.  
  17. public class MyPreferenceActivity extends ActionBarActivity {
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.pref_with_actionbar);
  22.  
  23. android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(uk.japplications.jcommon.R.id.toolbar);
  24. setSupportActionBar(toolbar);
  25.  
  26. getFragmentManager().beginTransaction().replace(R.id.content_frame, new MyPreferenceFragment()).commit();
  27. }
  28. }
  29.  
  30. <RelativeLayout
  31. xmlns:android="http://schemas.android.com/apk/res/android"
  32. xmlns:app="http://schemas.android.com/apk/res-auto"
  33. android:orientation="vertical"
  34. android:layout_width="match_parent"
  35. android:layout_height="match_parent">
  36.  
  37. <android.support.v7.widget.Toolbar
  38. android:id="@+id/toolbar"
  39. android:layout_height="@dimen/action_bar_height"
  40. android:layout_width="match_parent"
  41. android:minHeight="?attr/actionBarSize"
  42. android:background="?attr/colorPrimary"
  43. app:theme="@style/ToolbarTheme.Base"
  44. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  45.  
  46. <FrameLayout
  47. android:id="@+id/content_frame"
  48. android:layout_below="@+id/toolbar"
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content" />
  51.  
  52. </RelativeLayout>
  53.  
  54. public static class MyPreferenceFragment extends PreferenceFragment{
  55. @Override
  56. public void onCreate(final Bundle savedInstanceState){
  57. super.onCreate(savedInstanceState);
  58. addPreferencesFromResource(R.xml.settings);
  59. }
  60. }
  61.  
  62. <?xml version="1.0" encoding="utf-8"?>
  63. <android.support.v7.widget.Toolbar
  64. xmlns:android="http://schemas.android.com/apk/res/android"
  65. xmlns:app="http://schemas.android.com/apk/res-auto"
  66. android:id="@+id/toolbar"
  67. app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  68. android:layout_width="match_parent"
  69. android:layout_height="wrap_content"
  70. android:minHeight="?attr/actionBarSize"
  71. app:navigationContentDescription="@string/abc_action_bar_up_description"
  72. android:background="?attr/colorPrimary"
  73. app:navigationIcon="?attr/homeAsUpIndicator"
  74. app:title="@string/action_settings"
  75. />
  76.  
  77. public class SettingsActivity extends PreferenceActivity {
  78.  
  79. @Override
  80. protected void onPostCreate(Bundle savedInstanceState) {
  81. super.onPostCreate(savedInstanceState);
  82.  
  83. LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
  84. Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
  85. root.addView(bar, 0); // insert at top
  86. bar.setNavigationOnClickListener(new View.OnClickListener() {
  87. @Override
  88. public void onClick(View v) {
  89. finish();
  90. }
  91. });
  92. }
  93.  
  94. }
  95.  
  96. LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
  97.  
  98. public class SettingsActivity extends PreferenceActivity {
  99.  
  100. @Override
  101. protected void onPostCreate(Bundle savedInstanceState) {
  102. super.onPostCreate(savedInstanceState);
  103. Toolbar bar;
  104.  
  105. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  106. LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
  107. bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
  108. root.addView(bar, 0); // insert at top
  109. } else {
  110. ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
  111. ListView content = (ListView) root.getChildAt(0);
  112.  
  113. root.removeAllViews();
  114.  
  115. bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
  116.  
  117.  
  118. int height;
  119. TypedValue tv = new TypedValue();
  120. if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
  121. height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
  122. }else{
  123. height = bar.getHeight();
  124. }
  125.  
  126. content.setPadding(0, height, 0, 0);
  127.  
  128. root.addView(content);
  129. root.addView(bar);
  130. }
  131.  
  132. bar.setNavigationOnClickListener(new View.OnClickListener() {
  133. @Override
  134. public void onClick(View v) {
  135. finish();
  136. }
  137. });
  138. }
  139. }
  140.  
  141. import android.support.v7.internal.widget.TintCheckBox;
  142. import android.support.v7.internal.widget.TintCheckedTextView;
  143. import android.support.v7.internal.widget.TintEditText;
  144. import android.support.v7.internal.widget.TintRadioButton;
  145. import android.support.v7.internal.widget.TintSpinner;
  146.  
  147. @Override
  148. public View onCreateView(String name, Context context, AttributeSet attrs) {
  149. // Allow super to try and create a view first
  150. final View result = super.onCreateView(name, context, attrs);
  151. if (result != null) {
  152. return result;
  153. }
  154.  
  155. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  156. // If we're running pre-L, we need to 'inject' our tint aware Views in place of the
  157. // standard framework versions
  158. switch (name) {
  159. case "EditText":
  160. return new TintEditText(this, attrs);
  161. case "Spinner":
  162. return new TintSpinner(this, attrs);
  163. case "CheckBox":
  164. return new TintCheckBox(this, attrs);
  165. case "RadioButton":
  166. return new TintRadioButton(this, attrs);
  167. case "CheckedTextView":
  168. return new TintCheckedTextView(this, attrs);
  169. }
  170. }
  171.  
  172. return null;
  173. }
  174.  
  175. @Override
  176. public View onCreateView(String name, Context context, AttributeSet attrs) {
  177. // Allow super to try and create a view first
  178. final View result = super.onCreateView(name, context, attrs);
  179. if (result != null) {
  180. return result;
  181. }
  182.  
  183. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  184. // If we're running pre-L, we need to 'inject' our tint aware Views in place of the
  185. // standard framework versions
  186. switch (name) {
  187. case "EditText":
  188. return new AppCompatEditText(this, attrs);
  189. case "Spinner":
  190. return new AppCompatSpinner(this, attrs);
  191. case "CheckBox":
  192. return new AppCompatCheckBox(this, attrs);
  193. case "RadioButton":
  194. return new AppCompatRadioButton(this, attrs);
  195. case "CheckedTextView":
  196. return new AppCompatCheckedTextView(this, attrs);
  197. }
  198. }
  199.  
  200. return null;
  201. }
  202.  
  203. @SuppressWarnings("deprecation")
  204. @Override
  205. public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
  206. super.onPreferenceTreeClick(preferenceScreen, preference);
  207.  
  208. // If the user has clicked on a preference screen, set up the screen
  209. if (preference instanceof PreferenceScreen) {
  210. setUpNestedScreen((PreferenceScreen) preference);
  211. }
  212.  
  213. return false;
  214. }
  215.  
  216. public void setUpNestedScreen(PreferenceScreen preferenceScreen) {
  217. final Dialog dialog = preferenceScreen.getDialog();
  218.  
  219. Toolbar bar;
  220.  
  221. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  222. LinearLayout root = (LinearLayout) dialog.findViewById(android.R.id.list).getParent();
  223. bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
  224. root.addView(bar, 0); // insert at top
  225. } else {
  226. ViewGroup root = (ViewGroup) dialog.findViewById(android.R.id.content);
  227. ListView content = (ListView) root.getChildAt(0);
  228.  
  229. root.removeAllViews();
  230.  
  231. bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
  232.  
  233. int height;
  234. TypedValue tv = new TypedValue();
  235. if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
  236. height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
  237. }else{
  238. height = bar.getHeight();
  239. }
  240.  
  241. content.setPadding(0, height, 0, 0);
  242.  
  243. root.addView(content);
  244. root.addView(bar);
  245. }
  246.  
  247. bar.setTitle(preferenceScreen.getTitle());
  248.  
  249. bar.setNavigationOnClickListener(new View.OnClickListener() {
  250. @Override
  251. public void onClick(View v) {
  252. dialog.dismiss();
  253. }
  254. });
  255. }
  256.  
  257. <android.support.design.widget.AppBarLayout
  258. android:layout_width="match_parent"
  259. android:layout_height="wrap_content">
  260.  
  261. <android.support.v7.widget.Toolbar
  262. .../>
  263.  
  264. </android.support.design.widget.AppBarLayout>
  265.  
  266. compile 'com.android.support:support-v4:22.2.0'
  267. compile 'com.android.support:appcompat-v7:22.2.0'
  268. compile 'com.android.support:design:22.2.0'
  269.  
  270. public class SettingsActivity extends AppCompatPreferenceActivity {
  271.  
  272. @Override
  273. public void onBuildHeaders(List<Header> target) {
  274. loadHeadersFromResource(R.xml.settings, target);
  275.  
  276. setContentView(R.layout.settings_page);
  277. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  278. setSupportActionBar(toolbar);
  279.  
  280. ActionBar bar = getSupportActionBar();
  281. bar.setHomeButtonEnabled(true);
  282. bar.setDisplayHomeAsUpEnabled(true);
  283. bar.setDisplayShowTitleEnabled(true);
  284. bar.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
  285. bar.setTitle(...);
  286. }
  287.  
  288. @Override
  289. protected boolean isValidFragment(String fragmentName) {
  290. return SettingsFragment.class.getName().equals(fragmentName);
  291. }
  292.  
  293. @Override
  294. public boolean onOptionsItemSelected(MenuItem item) {
  295. switch (item.getItemId()) {
  296. case android.R.id.home:
  297. onBackPressed();
  298. break;
  299. }
  300. return super.onOptionsItemSelected(item);
  301. }
  302. }
  303.  
  304. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  305. android:layout_width="match_parent"
  306. android:layout_height="match_parent"
  307. android:layout_margin="0dp"
  308. android:orientation="vertical"
  309. android:padding="0dp">
  310. <android.support.v7.widget.Toolbar
  311. android:id="@+id/toolbar"
  312. android:layout_width="match_parent"
  313. android:layout_height="?attr/actionBarSize"
  314. android:background="?attr/colorPrimary"
  315. android:elevation="4dp"
  316. android:theme="@style/..."/>
  317. <ListView
  318. android:id="@id/android:list"
  319. android:layout_width="match_parent"
  320. android:layout_height="match_parent"/>
  321. </LinearLayout>
  322.  
  323. <preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
  324. <header
  325. android:fragment="com.example.SettingsFragment"
  326. android:summary="@string/..."
  327. android:title="@string/...">
  328. <extra
  329. android:name="page"
  330. android:value="page1"/>
  331. </header>
  332. <header
  333. android:fragment="com.example.SettingsFragment"
  334. android:summary="@string/..."
  335. android:title="@string/...">
  336. <extra
  337. android:name="page"
  338. android:value="page2"/>
  339. </header>
  340. ...
  341. </preference-headers>
  342.  
  343. public class SettingsFragment extends PreferenceFragment {
  344.  
  345. @Override
  346. public void onCreate(Bundle savedInstanceState) {
  347. super.onCreate(savedInstanceState);
  348. getActivity().setTheme(R.style...);
  349.  
  350. if (getArguments() != null) {
  351. String page = getArguments().getString("page");
  352. if (page != null)
  353. switch (page) {
  354. case "page1":
  355. addPreferencesFromResource(R.xml.settings_page1);
  356. break;
  357. case "page2":
  358. addPreferencesFromResource(R.xml.settings_page2);
  359. break;
  360. ...
  361. }
  362. }
  363. }
  364.  
  365. @Override
  366. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  367. View layout = inflater.inflate(R.layout.settings_page, container, false);
  368. if (layout != null) {
  369. AppCompatPreferenceActivity activity = (AppCompatPreferenceActivity) getActivity();
  370. Toolbar toolbar = (Toolbar) layout.findViewById(R.id.toolbar);
  371. activity.setSupportActionBar(toolbar);
  372.  
  373. ActionBar bar = activity.getSupportActionBar();
  374. bar.setHomeButtonEnabled(true);
  375. bar.setDisplayHomeAsUpEnabled(true);
  376. bar.setDisplayShowTitleEnabled(true);
  377. bar.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
  378. bar.setTitle(getPreferenceScreen().getTitle());
  379. }
  380. return layout;
  381. }
  382.  
  383. @Override
  384. public void onResume() {
  385. super.onResume();
  386.  
  387. if (getView() != null) {
  388. View frame = (View) getView().getParent();
  389. if (frame != null)
  390. frame.setPadding(0, 0, 0, 0);
  391. }
  392. }
  393. }
  394.  
  395. import android.support.v7.widget.Toolbar;
  396.  
  397. public class MyPreferenceActivity extends PreferenceActivity
  398.  
  399. Toolbar mToolbar;
  400.  
  401. @Override
  402. protected void onCreate(Bundle savedInstanceState) {
  403. super.onCreate(savedInstanceState);
  404.  
  405. ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
  406. LinearLayout content = (LinearLayout) root.getChildAt(0);
  407. LinearLayout toolbarContainer = (LinearLayout) View.inflate(this, R.layout.activity_settings, null);
  408.  
  409. root.removeAllViews();
  410. toolbarContainer.addView(content);
  411. root.addView(toolbarContainer);
  412.  
  413. mToolbar = (Toolbar) toolbarContainer.findViewById(R.id.toolbar);
  414. }
  415.  
  416. @Override
  417. public void onBuildHeaders(List<Header> target) {
  418. loadHeadersFromResource(R.xml.pref_headers, target);
  419. }
  420.  
  421. // Other methods
  422.  
  423. }
  424.  
  425. <LinearLayout
  426. xmlns:android="http://schemas.android.com/apk/res/android"
  427. xmlns:app="http://schemas.android.com/apk/res-auto"
  428. android:orientation="vertical"
  429. android:layout_width="match_parent"
  430. android:layout_height="match_parent">
  431.  
  432. <android.support.v7.widget.Toolbar
  433. android:id="@+id/toolbar"
  434. android:layout_height="?attr/actionBarSize"
  435. android:layout_width="match_parent"
  436. android:minHeight="?attr/actionBarSize"
  437. android:background="?attr/colorPrimary"
  438. app:theme="@style/AppTheme"
  439. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  440.  
  441. </LinearLayout>
  442.  
  443. <preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
  444.  
  445. <header
  446. android:fragment="com.example.FirstFragment"
  447. android:title="@string/pref_header_first" />
  448. <header
  449. android:fragment="com.example.SecondFragment"
  450. android:title="@string/pref_header_second" />
  451.  
  452. </preference-headers>
  453.  
  454. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  455. xmlns:tools="http://schemas.android.com/tools"
  456. xmlns:app="http://schemas.android.com/apk/res-auto"
  457. android:layout_width="match_parent"
  458. android:layout_height="match_parent"
  459. android:orientation="vertical" >
  460.  
  461. <android.support.v7.widget.Toolbar
  462. android:id="@+id/toolbar"
  463. android:layout_width="match_parent"
  464. android:layout_height="wrap_content"
  465. android:background="@color/app_theme_light"
  466. app:popupTheme="@style/Theme.AppCompat.Light"
  467. app:theme="@style/Theme.AppCompat" />
  468.  
  469. <FrameLayout
  470. android:layout_width="match_parent"
  471. android:layout_height="match_parent"
  472. android:padding="@dimen/padding_medium" >
  473.  
  474. <ListView
  475. android:id="@android:id/list"
  476. android:layout_width="match_parent"
  477. android:layout_height="match_parent" />
  478. </FrameLayout>
  479.  
  480. public class SettingsActivity extends PreferenceActivity {
  481.  
  482. @Override
  483. protected void onCreate(Bundle savedInstanceState) {
  484. super.onCreate(savedInstanceState);
  485.  
  486. setContentView(R.layout.activity_settings);
  487.  
  488. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  489.  
  490. addPreferencesFromResource(R.xml.preferences);
  491.  
  492. toolbar.setClickable(true);
  493. toolbar.setNavigationIcon(getResIdFromAttribute(this, R.attr.homeAsUpIndicator));
  494. toolbar.setTitle(R.string.menu_settings);
  495. toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  496.  
  497. @Override
  498. public void onClick(View v) {
  499. finish();
  500. }
  501. });
  502.  
  503. }
  504.  
  505. private static int getResIdFromAttribute(final Activity activity, final int attr) {
  506. if (attr == 0) {
  507. return 0;
  508. }
  509. final TypedValue typedvalueattr = new TypedValue();
  510. activity.getTheme().resolveAttribute(attr, typedvalueattr, true);
  511. return typedvalueattr.resourceId;
  512. }
  513. }
  514.  
  515. public void setSupportActionBar(@Nullable Toolbar toolbar) {
  516. getDelegate().setSupportActionBar(toolbar);
  517. ActionBar bar = getDelegate().getSupportActionBar();
  518. bar.setHomeButtonEnabled(true);
  519. bar.setDisplayHomeAsUpEnabled(true);
  520. }
  521.  
  522. abstract class AppCompatPreferenceFragment extends PreferenceFragment {
  523.  
  524. @Override
  525. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  526. View view = inflater.inflate(R.layout.activity_settings, container, false);
  527. if (view != null) {
  528. Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar_settings);
  529. ((AppCompatPreferenceActivity) getActivity()).setSupportActionBar(toolbar);
  530. }
  531. return view;
  532. }
  533.  
  534. @Override
  535. public void onResume() {
  536. super.onResume();
  537. View frame = (View) getView().getParent();
  538. if (frame != null) frame.setPadding(0, 0, 0, 0);
  539. }
  540. }
  541.  
  542. public class SettingsActivity extends AppCompatPreferenceActivity {
  543.  
  544. boolean mAttachedFragment;
  545.  
  546. @Override
  547. protected void onCreate(Bundle savedInstanceState) {
  548. mAttachedFragment = false;
  549. super.onCreate(savedInstanceState);
  550. }
  551.  
  552. @Override
  553. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  554. public void onBuildHeaders(List<Header> target) {
  555. loadHeadersFromResource(R.xml.pref_headers, target);
  556. }
  557.  
  558. @Override
  559. public void onAttachFragment(Fragment fragment) {
  560. mAttachedFragment = true;
  561. super.onAttachFragment(fragment);
  562. }
  563.  
  564. @Override
  565. protected void onPostCreate(Bundle savedInstanceState) {
  566. super.onPostCreate(savedInstanceState);
  567.  
  568. //if we didn't attach a fragment, go ahead and apply the layout
  569. if (!mAttachedFragment) {
  570. setContentView(R.layout.activity_settings);
  571. setSupportActionBar((Toolbar)findViewById(R.id.toolbar_settings));
  572. }
  573. }
  574.  
  575. /**
  576. * This fragment shows general preferences only. It is used when the
  577. * activity is showing a two-pane settings UI.
  578. */
  579. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  580. public static class GeneralPreferenceFragment extends AppCompatPreferenceFragment {
  581. @Override
  582. public void onCreate(Bundle savedInstanceState) {
  583. super.onCreate(savedInstanceState);
  584.  
  585. addPreferencesFromResource(R.xml.pref_general);
  586. setHasOptionsMenu(true);
  587.  
  588. bindPreferenceSummaryToValue(findPreference("example_text"));
  589. bindPreferenceSummaryToValue(findPreference("example_list"));
  590. }
  591.  
  592. @Override
  593. public boolean onOptionsItemSelected(MenuItem item) {
  594. int id = item.getItemId();
  595. if (id == android.R.id.home) {
  596. startActivity(new Intent(getActivity(), SettingsActivity.class));
  597. return true;
  598. }
  599. return super.onOptionsItemSelected(item);
  600. }
  601. }
  602. }
  603.  
  604. <?xml version="1.0" encoding="utf-8"?>
  605. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  606. android:orientation="vertical"
  607. android:layout_width="match_parent"
  608. android:layout_height="match_parent">
  609.  
  610. <include
  611. layout="@layout/app_bar_settings"
  612. android:layout_width="match_parent"
  613. android:layout_height="match_parent" />
  614.  
  615. </LinearLayout>
  616.  
  617. <?xml version="1.0" encoding="utf-8"?>
  618. <android.support.design.widget.CoordinatorLayout
  619. xmlns:android="http://schemas.android.com/apk/res/android"
  620. xmlns:app="http://schemas.android.com/apk/res-auto"
  621. xmlns:tools="http://schemas.android.com/tools"
  622. android:id="@+id/content_frame"
  623. android:layout_width="match_parent"
  624. android:layout_height="match_parent"
  625. android:fitsSystemWindows="true"
  626. tools:context=".SettingsActivity">
  627.  
  628. <android.support.design.widget.AppBarLayout
  629. android:layout_width="match_parent"
  630. android:layout_height="wrap_content"
  631. android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">
  632.  
  633. <android.support.v7.widget.Toolbar
  634. android:id="@+id/toolbar_settings"
  635. android:layout_width="match_parent"
  636. android:layout_height="?attr/actionBarSize"
  637. android:background="?attr/colorPrimary"
  638. app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay" />
  639.  
  640. </android.support.design.widget.AppBarLayout>
  641.  
  642. <include layout="@layout/content_settings" />
  643.  
  644. </android.support.design.widget.CoordinatorLayout>
  645.  
  646. <?xml version="1.0" encoding="utf-8"?>
  647. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  648. xmlns:app="http://schemas.android.com/apk/res-auto"
  649. xmlns:tools="http://schemas.android.com/tools"
  650. android:id="@+id/content"
  651. android:layout_width="match_parent"
  652. android:layout_height="match_parent"
  653. android:paddingBottom="@dimen/activity_vertical_margin"
  654. android:paddingLeft="@dimen/activity_horizontal_margin"
  655. android:paddingRight="@dimen/activity_horizontal_margin"
  656. android:paddingTop="@dimen/activity_vertical_margin"
  657. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  658. tools:context=".SettingsActivity"
  659. tools:showIn="@layout/app_bar_settings">
  660.  
  661. <ListView
  662. android:id="@android:id/list"
  663. android:layout_width="match_parent"
  664. android:layout_height="wrap_content" />
  665.  
  666. </RelativeLayout>
  667.  
  668. import android.support.design.widget.AppBarLayout;
  669. import android.support.v4.app.NavUtils;
  670. import android.support.v7.widget.Toolbar;
  671.  
  672. private void setupActionBar() {
  673. Toolbar toolbar = new Toolbar(this);
  674.  
  675. AppBarLayout appBarLayout = new AppBarLayout(this);
  676. appBarLayout.addView(toolbar);
  677.  
  678. final ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
  679. final ViewGroup window = (ViewGroup) root.getChildAt(0);
  680. window.addView(appBarLayout, 0);
  681.  
  682. setSupportActionBar(toolbar);
  683.  
  684. // Show the Up button in the action bar.
  685. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  686. toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  687. @Override
  688. public void onClick(View v) {
  689. onBackPressed();
  690. }
  691. });
  692. }
  693.  
  694. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  695. xmlns:app="http://schemas.android.com/apk/res-auto"
  696. xmlns:tools="http://schemas.android.com/tools"
  697. android:layout_width="match_parent"
  698. android:layout_height="match_parent"
  699. tools:context="com.my.package">
  700.  
  701. <android.support.v7.widget.Toolbar
  702. android:id="@+id/tool_bar"
  703. android:layout_width="match_parent"
  704. android:layout_height="?attr/actionBarSize"
  705. android:background="?attr/colorPrimary"
  706. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  707. app:elevation="@dimen/appbar_elevation"
  708. app:navigationIcon="?attr/homeAsUpIndicator"
  709. app:navigationContentDescription="@string/abc_action_bar_up_description"
  710. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
  711.  
  712. <ListView
  713. android:id="@android:id/list"
  714. android:layout_width="wrap_content"
  715. android:layout_height="wrap_content"
  716. android:layout_below="@+id/tool_bar" />
  717.  
  718. </RelativeLayout>
  719.  
  720. @Override
  721. protected void onCreate(Bundle savedInstanceState) {
  722. super.onCreate(savedInstanceState);
  723. setContentView(R.layout.activity_settings);
  724. Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
  725. toolbar.setTitle(R.string.action_settings);
  726. toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  727. @Override
  728. public void onClick(View v) {
  729. finish();
  730. }
  731. });
  732. }
  733.  
  734. <?xml version="1.0" encoding="utf-8"?>
  735. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  736. xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
  737. android:layout_width="match_parent" android:layout_height="match_parent"
  738. android:fitsSystemWindows="true" tools:context="edu.adelphi.Adelphi.ui.activity.MainActivity">
  739.  
  740. <android.support.design.widget.AppBarLayout android:id="@+id/appbar"
  741. android:layout_width="match_parent" android:layout_height="wrap_content"
  742. android:theme="@style/AppTheme.AppBarOverlay">
  743.  
  744. <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
  745. android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
  746. android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/>
  747.  
  748. </android.support.design.widget.AppBarLayout>
  749.  
  750. <FrameLayout android:id="@+id/content"
  751. android:layout_width="match_parent" android:layout_height="match_parent"/>
  752.  
  753. </android.support.design.widget.CoordinatorLayout>
  754.  
  755. @Override
  756. public void setContentView(@LayoutRes int layoutResID) {
  757. View view = getLayoutInflater().inflate(R.layout.toolbar, null);
  758. FrameLayout content = (FrameLayout) view.findViewById(R.id.content);
  759. getLayoutInflater().inflate(layoutResID, content, true);
  760. setContentView(view);
  761. }
  762.  
  763. private void setupActionBar() {
  764. Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
  765. //Toolbar will now take on default Action Bar characteristics
  766. setSupportActionBar(toolbar);
  767. getSupportActionBar().setHomeButtonEnabled(true);
  768. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  769.  
  770. }
  771.  
  772. @Override
  773. public boolean onOptionsItemSelected(MenuItem item) {
  774. switch (item.getItemId()) {
  775. case android.R.id.home:
  776. onBackPressed();
  777. return true;
  778. }
  779. return super.onOptionsItemSelected(item);
  780. }
  781.  
  782. @Override
  783. public void onBackPressed() {
  784. if (getFragmentManager().getBackStackEntryCount() > 0) {
  785. getFragmentManager().popBackStackImmediate();
  786. //If the last fragment was removed then reset the title of main
  787. // fragment (if so the previous popBackStack made entries = 0).
  788. if (getFragmentManager().getBackStackEntryCount() == 0) {
  789. getSupportActionBar()
  790. .setTitle(R.string.action_settings_title);
  791. }
  792. } else {
  793. super.onBackPressed();
  794. }
  795. }
  796.  
  797. <activity
  798. android:name=".Dashboard.PreferencepActivity"
  799. android:label="@string/title_activity_preference"
  800. android:theme="@style/Pref"></activity>
  801.  
  802. <style name="Pref" parent="Theme.AppCompat.DayNight.DarkActionBar">
  803. <!-- Customize your theme here. -->
  804. <item name="colorPrimary">@color/colorPrimary</item>
  805. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  806. <item name="colorAccent">@color/colorAccent</item>
  807. </style>
Add Comment
Please, Sign In to add comment