Setting ActionMenuView font family/typeface - android

I am currently working on an application on Xamarin & I am trying to set the ActionMenuView's font family (on the navigation bar), but I have found nothing to set the font family on this object.
Moreover, I wanted to set the title's font family & I made a custom renderer that is working perfectly. So I tried the same for the ActionMenuView but it does not have any typeface/font-family attribute on it.
My custom renderer (working) :
private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
{
// here I can pass on the ActionMenuView object but it does not have any typeface property?
if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
{
Android.Support.V7.Widget.AppCompatTextView AppCompatTextView = ((Android.Support.V7.Widget.AppCompatTextView)e.Child);
AppCompatTextView.SetTypeface(Typeface.CreateFromAsset(Context.ApplicationContext.Assets, "MontserratRegular.otf"), TypefaceStyle.Normal);
_toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
}
}

You can try to create style:
<style name="Style_font">
<item name="fontFamily">#fonts/PTSerif-Bold.ttf</item>
</style>
Then use this style in Menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
In MainActivity.cs:
Android.Support.V7.Widget.ActionMenuView bottombar =FindViewById<Android.Support.V7.Widget.ActionMenuView>(Resource.Id.toolbar_bottom);
IMenu bottomMenu = bottombar.Menu;
MenuInflater.Inflate(Resource.Drawable.menu_bottom_view_pager,bottomMenu);
I don't test, but you can try

Related

What is default background for AppCompatEditText?

I want to know what is default background of AppCompatEditText because I want to use its as default background of my selector state in xml but now I still not know what is it default backgrounf of this view.
This is what I currently did
private val defaultBackground = background
......
override fun setEnabled(enabled: Boolean) {
if(enabled) this.background = defaultBackground
else this.background = null
}
The default style for an AppCompatEditText is Widget.AppCompat.EditText.
Navigating through the styles you can find that the background is defined with:
<item name="android:background">?attr/editTextBackground</item>
Looking for this attribute in the appcompat theme you can find:
<item name="editTextBackground">#drawable/abc_edit_text_material</item>

How to add a scrollview edge color effect in Android Lollipop?

In my app I change the overscroll glow effect color like this:
int glowDrawableId = contexto.getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = contexto.getResources().getDrawable(glowDrawableId);
assert androidGlow != null;
androidGlow.setColorFilter(getResources().getColor(R.color.MyColor), PorterDuff.Mode.SRC_ATOP);
But when i updated to lollipop this code crashes. I get following error code:
FATAL EXCEPTION: main
Process: com.myproject.myapp, PID: 954
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getDrawable(Resources.java:756)
at android.content.res.Resources.getDrawable(Resources.java:724)
Seems that overscroll_glow resource is missing in lollipop.
How can I achieve this?
You can specify android:colorEdgeEffect in your theme to change the overscroll glow color within your entire app. By default, this inherits the primary color value set by android:colorPrimary.
res/values/themes.xml:
<style name="MyAppTheme" parent="...">
...
<item name="android:colorEdgeEffect">#color/my_color</item>
</style>
Alternatively, you can modify this value for a single view using an inline theme overlay.
res/values/themes.xml:
<!-- Note that there is no parent style or additional attributes specified. -->
<style name="MyEdgeOverlayTheme">
<item name="android:colorEdgeEffect">#color/my_color</item>
</style>
res/layout/my_layout.xml:
<ListView
...
android:theme="#style/MyEdgeOverlayTheme" />
The "android:colorEdgeEffect" solution works perfectly, and is much better than the previous hacks. However, it cannot be used if the edge color needs to be changed prorgrammatically.
It is possible, though, to use reflection to do so, setting the EdgeEffect objects directly in the AbsListView or ScrollView instances. For example:
EdgeEffect edgeEffectTop = new EdgeEffect(this);
edgeEffectTop.setColor(Color.RED);
EdgeEffect edgeEffectBottom = new EdgeEffect(this);
edgeEffectBottom.setColor(Color.RED);
try {
Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
f1.setAccessible(true);
f1.set(listView, edgeEffectTop);
Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
f2.setAccessible(true);
f2.set(listView, edgeEffectBottom);
} catch (Exception e) {
e.printStackTrace();
}
EdgeEffect.setColor() was added in Lollipop.
Same caveats as any reflection-based solution, though.
In lollipop the overscroll effect color can be customized with the item style colorPrimary :
<style name="MyApp" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/mycolor</item>
</style>
This item also affect the color of the toolbar.
I'm using this to change the edge color programmatically on android L. This works for both listView and scrollView, and views extend them.
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void setEdgeEffectL(View scrollableView, int color) {
final String[] edgeGlows = {"mEdgeGlowTop", "mEdgeGlowBottom", "mEdgeGlowLeft", "mEdgeGlowRight"};
for (String edgeGlow : edgeGlows) {
Class<?> clazz = scrollableView.getClass();
while (clazz != null) {
try {
final Field edgeGlowField = clazz.getDeclaredField(edgeGlow);
edgeGlowField.setAccessible(true);
final EdgeEffect edgeEffect = (EdgeEffect) edgeGlowField.get(scrollableView);
edgeEffect.setColor(color);
break;
} catch (Exception e) {
clazz = clazz.getSuperclass();
}
}
}
}
overscroll_glow.png doesn't exist in platform 21. You can copy the resourses from platform 20 and use them.
You can find overscroll_glow.png in:
{SDK_FOLDER}\platforms\android-20\data\res
This way you don't use reflection that can, as you can see, mess with your program after some updates.
i know i am too late, but this works for me for my app api >=17:
<style name="ListTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimaryDark</item>
</style>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ListTheme"/>

