I am making a text editor. I have a image button and what I want is that when I click that image button the editor font would change to a custom font given in assets folder.
Also when I run the app it unfortunately stops. Below is the java code.
String fontPath = "fonts/PORNSA_.TTF";
final Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
et = (EditText) findViewById(R.id.editText);
fontButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et.setTypeface(tf);
}
});
You want to change your font file name without Caps and "_" symbol?
Related
I want to change the button font style when it clicks how can i achieve am new in android please help
Button b1=(Button)findviewbyid(R.id.login);
how can i select from pre_installed fonts . studio 3.0 has preinstalled fonts
Get the font you need as a .ttf file and place it in your project's /assets/ directory.
Use this code to refer to the font and set it to your button:
Typeface face= Typeface.createFromAsset(getAppContext().getAssets(), "LightBlueFont.ttf");
button.setTypeface(face);
You can use this:
Button b1 = (Button) findViewById( R.id.login );
b1.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Typeface newFont = getResources().getFont(R.font.myfont);
b1.setTypeface(newFont);
}
});
I am getting the following exception when trying to set a custom font to a button: java.lang.RuntimeException: native typeface cannot be made
I already looked at similar questions but none of their solutions worked.
My font is inside src/main and the file structure seems to be fine.
Here is the file structure:
And here is the code:
private void generateButtonListener(final Button btn,final String inputText) {
btn.setTypeface(Typeface.createFromAsset(getAssets(), "rb.ttf"));
btn.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View view1) {
getCurrentInputConnection().commitText(inputText,1);
}
});
}
Not sure if it matters but I am making a custom keyboard app so I am setting this listener on buttons in the onCreateInputView method. Like this:
#Override
public View onCreateInputView() {
LayoutInflater lInflate = getLayoutInflater();
Resources res = getResources();
LinearLayout inputView = (LinearLayout) lInflate.inflate(R.layout.copypasta, null);
Button tab_navySeal = (Button) inputView.findViewById(R.id.tab_navySeal);
Button nSeal_1 = (Button) btnset_navyseal.findViewById(R.id.nSeal_1);
generateButtonListener(nSeal_1, res.getString(R.string.NStxt));
etc...
I seem to be following all the rules for fonts, what's the problem?
Are you use the fonts as a library or
btn.setTypeface(Typeface.createFromAsset(getAssets(), "rb.ttf"));
rb.ttfis the worng path? tryfonts/rb.ttf
I would like the user to be able to edit the font size and font style from within my app. The app should just call the default settings.
example:
displayBtn = (Button) findViewById(R.id.displayBtn);
displayBtn .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Settings.ACTION_DISPLAY_SETTINGS));
}
});
XML is not editable in run time so you can are going to have to set a static value for the font size and then manually set them in the applicaiton.
For example..
public static int defaultFontSize = defaultValue;
//Update the value when the user changes font..
ClassName (class where you declared the static variable).defaultFontSize = 10;
textView.setTextSize(ClassName.defaultFontSize);
I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.
I've tried this code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.
Try this
public void onClick(View v) {
textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
}
Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.
You have 2 Options:
textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());
And Second you can use SpanableStringBuilder
SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
sb.append("\n").append(editEmail.getText());
The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.
// Font path
String fontPath = "fonts/jcc.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
In Android Studio it comes up with an error, "Cannot resolve symbol 'setTypeface'" and "Unknown class: 'tf'". I do not know why, I defined 'tf' and I have looked at many tutorial's that use setTypeface.
Please Help!
Edit, here is a screenshot, I am using this exact code and my fonts are under 'assets/font/jcc.tf'.
http://i.imgur.com/fcDdVRz.png
Sorry do not have enough reputation to post images :(
After much trial and error I found the solution!
My code was after the onCreate method, once I placed it inside all the errors went away!
The code with errors:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Font path
String fontPath = "fonts/SECRCODE.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.java1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
The code without errors (notice the code is now inside onCreate method):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Font path
String fontPath = "fonts/SECRCODE.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.java1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
}
If anyone could explain why this works it would help greatly!
Thanks!
The reason you need to set the font (typeface) in the onCreate method is because your Java code in the onCreate method is automatically executed when an activity is loaded. Without putting it inside the onCreate() method there is no reference/call to execute your code.