I want to know if there is a way to set the default phone font to my app. At this point my app reads the font from my LG G2 (purewhite font) and sets it only on dialogs, snackbars and toasts not in textviews or spinners. How can I pass the font to textview and spinners?
There are several ways to do this.
From this answer, you can create custom views such as:
public class YourTextView extends TextView {
public YourTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public YourTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public YourTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fonts/helveticaneue.ttf");
setTypeface(tf);
}
}
Then you can simply use this textview in your layouts instead. The main benefit is that you don't have to specify fonts for them individually.
If you do want to specify fonts individually then:
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
tv.setTypeface(face);
If you'd rather use a library, check out Calligraphy, which does all this and more.
ok, i found the solution:
i replaced
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
with
<style name="AppTheme" parent="android:Theme.DeviceDefault.NoActionBar">
in my "styles.xml"
and added
app:backgroundTint="#color/colorPrimary"
app:rippleColor="#color/colorPrimary"
in my floating button code (android.support.design.widget.FloatingActionButton)
Related
We have an old custom navigation bar in our tablet code that I am trying to convert to Jetpack Compose.
I have the scaffold set up and it appears on the app. I'm now trying to use the stored resources to display them on my new navigation bar.
The problem is that I can't seem to reference any of these resources when they come from the StyledAttributes.
For example, I am just trying to get the string for the Title of the app to appear and here is the existing code (that will eventually be replaced):
NavigationBar
public class NavigationBar extends RelativeLayout {
...
public NavigationBar(Context context) {
this(context, null, R.attr.navigationBarStyle);
}
public NavigationBar(Context context, AttributeSet attrs) {
this(new ContextThemeWrapper(context, R.style.Material_ActionBar_Light), attrs, R.attr.navigationBarStyle);
}
public NavigationBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = this.getContext().obtainStyledAttributes(attrs, R.styleable.NavigationBar, defStyle, 0);
Drawable iconId = a.getDrawable(R.styleable.NavigationBar_navIcon);
String title = a.getString(R.styleable.NavigationBar_navTitle);
a.recycle();
init(context, iconId, title);
}
...
Is there a good way to reference the Styled Attributes to obtain the icons and strings?
It's especially tricky when using this to reference the NavigationBar because the NavigationBar is the part I'm trying to get rid of.
I have a custom extended TextView I am using for custom fonts within my application, but for some reason I keep getting a run-time exception where the program can't find the font in question.
The format of the directory I'm using is main > assets > fonts > Portrait-Light.ttf
I've looked everywhere for a solution but they all seem to be rounding to the same answers on SO.
CustomFontTextView.java :
public class CustomFontTextView extends TextView {
public CustomFontTextView(Context context) {
super(context);
applyCustomFont(context);
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
Log.e("it gets here", "custom font");
Typeface customFont = FontCache.getTypeface("Roboto-Italic.ttf", context);
setTypeface(customFont);
}
}
FontCache.java
class FontCache {
private static HashMap<String, Typeface> fontCache = new HashMap<>();
static Typeface getTypeface(String fontname, Context context) {
Typeface typeface = fontCache.get(fontname);
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontname);
} catch (Exception e) {
Log.e("failed", e.getMessage());
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}
XML:
<icn.premierandroid.misc.CustomFontTextView
android:id="#+id/switch_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textColor="#color/colorPrimary"
android:gravity="center_horizontal"
android:text="#string/are_you_over_18_years_old"/>
I have tried this with different formats such as .otf and with different fonts such as Roboto-Italic.ttf and another random one from dafont.com called Sunset-Clouds.ttf but still i get the error message, what is going on? this should be working. I've even updated all plugins such as Gradle, Grade-Wrapper distribution and Android gradle plugin just in case.
I have also tried the single way of doing it:
AssetManager am = context. getApplicationContext(). getAssets();
Typeface font = Typeface.createFromAsset(
am, String.format(Locale.US, "fonts/%s", "portrait-light.ttf"));
Am i missing something?
Update:
Removal of the catch reveals this stacktrace.
Process: icn.premierandroid, PID: 3829
java.lang.RuntimeException: Unable to start activity ComponentInfo{icn.premierandroid/icn.premierandroid.RegisterActivity}:
android.view.InflateException: Binary XML file line #100: Error
inflating class icn.premierandroid.misc.CustomFontTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
.....
Caused by: android.view.InflateException: Binary XML file line #100:
Error inflating class icn.premierandroid.misc.CustomFontTextView
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
.....
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
....
Caused by: java.lang.RuntimeException: Font asset not found
fonts/GleegooRegular.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:272)
at icn.premierandroid.misc.FontCache.getTypeface(FontCache.java:21)
at icn.premierandroid.misc.CustomFontTextView.applyCustomFont(CustomFontTextView.java:28)
at icn.premierandroid.misc.CustomFontTextView.(CustomFontTextView.java:18)
at java.lang.reflect.Constructor.newInstance(Native Method)
.....
UPDATE 2:
Okay, so there is a weird work-around for this. Apparently android studio doesn't like it when you add the assets folder straight into the main folder inside app/src/main. You need to add it into the app/src/main/res folder first and then move it to the main folder. Don't have a clue why it's like this but it solved my problem.
I think the real problem is that you haven't configured your assets folder. If you had, it would look like this in Project view:
Or like this in Android view:
So you should simply add this folder as assets folder in Studio:
Click on your project structure window (or press Alt+1), then press Alt + Insert and select Folder/Assets Folder. Then put your fonts there.
Update from OP:
Okay, so there is a weird work-around for this. Apparently android studio doesn't like it when you add the assets folder straight into the main folder inside app/src/main. You need to add it into the app/src/main/res folder first and then move it to the main folder. Don't have a clue why it's like this but it solved my problem.
We wrote this class for our needs. It allows to set our custom fonts using attribute, like this:
app:customFont="Roboto-Medium.ttf"
Hope you find it useful:
public class TextViewFonted extends TextView {
private static final String TAG = "TextViewFonted";
public TextViewFonted(Context context) {
super(context);
}
public TextViewFonted(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewFonted(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewFonted);
String customFont = a.getString(R.styleable.TextViewFonted_customFont);
if (customFont == null) customFont = "Roboto-Regular.ttf";
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface", e);
return false;
}
setTypeface(tf);
setLineSpacing(getTextSize() * 0.3f, 0.75f);
return true;
}
}
And to be able to use this custom attribute, you should add to your attrs.xml next block (so R.styleable.TextViewFonted_customFont will work):
<declare-styleable name="TextViewFonted">
<attr name="customFont" format="string"/>
</declare-styleable>
In our case, we put fonts in the root of assets folder, but you could change this behavior in method setCustomFont()
Update
To add this font, for example, we use
app:customFont="Comfortaa-Bold.ttf"
Try this....
put .ttf fonts in app > assets > fonts > Roboto-Italic.ttf
And then in onCreate method,
Typeface Roboto = Typeface.createFromAsset(getAssets(),
"fonts/Roboto-Italic.ttf");
and set font in textview,etc
textview.setTypeface(Roboto);
For instance I have a custom button and want to connect it to a SeekBar:
public class SeekBarButton extends ImageButton {
SeekBar seekBar;
public SeekBarButton(Context context) {
super(context);
}
public SeekBarButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SeekBarButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setSeekBar(SeekBar seekBar) {
this.seekBar = seekBar;
}
public SeekBar getSeekBar() {
return seekBar;
}
}
I can do it in the code:
sbb = (SeekBarButton) rootView.findViewById(R.id.minus_red);
sbRed = (SeekBar) rootView.findViewById(R.id.sbRed);
sbb.setSeekBar(sbRed);
But 8 buttons will give a lot of boilerplate, and I want something like:
<com.whatever.views.SeekBarButton
...
whatToPutHere:seekbar="#+id/sbRed" // like this? whatToPutHere?
android:id="#+id/minus_red" />
<SeekBar
android:id="#+id/sbRed"
... />
The easiest way to do this is to create a custom ViewGroup that contains both the Button and Seekbar. If you cannot do that, for any reason, here's a solution:
There are a few steps to make this work. First you must define a custom XML attribute that you can then reference and use.
Edit (or create) res/values/attrs.xml. Add:
<declare-styleable name="SeekBarButton">
<attr name="seekbarId" format="integer" />
</declare-styleable>
Then, in SeekBarButton, call this from the constructors:
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SeekBarButton, defStyleAttr, 0);
mSeekbarId = a.getResourceId(R.styleable.SeekBarButton_seekbarId, 0);
a.recycle();
}
}
Finally, in your root ViewGroup of your layout file, add
xmlns:app="http://schemas.android.com/apk/res-auto"
Then,
<com.whatever.views.SeekBarButton
android:id="#+id/minus_red"
app:seekbarId="#+id/sbRed"
... />
<SeekBar
android:id="#+id/sbRed"
... />
Note You will need to call ((ViewGroup) getParent()).findViewById(mSeekbarId) in SeekBarButton to instantiate the SeekBar, but getParent() will be null in SeekBarButton constructors. So, delay findViewById() until you need the SeekBar.
I think you are close. In the first XML tag of your layout file (my example is a RelativeLayout) you need this reference to "custom":
<RelativeLayout
xmlns:custom="http://schemas.android.com/apk/res-auto">
Then farther down wherever your custom ImageButton is, you need this:
<com.whatever.views.SeekBarButton
...
custom:seekbar="#+id/sbRed"
android:id="#+id/minus_red" />
You will also need a seekBarButton.xml file in your project\res\values folder, if you didn't already know that.
I am new to android and would like some assistance on rotating my relative layout for a two-player game I am working on. I have seen the posts here and have looked into many other posts on the same subject on SO. My first question is how to I call the new class I just made? For all the posts that dont just tell me to use android:rotation (which is not avaliable in 2.3-) I make a new class, but I get a notification that the class is never called. Do I call the class simmilar to how I call a method? Or is there some command in AndroidManifest? Finialy, how does the new class rotate only one Relative layout and not the other? I just would like to know how its supposted to work.
This is the new Class that I am supposted to make:
public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyRelativeLayout(Context context) {
super(context);
init();
}
private void init() {
setStaticTransformationsEnabled(true);
}
#Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
t.setTransformationType(Transformation.TYPE_MATRIX);
Matrix m = t.getMatrix();
m.reset();
m.postRotate(180, child.getWidth() / 2.0f, child.getHeight() / 2.0f);
return true;
Thanks for your help
In the oncreate() method in your activity do setContentView(new MyRelativeLayout(this))
I need to show Hindi and Bengali font support in my app. How to do this.Please help.
Also tell me how can I get the support from XML, as I need to get the data through web-services.
What I did is that is put a particular font of choice to assets folder and used this code:
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initFont();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initFont();
}
public CustomTextView(Context context) {
super(context);
initFont();
}
private void initFont() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"myfont.ttf");
setTypeface(tf);
}
}
But the problem what I am facing is that the even if I am getting the fonts in my emulator some of the fonts patterns are not correct.
Reply if anybody wants to add more to my answer, you are welcome.