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);
}
});
Related
How to delete the image of BackgroundResource from button?
For example: b.setBackgroundResource(R.drawable.breakdown)
I'm sure that setBackgroundResource(0) is not available!
Thank you
Put this code...
b.setBackgroundResource(null);
try this using this you will get default button style.
btn.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));
or this
b.setBackgroundResource(null);
or this
b.setBackgroundResource(0);
Try:
b.setBackground(null);
The documentation states:
Set the background to a given Drawable, or remove the background.
Also:
b.setBackgroundResource(0);
The documentation states:
Set the background to a given resource. The resource should refer to a
Drawable object or 0 to remove the background.
Edit:
Because you stated that it does not work, I wrote this to test it, and it works perfectly:
final Button btnTest = (Button) findViewById(R.id.btn_test);
btnTest.setBackgroundResource(R.drawable.some_drawable);
Button btnClean = (Button) findViewById(R.id.btn_clean);
btnClean.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnTest.setBackground(null);
}
});
I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html
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 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?
i have build some simple app which contain one textview and two button
and at programmatic i have find there id and change textsize as button click as per below.
Button btn1,btn2;
TextView txtmain;
txtmain=(TextView)findViewById(R.id.textView1);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
txtmain.setBackgroundColor(Color.YELLOW);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain.setTextSize(30);
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain.setTextSize(100);
}
});
so the output is as below when i click btn2 textsize is 100 and when i select textview size is 30 but in android os 4.0.3 output is very different from other os.
button1 click output screen
button2 click output screen
Again button1 click mismatch output as per button1 clicked image
so please help me how to solved this problem this is one simple demo. is this any os related problem because apart from android os 4.0.3 it is run perfect.
see the following link.
https://code.google.com/p/android/issues/detail?id=17343
this is the android ice cream sandwich issue .
solve this issue add extra spacing character end of the text.
example:
final String DOUBLE_BYTE_WORDJOINER = "\u2060";
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
txtmain.append(DOUBLE_BYTE_WORDJOINER);