Skip to content

Commit e789d6a

Browse files
committed
Refactored code to make it easier to define a font family for the application along with its own specific ‘textStyle’ values.
For example, you can have the Roboto font family with ‘normal’, ‘bold’, ‘condensed’, ‘condensedBold’ etc. and also the SourceSansPro family with ‘lightItalic’ and ‘black’. Normally in one application theme you have a single font family, but this also supports multiple font families at once.
1 parent c40795a commit e789d6a

19 files changed

+325
-79
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local.properties
1818
# Eclipse project files
1919
.classpath
2020
.project
21+
.gradle/
2122

2223
# Proguard folder generated by Eclipse
2324
proguard/

Library/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
android:allowBackup="true"
1111
android:icon="@drawable/ic_launcher"
1212
android:label="@string/app_name"
13-
android:theme="@style/AppTheme"
13+
android:theme="@style/AppTheme.Light"
1414
android:name=".MyApp">
1515
<activity
1616
android:name=".MainActivity"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Library/src/main/java/org/codeandmagic/android/MainActivity.java

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,102 @@
77
import android.view.Menu;
88
import android.view.MenuItem;
99
import android.view.View;
10+
import android.view.View.OnClickListener;
1011
import android.view.ViewGroup;
1112

1213
public class MainActivity extends ActionBarActivity {
1314

15+
public static final String THEME_ID = "theme_id";
16+
1417
@Override
1518
protected void onCreate(Bundle savedInstanceState) {
1619
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+
1731
setContentView(R.layout.activity_main);
1832

33+
// Demo fragments for showing the fonts in action.
1934
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();
2338
}
2439
}
2540

2641

2742
@Override
2843
public boolean onCreateOptionsMenu(Menu menu) {
29-
30-
// Inflate the menu; this adds items to the action bar if it is present.
3144
getMenuInflater().inflate(R.menu.main, menu);
3245
return true;
3346
}
3447

3548
@Override
3649
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);
4359
}
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));
4563
}
4664

47-
/**
48-
* A placeholder fragment containing a simple view.
49-
*/
50-
public static class PlaceholderFragment extends Fragment {
5165

52-
public PlaceholderFragment() {
66+
public static class RobotoFragment extends Fragment {
67+
68+
public static RobotoFragment newInstance() {
69+
return new RobotoFragment();
5370
}
5471

5572
@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+
5976
// 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);
6178
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+
});
62106
return rootView;
63107
}
64108
}

Library/src/main/java/org/codeandmagic/android/MyApp.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.codeandmagic.android;
22

33
import android.app.Application;
4+
import android.content.Context;
5+
import android.content.pm.PackageInfo;
6+
import android.content.pm.PackageManager;
47

58
/**
69
* Created by evelina on 16/01/2014.
@@ -10,6 +13,17 @@ public class MyApp extends Application {
1013
@Override
1114
public void onCreate() {
1215
super.onCreate();
13-
TypefaceManager.setTextStyleExtractor(RobotoTextStyleExtractor.getInstance());
16+
TypefaceManager.addTextStyleExtractor(RobotoTextStyleExtractor.getInstance());
17+
TypefaceManager.addTextStyleExtractor(SourceSansProTextStyleExtractor.getInstance());
18+
}
19+
20+
public static int getThemeId(Context context) {
21+
try {
22+
String packageName = MyApp.class.getPackage().getName();
23+
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
24+
return packageInfo.applicationInfo.theme;
25+
} catch (Exception e) {
26+
return 0;
27+
}
1428
}
1529
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
package org.codeandmagic.android;
22

33
/**
4+
* Implementation of {@link TextStyle} defining the possible values for the 'textStyle' attribute
5+
* using the Roboto font.
46
* Created by evelina on 16/01/2014.
57
*/
68
public enum RobotoTextStyle implements TextStyle {
79

8-
NORMAL("roboto/Roboto-Regular.ttf"),
9-
LIGHT("roboto/Roboto-Light.ttf"),
10-
BOLD("roboto/Roboto-Bold.ttf"),
11-
CONDENSED("roboto/RobotoCondensed-Regular.ttf"),
12-
CONDENSED_LIGHT("roboto/RobotoCondensed-Light.ttf"),
13-
CONDENSED_BOLD("roboto/RobotoCondensed-Bold.ttf");
10+
NORMAL("normal", "roboto/Roboto-Regular.ttf"),
11+
LIGHT("light", "roboto/Roboto-Light.ttf"),
12+
BOLD("bold", "roboto/Roboto-Bold.ttf"),
13+
CONDENSED("condensed", "roboto/RobotoCondensed-Regular.ttf"),
14+
CONDENSED_LIGHT("condensedLight", "roboto/RobotoCondensed-Light.ttf"),
15+
CONDENSED_BOLD("condensedBold", "roboto/RobotoCondensed-Bold.ttf");
1416

17+
private String mName;
1518
private String mFontName;
1619

17-
RobotoTextStyle(String fontName) {
20+
RobotoTextStyle(String name, String fontName) {
21+
mName = name;
1822
mFontName = fontName;
1923
}
2024

2125
@Override
2226
public String getFontName() {
2327
return mFontName;
2428
}
29+
30+
@Override
31+
public String getName() {
32+
return mName;
33+
}
2534
}
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package org.codeandmagic.android;
22

3-
import android.app.Application;
4-
53
/**
6-
* Implementation of {@TextStyleExtractor} which defines what fonts are used by the
7-
* {@link Application}.
4+
* Implementation of {@TextStyleExtractor} for the Roboto font.
85
*/
9-
public class RobotoTextStyleExtractor implements TextStyleExtractor {
6+
public class RobotoTextStyleExtractor extends TextStyleExtractor {
107

118
private static final RobotoTextStyleExtractor INSTANCE = new RobotoTextStyleExtractor();
129

@@ -18,9 +15,4 @@ public static TextStyleExtractor getInstance() {
1815
public TextStyle[] getTextStyles() {
1916
return RobotoTextStyle.values();
2017
}
21-
22-
@Override
23-
public TextStyle getFromTextStyleOrdinal(int textStyleOrdinal) {
24-
return getTextStyles()[textStyleOrdinal];
25-
}
2618
}

0 commit comments

Comments
 (0)