Skip to content

Commit

Permalink
Added support for MenuItem,Add icons programmatically, Updated Readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Livin21 committed Nov 28, 2016
1 parent 269a8ef commit 9ba23db
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 7 deletions.
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 49 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# [![icon-96x96.png](https://s18.postimg.org/tboj9r74p/icon_96x96.png)](https://postimg.org/image/efq025vpx/) DroidAwesome
Custom Views that support FontAwesome directly
A library to display FontAwesome Icons in any View or a MenuItem

[ ![Download](https://api.bintray.com/packages/lmntrx-tech/DroidAwesome/droid-awesome/images/download.svg) ](https://bintray.com/lmntrx-tech/DroidAwesome/droid-awesome/_latestVersion) [ ![Download](https://jitpack.io/v/Livin21/DroidAwesome.svg "jitpack.io") ](https://jitpack.io/#Livin21/DroidAwesome/v1.1.0) [![StackShare](https://img.shields.io/badge/tech-stack-0690fa.svg?style=flat)](https://stackshare.io/Livin21/droidawesome)

Expand All @@ -15,15 +15,16 @@ Custom Views that support FontAwesome directly
* ImageView
* ImageButton
* FloatingActionButton
* MenuItem

## Screenshot ##
[![Screenshot_1480200614.png](https://s22.postimg.org/sdpaqnqyp/Screenshot_1480200614.png)](https://postimg.org/image/mpizzrmm5/) [![Screenshot_1480244308.png](https://s21.postimg.org/4mk08gn93/Screenshot_1480244308.png)](https://postimg.org/image/nrn9i81wz/)
[![Screenshot_1480330061.png](https://s16.postimg.org/63vrfifdh/Screenshot_1480330061.png)](https://postimg.org/image/hg8cxao29/) [![Screenshot_1480244308.png](https://s21.postimg.org/4mk08gn93/Screenshot_1480244308.png)](https://postimg.org/image/nrn9i81wz/)

## Gradle Setup ##
Add the following code snippet to module/build.gradle
```
dependencies {
compile 'com.lmntrx.livin.library.droidawesome:droid-awesome:1.1.0'
compile 'com.lmntrx.livin.library.droidawesome:droid-awesome:1.1.1'
}
```

Expand All @@ -33,7 +34,7 @@ dependencies {
<dependency>
<groupId>com.lmntrx.livin.library.droidawesome</groupId>
<artifactId>droid-awesome</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<type>pom</type>
</dependency>
```
Expand All @@ -45,6 +46,7 @@ dependencies {
<resources>
<string name="android_icon_font_awesome">&#xf17b;</string> //http://fontawesome.io/cheatsheet/
<string name="not_allowed_font_awesome">&#xf05e;</string>
<string name="fa_settings_gears">&#xf085;</string>
</resources>
```
* Now use custom view in activity.xml
Expand All @@ -64,11 +66,54 @@ dependencies {
app:textColor="@color/colorAccent"
app:textSize="38sp"/>
```
* Or in java file
```
DroidAwesomeTextView textView = new DroidAwesomeTextView(context);
textView.setText(getString(R.string.your_icon_string));
```
Or
```
//TextView, EditText, Button, etc.
TextView textView = new TextView(context);
textView = (TextView)DroidAwesome.setFontIcon(context,textView,context.getString(R.string.your_icon_string));
//Imageview, ImageButton, FloatingActionButton, etc.
Imageview imageView = new Imageview(context);
imageView = (Imageview)DroidAwesome.setFontIcon(context,imageView,context.getString(R.string.your_icon_string));
```
* To use with menu items
```
//XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.lmntrx.livin.droidawesome.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
/>
</menu>
//Java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_settings);
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menuItem.setIcon(DroidAwesome.getFontIcon(this,getString( R.string.fa_settings_gears)));
return true;
}
```
### Tip: For smooth rendering in android studio one might have to include [font-awesome.ttf](https://github.com/Livin21/DroidAwesome/blob/master/droid-awesome/src/main/assets/fonts/font-awesome.ttf?raw=true) in assets/fonts/ ###

## ChangeLog ##

### 1.1.1 ###
* Add FontAwesome icon programmatically
* Add FontAwesome icon to a MenuItem

### 1.1.0 ###
* Added ImageView
* Added ImageButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.view.Menu;
import android.view.MenuItem;

import com.lmntrx.livin.library.droidawesome.DroidAwesome;

public class MainActivity extends AppCompatActivity {

@Override
Expand All @@ -34,6 +36,9 @@ public void onClick(View view) {
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_settings);
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menuItem.setIcon(DroidAwesome.getFontIcon(this,getString(R.string.fa_settings_gears)));
return true;
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/icons.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="android_icon_font_awesome">&#xf17b;</string>
<string name="fa_settings_gears">&#xf085;</string>
</resources>
4 changes: 2 additions & 2 deletions droid-awesome/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.1.0"
versionName "1.1.1"

}

Expand All @@ -27,7 +27,7 @@ publish {
groupId = 'com.lmntrx.livin.library.droidawesome'
repoName = 'DroidAwesome'
artifactId = 'droid-awesome'
publishVersion = '1.1.0'
publishVersion = '1.1.1'
desc = 'Custom Views that support FontAwesome directly'
website = 'https://github.com/Livin21/DroidAwesome'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.lmntrx.livin.library.droidawesome;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/***
* Created by livin on 28/11/16.
*/

public class DroidAwesome {

/**
* @param context context passed
* @param view view instance to which icon is applied
* @param iconText FontAwesome icon text
* @return returns modified view
*/
public static View setFontIcon(Context context, View view, String iconText){
if (view instanceof TextView){
TextView textView = (TextView)view;
textView.setTypeface(FontManager.getTypeface(context));
textView.setText(iconText);
return textView;
}else if (view instanceof ImageView){
ImageView imageView = (ImageView)view;
TextDrawable textDrawable = new TextDrawable(context);
textDrawable.setText(iconText);
textDrawable.setTypeface(FontManager.getTypeface(context));
imageView.setImageDrawable(textDrawable);
return imageView;
}else return null;
}

/**
* @param context context passed
* @param view view instance to which icon is applied
* @param iconText FontAwesome icon text
* @param size icon size
* @return returns modified view
*/
public static View setFontIcon(Context context, View view, String iconText, float size){
if (view instanceof TextView){
TextView textView = (TextView)view;
textView.setTypeface(FontManager.getTypeface(context));
textView.setText(iconText);
textView.setTextSize(size);
return textView;
}else if (view instanceof ImageView){
ImageView imageView = (ImageView)view;
TextDrawable textDrawable = new TextDrawable(context);
textDrawable.setText(iconText);
textDrawable.setTypeface(FontManager.getTypeface(context));
textDrawable.setTextSize(size);
imageView.setImageDrawable(textDrawable);
return imageView;
}else return null;
}

/**
* @param context context passed
* @param view view instance to which icon is applied
* @param iconText FontAwesome icon text
* @param size icon size
* @param colorRes color resource
* @return returns modified view
*/
public static View setFontIcon(Context context, View view, String iconText, float size, int colorRes){
if (view instanceof TextView){
TextView textView = (TextView)view;
textView.setTypeface(FontManager.getTypeface(context));
textView.setText(iconText);
textView.setTextColor(colorRes);
textView.setTextSize(size);
return textView;
}else if (view instanceof ImageView){
ImageView imageView = (ImageView)view;
TextDrawable textDrawable = new TextDrawable(context);
textDrawable.setTypeface(FontManager.getTypeface(context));
textDrawable.setText(iconText);
textDrawable.setTextSize(size);
textDrawable.setTextColor(colorRes);
imageView.setImageDrawable(textDrawable);
return imageView;
}else return null;
}

/**
* @param context context passes
* @param text FontAwesome icon text
* @param size FontAwesome icon size
* @return returns drawable
*/
public static Drawable getFontIcon(Context context, String text, float size) {
TextDrawable textDrawable = new TextDrawable(context);
textDrawable.setTypeface(FontManager.getTypeface(context));
textDrawable.setText(text);
textDrawable.setTextSize(size);
return textDrawable;
}

/**
* @param context context passes
* @param text FontAwesome icon text
* @return returns drawable
*/
public static TextDrawable getFontIcon(Context context, String text) {
TextDrawable icon = new TextDrawable(context);
icon.setTypeface(FontManager.getTypeface(context));
icon.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
icon.setTextAlign(Layout.Alignment.ALIGN_CENTER);
icon.setTextColor(ContextCompat.getColor(context, android.R.color.white));
icon.setText(text);
return icon;
}

/**
* @param context context passes
* @param text FontAwesome icon text
* @param colorRes color resource
* @param size FontAwesome icon size
* @return returns drawable
*/
public static Drawable getFontIcon(Context context, String text, float size, int colorRes) {
TextDrawable textDrawable = new TextDrawable(context);
textDrawable.setTypeface(FontManager.getTypeface(context));
textDrawable.setText(text);
textDrawable.setTextSize(size);
textDrawable.setTextColor(colorRes);
return textDrawable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,53 @@
*/

public class DroidAwesomeAutoCompleteTextView extends AutoCompleteTextView {

/**
* @param context context passed
*/
public DroidAwesomeAutoCompleteTextView(Context context) {
super(context);
setTypeface(FontManager.getTypeface(context));
}

/**
* @param context context passed
* @param attrs attributes in xml
*/
public DroidAwesomeAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface(FontManager.getTypeface(context));
}

/**
* @param context context passed
* @param attrs attributes in xml
* @param defStyleAttr style attributes
*/
public DroidAwesomeAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setTypeface(FontManager.getTypeface(context));
}

/**
* @param context context passed
* @param attrs attributes in xml
* @param defStyleAttr style attributes
* @param defStyleRes style resource
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public DroidAwesomeAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setTypeface(FontManager.getTypeface(context));
}

/**
* @param context context passed
* @param attrs attributes in xml
* @param defStyleAttr style attributes
* @param defStyleRes style resource
* @param popupTheme style popUp theme resource
*/
@TargetApi(Build.VERSION_CODES.N)
public DroidAwesomeAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, Resources.Theme popupTheme) {
super(context, attrs, defStyleAttr, defStyleRes, popupTheme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,40 @@
*/

public class DroidAwesomeButton extends Button {

/**
* @param context context passed
*/
public DroidAwesomeButton(Context context) {
super(context);
setTypeface(FontManager.getTypeface(context));
}

/**
* @param context context passed
* @param attrs attributes in xml
*/
public DroidAwesomeButton(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface(FontManager.getTypeface(context));
}

/**
* @param context context passed
* @param attrs attributes in xml
* @param defStyleAttr style attributes
*/
public DroidAwesomeButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setTypeface(FontManager.getTypeface(context));
}

/**
* @param context context passed
* @param attrs attributes in xml
* @param defStyleAttr style attributes
* @param defStyleRes style resource
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public DroidAwesomeButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
Expand Down
Loading

0 comments on commit 9ba23db

Please sign in to comment.