Skip to content

Commit c40795a

Browse files
committed
Initial commit
1 parent 9a766f9 commit c40795a

38 files changed

+771
-0
lines changed

Library/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Library/build.gradle

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: 'android'
2+
3+
android {
4+
compileSdkVersion 19
5+
buildToolsVersion "19.0.0"
6+
7+
defaultConfig {
8+
minSdkVersion 7
9+
targetSdkVersion 19
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
buildTypes {
14+
release {
15+
runProguard false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
compile 'com.android.support:support-v4:+'
23+
compile 'com.android.support:appcompat-v7:+'
24+
}

Library/proguard-rules.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/evelina/android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the ProGuard
5+
# include property in project.properties.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}

Library/src/main/AndroidManifest.xml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="org.codeandmagic.android">
4+
5+
<uses-sdk
6+
android:minSdkVersion="7"
7+
android:targetSdkVersion="19"/>
8+
9+
<application
10+
android:allowBackup="true"
11+
android:icon="@drawable/ic_launcher"
12+
android:label="@string/app_name"
13+
android:theme="@style/AppTheme"
14+
android:name=".MyApp">
15+
<activity
16+
android:name=".MainActivity"
17+
android:label="@string/app_name">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN"/>
20+
21+
<category android:name="android.intent.category.LAUNCHER"/>
22+
</intent-filter>
23+
</activity>
24+
</application>
25+
26+
</manifest>
114 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Library/src/main/ic_launcher-web.png

46 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.codeandmagic.android;
2+
3+
import android.os.Bundle;
4+
import android.support.v4.app.Fragment;
5+
import android.support.v7.app.ActionBarActivity;
6+
import android.view.LayoutInflater;
7+
import android.view.Menu;
8+
import android.view.MenuItem;
9+
import android.view.View;
10+
import android.view.ViewGroup;
11+
12+
public class MainActivity extends ActionBarActivity {
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.activity_main);
18+
19+
if (savedInstanceState == null) {
20+
getSupportFragmentManager().beginTransaction()
21+
.add(R.id.container, new PlaceholderFragment())
22+
.commit();
23+
}
24+
}
25+
26+
27+
@Override
28+
public boolean onCreateOptionsMenu(Menu menu) {
29+
30+
// Inflate the menu; this adds items to the action bar if it is present.
31+
getMenuInflater().inflate(R.menu.main, menu);
32+
return true;
33+
}
34+
35+
@Override
36+
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;
43+
}
44+
return super.onOptionsItemSelected(item);
45+
}
46+
47+
/**
48+
* A placeholder fragment containing a simple view.
49+
*/
50+
public static class PlaceholderFragment extends Fragment {
51+
52+
public PlaceholderFragment() {
53+
}
54+
55+
@Override
56+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
57+
Bundle savedInstanceState) {
58+
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
59+
// Apply a custom TextStyle from code.
60+
TypefaceTextView textView = (TypefaceTextView) rootView.findViewById(R.id.textView);
61+
textView.setTextStyle(RobotoTextStyle.BOLD);
62+
return rootView;
63+
}
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.codeandmagic.android;
2+
3+
import android.app.Application;
4+
5+
/**
6+
* Created by evelina on 16/01/2014.
7+
*/
8+
public class MyApp extends Application {
9+
10+
@Override
11+
public void onCreate() {
12+
super.onCreate();
13+
TypefaceManager.setTextStyleExtractor(RobotoTextStyleExtractor.getInstance());
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.codeandmagic.android;
2+
3+
/**
4+
* Created by evelina on 16/01/2014.
5+
*/
6+
public enum RobotoTextStyle implements TextStyle {
7+
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");
14+
15+
private String mFontName;
16+
17+
RobotoTextStyle(String fontName) {
18+
mFontName = fontName;
19+
}
20+
21+
@Override
22+
public String getFontName() {
23+
return mFontName;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.codeandmagic.android;
2+
3+
import android.app.Application;
4+
5+
/**
6+
* Implementation of {@TextStyleExtractor} which defines what fonts are used by the
7+
* {@link Application}.
8+
*/
9+
public class RobotoTextStyleExtractor implements TextStyleExtractor {
10+
11+
private static final RobotoTextStyleExtractor INSTANCE = new RobotoTextStyleExtractor();
12+
13+
public static TextStyleExtractor getInstance() {
14+
return INSTANCE;
15+
}
16+
17+
@Override
18+
public TextStyle[] getTextStyles() {
19+
return RobotoTextStyle.values();
20+
}
21+
22+
@Override
23+
public TextStyle getFromTextStyleOrdinal(int textStyleOrdinal) {
24+
return getTextStyles()[textStyleOrdinal];
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.codeandmagic.android;
2+
3+
/**
4+
* Created by evelina on 16/01/2014.
5+
*/
6+
public interface TextStyle {
7+
8+
String getFontName();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.codeandmagic.android;
2+
3+
/**
4+
* Created by evelina on 16/01/2014.
5+
*/
6+
public interface TextStyleExtractor {
7+
8+
TextStyle getFromTextStyleOrdinal(int textStyleOrdinal);
9+
10+
TextStyle[] getTextStyles();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.codeandmagic.android;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.graphics.Typeface;
6+
import android.util.AttributeSet;
7+
import android.view.ContextThemeWrapper;
8+
import android.widget.TextView;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
/**
14+
* Utility class used to apply a custom {@link Typeface} to a {@link TextView} subclass.
15+
*/
16+
public class TypefaceManager {
17+
18+
private static final TypefaceManager INSTANCE = new TypefaceManager();
19+
20+
public static TypefaceManager getInstance() {
21+
return INSTANCE;
22+
}
23+
24+
public static void setTextStyleExtractor(TextStyleExtractor textStyleExtractor) {
25+
INSTANCE.mTextStyleExtractor = textStyleExtractor;
26+
}
27+
28+
private final Map<TextStyle, Typeface> mTypefaces = new HashMap<TextStyle, Typeface>();
29+
private TextStyleExtractor mTextStyleExtractor;
30+
31+
private TypefaceManager() {
32+
// Singleton
33+
}
34+
35+
/**
36+
* Method called from the {@TypefaceTextView} constructor to
37+
* apply a custom {@link TextStyle} defined in the application theme.
38+
*
39+
* @param textView the {@link TextView} to have the {@link TextStyle} applied
40+
* @param context the {@link Context} of the {@link TextView}
41+
* @param attrs the {@link AttributeSet} of the {@link TextView}
42+
*/
43+
public void applyTypeface(TextView textView, Context context, AttributeSet attrs) {
44+
final TypedArray styleValues = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.textAppearance});
45+
final int textAppearanceStyleId = styleValues.getResourceId(0, 0);
46+
if (textAppearanceStyleId != -1) {
47+
final ContextThemeWrapper themedContext = new ContextThemeWrapper(context, textAppearanceStyleId);
48+
final TypedArray textAppearanceStyleValues = themedContext.obtainStyledAttributes(new int[]{R.attr.textStyle});
49+
final int textStyleOrdinal = textAppearanceStyleValues.getInt(0, -1);
50+
if (textStyleOrdinal != -1) {
51+
final TextStyle textStyle = mTextStyleExtractor.getFromTextStyleOrdinal(textStyleOrdinal);
52+
applyTypeface(textView, textStyle);
53+
}
54+
textAppearanceStyleValues.recycle();
55+
}
56+
styleValues.recycle();
57+
}
58+
59+
/**
60+
* Method called from code to apply a custom {@link TextStyle}.
61+
*
62+
* @param textView the {@link TextView} to have the {@link TextStyle} applied
63+
* @param textStyle the {@link TextStyle} to be applied
64+
*/
65+
public void applyTypeface(TextView textView, TextStyle textStyle) {
66+
final Typeface typeface = getTypeface(textView.getContext(), textStyle);
67+
if (typeface != null) {
68+
textView.setTypeface(typeface);
69+
}
70+
}
71+
72+
/**
73+
* Apply a {@link Typeface} for a given {@link TextStyle}.
74+
*
75+
* @param context the {@link Context} of the {@link TextView}
76+
* @param textStyle the {@link TextStyle} to be applied
77+
* @return the {@link Typeface} corresponding to the {@link TextStyle}, if defined
78+
*/
79+
private Typeface getTypeface(Context context, TextStyle textStyle) {
80+
if (textStyle == null) {
81+
throw new IllegalArgumentException("Param 'textStyle' can't be null.");
82+
}
83+
if (mTypefaces.containsKey(textStyle)) {
84+
return mTypefaces.get(textStyle);
85+
}
86+
87+
final Typeface typeface = Typeface.createFromAsset(context.getAssets(), textStyle.getFontName());
88+
if (typeface == null) {
89+
throw new RuntimeException("Can't create Typeface for font '" + textStyle.getFontName() + "'");
90+
}
91+
92+
mTypefaces.put(textStyle, typeface);
93+
return typeface;
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.codeandmagic.android;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.widget.TextView;
6+
7+
/**
8+
* A subclass of {@link TextView} which has a custom {@link TextStyle} applied.
9+
*/
10+
public class TypefaceTextView extends TextView {
11+
12+
public TypefaceTextView(Context context, AttributeSet attrs) {
13+
super(context, attrs);
14+
TypefaceManager.getInstance().applyTypeface(this, context, attrs);
15+
}
16+
17+
public void setTextStyle(TextStyle textStyle) {
18+
TypefaceManager.getInstance().applyTypeface(this, textStyle);
19+
}
20+
}
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:id="@+id/container"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context="org.codeandmagic.android.MainActivity"
7+
tools:ignore="MergeRootFrame" />

0 commit comments

Comments
 (0)