Setting custom font to all TextViews

Is there an easy way to make all textviews (and any other text elements in my app) to use a custom font of my own (and not the built-in choices), without having to manually set them via textview.setTypeface()?
I guess extending Textview would do the trick but then building interfaces with the visual editor is kind of a pain. I was thinking of something like styling but can't find how to set a custom font there.
If you need to set one font for all TextViews in android application you can use this solution. It will override ALL TextView's typefaces, includes action bar and other standard components, but EditText's password font won't be overriden.
MyApp.java
public class MyApp extends Application {
#Override
public void onCreate()
{
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf");
}
}
TypefaceUtil.java
public class TypefaceUtil {
/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
* #param context to work with assets
* #param defaultFontNameToOverride for example "monospace"
* #param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
try {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e("TypefaceUtil","Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="#android:style/Theme.Holo.Light">
<!-- you should set typeface which you want to override with TypefaceUtil -->
<item name="android:typeface">serif</item>
</style>
</resources>
Update for Android 5.0 or greater
As i have investigated and tested on device running api 5.0 or greater this solution is working fine because i am using the single style.xml file and not making other style.xml in values-21 folder
Although
Some users asking me that this solution not working with android 5.0 device (they may be using values-21 style.xml i guess)
So that is because Under API 21, most of the text styles include fontFamily setting, like
<item name="fontFamily">#string/font_family_body_1_material</item>
Which applys the default Roboto Regular font:
<string name="font_family_body_1_material">sans-serif</string>
So the best solution is
If Any one having problem not working for 5.0+. Don't override typeface in your values-v21 styles.
Just override font in values/style.xml and you will good to go :)
You can create a method to set typeface for a textview in a common class and you can set the call that method and send the textview as its attribute.
Yes. Just make a style and set it to a certain font and then set the entire app in that style.
You can do it by creating subclass of TextView and call setTypeFace method within it. For example in constructor.
Make a method in Constants class so that it can be accessed from all fragments and activities:
public static void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf"));
} else if (v instanceof EditText) {
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf"));
} else if (v instanceof Button) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf"));
}
} catch (Exception e) {
}
}
Call Method in activity as:
Constants.overrideFonts(MainActivity.this, getWindow().getDecorView().getRootView());
Call method in Fragment as:
Constants.overrideFonts(getActivity(), view);

Android : Crouton lib and custom font

