Dua'a Shloul
Dua'a Shloul

Reputation: 51

how to change EditText font family

i have to change the font-family of EditText by choose on option from a dropdown check the code below my problem is how to change the font-family by code >> and let the user to choose his font family ..

 public class Graduation extends ActionBarActivity {

ImageView imageView ;
 Spinner spinner1;
 int fontSizeFamily;
String[]f_items = { "Times New Roman ", "Arial", "sans-serif", "20", "24", "28" , "30" };
 EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.graduation);


    spinner1 = (Spinner)findViewById(R.id.spinner1);  // spinner1 exist in xml file
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
        this, android.R.layout.simple_spinner_item, f_items);
    spinner.setAdapter(adapter1);
    spinner.setOnItemSelectedListener(
              new AdapterView.OnItemSelectedListener() {
                  @Override
                  public void onItemSelected(AdapterView<?> arg0, View arg1,
                          int arg2, long arg3) {
                    int position = spinner1.getSelectedItemPosition();

                    try
                    {
                    if (position == 0 )
                        {// whats the code here ??? }
                    }
                     if (position == 1 )
                        {// whats the code here ??? }
                    }
                    catch (NumberFormatException e)
                    {
                        // whats the code here  ? 
                    }

                  }
                  @Override
                  public void onNothingSelected(AdapterView<?> arg0) {
                      // TODO Auto-generated method stub
                  }
              }
          );

so i don't know what to put in the if-statment ?

Upvotes: 0

Views: 2418

Answers (3)

Krupal Shah
Krupal Shah

Reputation: 9187

Step 1: Download font file and Put it (.ttf or .otf file) in asset/font like this (you need to create font folder in assets):

enter image description here

Step 2: Add code in your if/else logic same as mentioned by @Decoy:

Typeface f = Typeface.createFromAsset(getContext().getAssets(),"font/Name_of_Font_File.ttf");
et.setTypeFace(f);

Use getContext().getAssets()

Upvotes: 1

Decoy
Decoy

Reputation: 1597

Typeface font = Typeface.createFromAsset(getAssets(),"yourFont.ttf");
et.setTypeFace(font);

Upvotes: 1

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

You can chanage the typeface of any view like this

yourView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));

Upvotes: 0

Related Questions