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));
}
Related
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
When I write text in entry view in xamarin.forms I alway have underline under the text
I search all the web for a solution - it was to change the background to null or transparent BUT its not working.
is there any other solution ?
I know this is an old question but it seems to get viewed/asked a lot, and Xamarin still hasn't added built-in support for customizing this very basic UI feature, so I will post an answer here that will hopefully be helpful. Also most answers you will find online (including the Microsoft documentation) show you how to create a static custom renderer, but what we really want is a control where we can set properties in shared code like with any other control. I figured out how to do this so I will share here. I've also included a property for setting border color and width since this is a common UI element we like to set.
In your shared project create a class called CustomEntry (you can rename this later if you wish).
using Xamarin.Forms;
namespace CustomizedControl
{
public class CustomEntry : Entry
{
public Color BorderColor { get; set; }
public int BorderThickness { get; set; }
public bool HasUnderline { get; set; }
}
}
In your Android project create a class called CustomEntryAndroid and paste in this code:
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using CustomizedControl;
using CustomizedControl.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryAndroid))]
namespace CustomizedControl.Droid
{
public class CustomEntryAndroid : EntryRenderer
{
public CustomEntryAndroid(Context context) : base(context)
{ }
private bool HasUnderline;
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var element = (CustomEntry)Element;
HasUnderline = element.HasUnderline;
var BorderClr = element.BorderColor.ToAndroid();
if (HasUnderline == false)
{
GradientDrawable gd = new GradientDrawable();
gd.SetColor(Android.Graphics.Color.Transparent);
Control.SetBackgroundDrawable(gd); //this is depreciated but it doesn't matter, the new method SetBackgroud simply calls SetBackgroundDrawable
} //Else maintain default underline
if (BorderClr != Android.Graphics.Color.Transparent)
{
int borderThickness = element.BorderThickness;
if (borderThickness == 0) { borderThickness = 1; } //in case border thickness was not set then default to 1
var brdr = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());
brdr.Paint.Color = BorderClr;
brdr.Paint.SetStyle(Paint.Style.Stroke);
Control.Background = brdr;
GradientDrawable gd = new GradientDrawable();
gd.SetColor(Android.Graphics.Color.Transparent);
gd.SetStroke(borderThickness, BorderClr);
Control.SetBackground(gd);
}
}//end if
}//end OnElementChanged
}//end public class CustomEntryAndroid
}//end NameSpace
You can now use the custom entry in any xaml page and get rid of the underline like this:
<local:CustomEntry HasUnderline="False" />
Here is a very simple content page with pretty much nothing on it but an entry:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CustomizedControl"
x:Class="CustomizedControl.MainPage">
<StackLayout Padding="30">
<local:CustomEntry Text="Test text" WidthRequest="250" HorizontalOptions="Start" HasUnderline="False" />
</StackLayout>
</ContentPage>
Note the import statement xmlns:local="clr-namespace:CustomizedControl". Here is the output:
Now with the underline attribute set to true:
<local:CustomEntry Text="Test text" WidthRequest="250" HorizontalOptions="Start" HasUnderline="True" />
And finally with a border:
<local:CustomEntry Text="Test text" WidthRequest="250" HorizontalOptions="Start" BorderColor="Purple" />
Notice I did not specify a width in the xaml but the border still appeared. This is because I set the default width to 1 in the Android custom renderer. But you can set the width thicker like this BorderThickness="6" or modify the default behavior if you wanted to.
One final note: the namespace I used for this project is "CustomizedControl", so you will of course need to replace "CustomizedControl" with whatever your namespace is.
Take a look here
http://geeks.ms/xamarinteam/2015/04/20/branding-a-xamarin-forms-app-on-android-accent-color/
Your ask was a little obscure, but I think the link above can help you ... you just need to set the underline color for all status as same to the background or transparent.
in Android 5.0 devices, which use the specified accent color you can just change the color, another hand, in androids 4.< you need to create an image
I can't change color of the u23F8 (pause symbol).
play symbol looks ok and I can change its color
playPauseButton = new Button(mContext);
playPauseButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 72);
playPauseButton.setTextColor(Color.WHITE);
...
if (mPlayer.isPlaying()) {
playPauseButton.setText("\u23F8");
} else {
playPauseButton.setText("\u25B6")
}
The TextView font renders \u23F8 and \u25B6 as an emoji, which means it basically uses a predefined image, so font colors are ignored on these.
I have an Api class in my application. Inside the Api class there is a custom font that has been setup as static. For example:
public static Typeface fontShort;
public Api(Context c, Display d) {
// I want to change this if user wants to keep using system font style or my custom style
if (shouldKeepCurrent == true) {
// Use system default font
fontTitle = // ???
} else {
// Use my costume font
fontTitle = Typeface.createFromAsset(context.getAssets(), "fonts/custom.ttf");
}
}
I want to get default and current Typeface of device if user doesn't want to use my custom font!
In Activity class:
TextView myView = (TextView) findViewById(R.id.myView);
// Change to custom font style or keep current font style
myView.setTypeface(Api.fontTitle);
Any ideas?
You can get the default Typeface using:
if (keep_curren) {
font_title = Typeface.DEFAULT;
}
You can also get the default Typeface based on specified style: Typeface.defaultFromStyle(int style).
More on this: Here.
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