I use custom fonts in my app so i want a custom font for Crouton. I 've tried to do it with setTextAppearance, it doesn't work.
<?xml version="1.0" encoding="utf-8"?>
<com.ecab.ui.custom.TextViewCustomFont
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.crouton"
android:id="#+id/crouton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ban_confirmation"
android:gravity="center"
android:text="TEST"
android:textColor="#android:color/white"
custom:typeface="gothamBold" />
In Style class :
INFOCUSTOM = new Builder().setDuration(3000).setTextAppearance(R.id.crouton).build();
Then, I've tried to do it by changing setTypeface() with my font, it doesn't work.
In Crouton class :
private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
text.setId(TEXT_ID);
text.setText(this.text);
text.setTypeface(MyFonts.getGothamBookBold(this.activity));
Log.d(Constants.D_TAG, "chaneg the typeFace");
text.setGravity(this.style.gravity);
// set the text color if set
if (this.style.textColorResourceId != 0) {
text.setTextColor(resources.getColor(this.style.textColorResourceId));
}
// Set the text size. If the user has set a text size and text
// appearance, the text size in the text appearance
// will override this.
if (this.style.textSize != 0) {
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, this.style.textSize);
}
// Setup the shadow if requested
if (this.style.textShadowColorResId != 0) {
initializeTextViewShadow(resources, text);
}
// Set the text appearance
if (this.style.textAppearanceResId != 0) {
text.setTextAppearance(this.activity, this.style.textAppearanceResId);
}
return text;
}
What can i do to have a custom Font ?
ps : library version ==> 1.7
Okay, I found the problem !
It works with the second solution by changing the Typeface. I had just forget to remove the
setTextAppearance(R.id.crouton)
in the Style class. So my custom style is like this :
INFOCUSTOM = new Builder().setDuration(3000).setBackgroundDrawable(R.drawable.ban_confirmation).setHeight(LayoutParams.WRAP_CONTENT)
.build();
One problem resolves, another arrives :) ! With the background drawable, the text is not vertically center
You can a custom Style that uses the resourceId of your text
appearance via Style.Builder.setTextAppearance(...).
This takes a reference from your styles.xml and uses it within the
internal TextView of the Crouton.
Then you can call Crouton.makeText or Crouton.showText with your
custom Style.
Source
How does MyFonts.getGothamBookBold() look like?
This however should work:
private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
text.setId(TEXT_ID);
text.setText(this.text);
Typeface myTypeFace = Typeface.createFromAsset(this.activity.getAssets(), "gothamBold.ttf");
text.setTypeface(myTypeFace);
text.setGravity(this.style.gravity);
// set the text color if set
if (this.style.textColorResourceId != 0) {
text.setTextColor(resources.getColor(this.style.textColorResourceId));
}

How to change textcolor of switch in Android

I'm creating an application which uses Android 4.0.
I'm wondering if it is possible to change the text color of the text in a switch.
I've tried setting the text color, but it doesn't work.
Any ideas?
Thanks in advance!
You must use android:switchTextAppearance attribute, eg:
android:switchTextAppearance="#style/SwitchTextAppearance"
and in styles:
<style name="SwitchTextAppearance" parent="#android:style/TextAppearance.Holo.Small">
<item name="android:textColor">#color/my_switch_color</item>
</style>
you can also do it in code, also using above styles:
mySwitch.setSwitchTextAppearance(getActivity(), R.style.SwitchTextAppearance);
...and as for setTextColor and Switch - this color will be used if your SwitchTextAppearance style doesn't provide a textColor
you can check it in Switch source code in setSwitchTextAppearance:
ColorStateList colors;
int ts;
colors = appearance.getColorStateList(com.android.internal.R.styleable.
TextAppearance_textColor);
if (colors != null) {
mTextColors = colors;
} else {
// If no color set in TextAppearance, default to the view's textColor
mTextColors = getTextColors();
}
ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
TextAppearance_textSize, 0);
if (ts != 0) {
if (ts != mTextPaint.getTextSize()) {
mTextPaint.setTextSize(ts);
requestLayout();
}
}
I think you have to look at the theme which you are using for your application. Because the color of the switch is the responsibility of the theme, afaik. So I would suggest you have a look on how you can change the settings of a theme. Or you could create a custom theme with the new colors.
TextView.setTextColor() takes an int representing the color (eg. 0xFFF5DC49) not the resource id from the xml file. In an activity, you can do something like:
textView1.setTextColor(getResources().getColor(R.color.mycolor))
outside of an activity you'll need a Context eg.
textView1.setTextColor(context.getResources().getColor(R.color.mycolor))
For more refer this

Categories

Resources