|
7 | 7 | import android.view.Menu;
|
8 | 8 | import android.view.MenuItem;
|
9 | 9 | import android.view.View;
|
| 10 | +import android.view.View.OnClickListener; |
10 | 11 | import android.view.ViewGroup;
|
11 | 12 |
|
12 | 13 | public class MainActivity extends ActionBarActivity {
|
13 | 14 |
|
| 15 | + public static final String THEME_ID = "theme_id"; |
| 16 | + |
14 | 17 | @Override
|
15 | 18 | protected void onCreate(Bundle savedInstanceState) {
|
16 | 19 | super.onCreate(savedInstanceState);
|
| 20 | + |
| 21 | + // The AppTheme.Light theme shows Roboto and SourceSansPro fonts. |
| 22 | + // The AppTheme.Dark theme shows SourceSansPro font. |
| 23 | + final int themeId; |
| 24 | + if (getIntent().hasExtra(THEME_ID)) { |
| 25 | + themeId = getIntent().getIntExtra(THEME_ID, R.style.AppTheme_Light); |
| 26 | + setTheme(themeId); |
| 27 | + } else { |
| 28 | + themeId = MyApp.getThemeId(this); |
| 29 | + } |
| 30 | + |
17 | 31 | setContentView(R.layout.activity_main);
|
18 | 32 |
|
| 33 | + // Demo fragments for showing the fonts in action. |
19 | 34 | if (savedInstanceState == null) {
|
20 |
| - getSupportFragmentManager().beginTransaction() |
21 |
| - .add(R.id.container, new PlaceholderFragment()) |
22 |
| - .commit(); |
| 35 | + final Fragment fragment = (R.style.AppTheme_Light == themeId) ? RobotoFragment.newInstance() |
| 36 | + : SourceSansProFragment.newInstance(); |
| 37 | + getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit(); |
23 | 38 | }
|
24 | 39 | }
|
25 | 40 |
|
26 | 41 |
|
27 | 42 | @Override
|
28 | 43 | public boolean onCreateOptionsMenu(Menu menu) {
|
29 |
| - |
30 |
| - // Inflate the menu; this adds items to the action bar if it is present. |
31 | 44 | getMenuInflater().inflate(R.menu.main, menu);
|
32 | 45 | return true;
|
33 | 46 | }
|
34 | 47 |
|
35 | 48 | @Override
|
36 | 49 | public boolean onOptionsItemSelected(MenuItem item) {
|
37 |
| - // Handle action bar item clicks here. The action bar will |
38 |
| - // automatically handle clicks on the Home/Up button, so long |
39 |
| - // as you specify a parent activity in AndroidManifest.xml. |
40 |
| - int id = item.getItemId(); |
41 |
| - if (id == R.id.action_settings) { |
42 |
| - return true; |
| 50 | + return item.getItemId() == R.id.action_settings || super.onOptionsItemSelected(item); |
| 51 | + } |
| 52 | + |
| 53 | + public void switchTheme() { |
| 54 | + final int themeId; |
| 55 | + if (getIntent().hasExtra(THEME_ID)) { |
| 56 | + themeId = getIntent().getIntExtra(THEME_ID, R.style.AppTheme_Light); |
| 57 | + } else { |
| 58 | + themeId = MyApp.getThemeId(this); |
43 | 59 | }
|
44 |
| - return super.onOptionsItemSelected(item); |
| 60 | + final int newThemeId = (R.style.AppTheme_Light == themeId) ? R.style.AppTheme_Dark : R.style.AppTheme_Light; |
| 61 | + finish(); |
| 62 | + startActivity(getIntent().putExtra(THEME_ID, newThemeId)); |
45 | 63 | }
|
46 | 64 |
|
47 |
| - /** |
48 |
| - * A placeholder fragment containing a simple view. |
49 |
| - */ |
50 |
| - public static class PlaceholderFragment extends Fragment { |
51 | 65 |
|
52 |
| - public PlaceholderFragment() { |
| 66 | + public static class RobotoFragment extends Fragment { |
| 67 | + |
| 68 | + public static RobotoFragment newInstance() { |
| 69 | + return new RobotoFragment(); |
53 | 70 | }
|
54 | 71 |
|
55 | 72 | @Override
|
56 |
| - public View onCreateView(LayoutInflater inflater, ViewGroup container, |
57 |
| - Bundle savedInstanceState) { |
58 |
| - View rootView = inflater.inflate(R.layout.fragment_main, container, false); |
| 73 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 74 | + final View rootView = inflater.inflate(R.layout.fragment_main_roboto, container, false); |
| 75 | + |
59 | 76 | // Apply a custom TextStyle from code.
|
60 |
| - TypefaceTextView textView = (TypefaceTextView) rootView.findViewById(R.id.textView); |
| 77 | + final TypefaceTextView textView = (TypefaceTextView) rootView.findViewById(R.id.textView); |
61 | 78 | textView.setTextStyle(RobotoTextStyle.BOLD);
|
| 79 | + |
| 80 | + rootView.findViewById(R.id.switch_theme).setOnClickListener(new OnClickListener() { |
| 81 | + @Override |
| 82 | + public void onClick(View v) { |
| 83 | + ((MainActivity) getActivity()).switchTheme(); |
| 84 | + } |
| 85 | + }); |
| 86 | + return rootView; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static class SourceSansProFragment extends Fragment { |
| 91 | + |
| 92 | + public static SourceSansProFragment newInstance() { |
| 93 | + return new SourceSansProFragment(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 98 | + final View rootView = inflater.inflate(R.layout.fragment_main_source, container, false); |
| 99 | + |
| 100 | + rootView.findViewById(R.id.switch_theme).setOnClickListener(new OnClickListener() { |
| 101 | + @Override |
| 102 | + public void onClick(View v) { |
| 103 | + ((MainActivity) getActivity()).switchTheme(); |
| 104 | + } |
| 105 | + }); |
62 | 106 | return rootView;
|
63 | 107 | }
|
64 | 108 | }
|
|
0 commit comments