0

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 ?

3 Answers3

1
Typeface font = Typeface.createFromAsset(getAssets(),"yourFont.ttf");
et.setTypeFace(font);
Decoy
  • 1,598
  • 12
  • 19
1

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

Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
  • I didn't understand :( please be more specific .. how to download it >> and what to write in each if-statement .. instead of "FontFile.ttf" – Dua'a Shloul Dec 08 '14 at 14:14
  • @Dua'aShloul you can download fonts from simple google search...and write the code in step2 in your logic whenever you want to change typeface. – Krupal Shah Dec 08 '14 at 14:25
  • is it getContext() or getBaseContext() ?? because getContext() is not define !! – Dua'a Shloul Dec 08 '14 at 14:29
  • I don't know much abut that ..but ya..you can try getbaseContext() or getApplicationContext()...You can use any of them..basically, context is a handler which is nothing but current state of application/object. see this: http://stackoverflow.com/questions/3572463/what-is-context-in-android There are very well answers there..Read all of them. – Krupal Shah Dec 08 '14 at 14:31
0

You can chanage the typeface of any view like this

yourView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83