Supporting custom text weight in Android - android

Is there a way to support more than standard three available text weights in Android. I need to have five: ExtraLight, Light, Normal, Medium and Bold.
I saw this question: How to change fontFamily of TextView in Android, but it related to family rather than weight.
If there is no in-build support, maybe any external resources or libraries are available?

As I am not aware of how a library could add support for your font without including it, I would advise you to create your own implementation of TextView to handle that, it can be done quite quickly, if you only need it for TextView (you would need to implement your own EditText as well if you need that widget).
You can start by looking at the RobotoTextView library which allows for the RobotoTextView widgets to be used in code as well as directly in xml, using attributes.

Related

Using style instead of android:textAppearance

android:textAppearance allows you to apply an additional layer of styling for certain attributes on text views. I'm wondering whether the flexibility of this feature outweighs its complexity.
Is android:textAppearance required for any technical or visual reason?
What might be the consequences of relying solely on style throughout a project and omitting the use of android:textAppearance in layouts?
What if it were omitted from styles too?
Here's two guides that recommend the use of android:textAppearance:
Styling Views on Android (Without Going Crazy)
TextAppearance allows you to merge two styles for some of the most
commonly modified text attributes. Take a look at all your styles: how
many of them only modify how the text looks? In those cases, you could
instead just modify the TextAppearance.
Best practices for happy Android resources
To ensure consistent-looking TextViews, do not define any of the TextAppearance attributes in a normal style, but always set a TextAppearance from your library on that style’s android:textAppearance field.
Looking at TextView.java, there don't appear to be any attributes that are defined by android:textAppearance that are not able to be set on a TextView directly. So there doesn't appear to be any technical reason that one needs to use them over defining attributes in styles directly, at least in one's ability to configure how a TextView looks.
This means that you can avoid using it in your layouts as well as your styles without any adverse effects, provided that you override all the associated attributes. The base theme defines many different standard android:textAppearances for the various widgets, so you should check that all the widgets are properly overridden.
Based on my experience, android:textAppearance is most useful if you wish your app to appear to integrate into the rest of the device. So if you need big text, you can use android:textAppearance="?android:attr/textAppearanceLarge" and now your text is large! No need to know how many sp that means for the given device/screen size/etc..
However if your app is highly styled and you'd otherwise be overriding all the text sizes anyhow, the value-add of android:textAppearance diminishes. You can certainly use it as referenced in the posts above, but if that doesn't fit into your styling system, then feel free to omit it. It's just another tool to help you get a good-looking app across all devices.
I would also recommend making extra-sure that you try your app across a variety of devices if you choose not to use it, just to make sure that you didn't neglect to override a default android:textAppearance.

Default custom font

Is it possible to set a custom font as default in a android application? Because typing this for every TextView is not efficient.
login = (TextView)findViewById(R.id.tvLogin);
login.setTypeface(titleFont);
Is it possible to set a custom font as default in a android application?
Not for a custom Typeface that you loaded from your own font file, sorry. As others have noted, there are workarounds and libraries to help simplify matters a bit. Personally, I'd look at Calligraphy or perhaps Fontify.

Getting All Views has text on Android layout for setting custom font. What is the Best Practice?

I need to get all views that have text for setting custom fonts.
I can develop a recursive method in myBaseActiviy class for getting all views with checking instanceof when programme is in OnCreate(). But I worry about the performance? I interest your idea? What should I do?
There is no best way. An approach, the one I use, is to subclass TextView, adding a new attribute to specify the font I want to use, delegating the logic to the subclass self.
I think the easiest way is to create your own TextView. It's not as hard as it sounds ;-).
This is the origional answer:
https://stackoverflow.com/a/19679639/2767703
And this is the link you'll need:
http://javatechig.com/android/using-external-fonts-in-android-view
Or if you want to set the font in the xml:
https://stackoverflow.com/a/7197867/2767703
Warning
If you are developing for Android versions lower than Android 4.0 then
you should change the code. In these versions there is a bug that
doesn't free up memory for typefaces. What you should do is create a
HashMap that allows reusage of Typefaces. You can find more in the
comments of the last link (search for the comment with the highest
upvote).
You could also use this:
https://stackoverflow.com/a/16883281/2767703
This changes the font of every text in your application. Note: You still have to look at my warning if you use this.
You can do something like this:
Add new attribute for store font in style.
Extend your view to handle this attr and set font by view when do you need.
You can find example of using and creation of new attribute for font at this link https://stackoverflow.com/a/12282315/2156937
Hope it helps.

EditText field appearence

The EditText fields in the Android emulator have an inner shadow and rounded corners. The same app on my Samsung Galaxy shows the EditText fields flat looking and perfectly rectangular.
I realize there are differences between the versions of Android but is there a way to influence these properties of EditText fields?
In most cases it is better to use the default style, this is what the user expects.
You can completely define the look of your UI fields. If you want a fully custom look, go for a 9-patch background image. You can also define a look with xml drawable resources, which allow for rounded corners and gradients.
If you define the look, it will look that way on all phones. the highly variable UI changes between manufacturers and carriers is, in my opinion, one of the most frustrating issues with Android.
Most clients we have worked with has a complete UI spec. While there is an argument for using "the default", most commercial apps do not. At least for the style stuff. I would still argue that menus, notifications, preferences, etc should be Android standard.

How can I make a Android app look pretty?

When I place things like text boxes they are fixed in one position and I can't move them around easily.
How can I edit the style and maybe put an image at the top of the text boxes and give the app a neat and pretty little layout?
Thanks
Use RelativeLayouts instead of LinearLayouts (i assume that you are using them) which allow free positioning.
To modify the theme/colors, read Applying Styles and Themes.
Create 9-patch graphics and assign them as backgrounds.
I am unsure if this is what you are looking for,but you can arrange the object on the layout from the xml files in res/layouts/ folder.Do not forget to follow the UI guidelines provided by google for android development. Also you can create custom themes for you app if you do not like the default template.

Categories

Resources