How can I support unicode fonts in Android? - java

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.

Related

Custom XML/Java Navigation Bar Convert to Jetpack Compose with `obtainStyledAttribues` to Obtain Resources

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.

How to set the default phone font to my entire app

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)

Can't find native method using JNI when extending Android component

I got a class to extend NumberPicker component to introduce min & max value:
public class ExtendedNumberPicker extends NumberPicker {
public ExtendedNumberPicker(Context context) {
super(context);
}
public ExtendedNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
processAttributeSet(attrs);
}
public ExtendedNumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
processAttributeSet(attrs);
}
private void processAttributeSet(AttributeSet attrs) {
//This method reads the parameters given in the xml file and sets the properties according to it
this.setMinValue(attrs.getAttributeIntValue(null, "min", 0));
this.setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
}
}
I put it into layout:
<com.example.myapp.component.ExtendedNumberPicker
android:id="#+id/pick_hh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
min="140"
max="200" />
In LogCat, it shows:
Tag Text
---------------------------
PropertyValuesHolder Can't find native method using JNI, use reflectionjava.lang.NoSuchMethodError: no method with name='setSelectorPaintAlpha' signature='(I)V' in class Lcom/example/myapp/component/ExtendedNumberPicker;
PropertyValuesHolder Couldn't find setter/getter for property selectorPaintAlpha with value type int
I know JNI is Java Native Interface, but I never use/see the property selectorPaintAlpha. What is it? How to resolve the issue?

Rotating a RelativeLayout in Android 2.3

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))

Strange problem overriding one of LinearLayout's constructors

For some reason, Eclipse doesn't like the call to super(context, attrs, defStyle), yet it's happy with the other super calls. The error is "The constructor LinearLayout(Context, AttributeSet, int) is undefined".
I don't think the problem is with this code itself, but something else in the project's settings or something, since I adapted almost identical code from an example that did the same thing but for a RelativeLayout, which ran fine on my Eclipse setup in a test project.
Please Help :)
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
myInit();
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
myInit();
}
public MyLinearLayout(Context context, int checkableId) {
super(context);
myInit();
}
According to these javadocs the error is completely correct.
LinearLayout does not have that constructor. These are the two valid constructors:
LinearLayout(Context context)
LinearLayout(Context context, AttributeSet attrs, Map inflateParams)
http://developer.android.com/reference/android/widget/LinearLayout.html
According to the developer site, the LinearLayout(context, attrs, defStyle) constructor is only available in API version 11, so it won't work in earlier versions.

Categories

Resources