-1

I'm new in android... I want to have custom font in my android project, so I put my code base on this tutorials. I want to use my custom font in R.id.Headline and some xml element

My question is:

-Is there something wrong in my code?

-Is there any tutorials to build custom UI from xml?

Here's my code

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.concurrent.ExecutionException;

    import android.app.ListActivity;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;

    public class MainActivity extends ListActivity {
        private static String url = "http:some url";
        private static final String TAG_HEADLINE = "headline";
        private static final String TAG_PROVIDER = "provider";
        private static final String TAG_TYPE = "deal_type";
        private static final String TAG_BLURB = "blurb";
        private static final String TAG_FEATURED = "featured";
        private static final String TAG_URL = "url";
        private static final String TAG_PRICE = "price";
        private static final String TAG_LOCATION = "location";
        private static final String TAG_IMAGE = "image";
        private static final String TAG_OFFER = "offer";
        private static final String TAG_THUMBNAIL = "thumbnail";
        private static final String TAG_FORMATED = "formatted";
        ArrayList<HashMap<String, String>> dealArrayList = new ArrayList<HashMap<String, String>>();

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_main, menu);
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            try {
                ArrayList<HashMap<String, String>> deals = new NetraAsycn()
                        .execute(url).get();
                ListAdapter adapter = new SimpleAdapter(this, deals,
                        R.layout.list_item, new String[] { TAG_THUMBNAIL,
                                TAG_FORMATED, TAG_PROVIDER, TAG_LOCATION,
                                TAG_HEADLINE }, new int[] { R.id.thumbnail,
                                R.id.price, R.id.provider, R.id.location,
                                R.id.headline });
  Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
    TextView myTextView = (TextView)findViewById(R.id.headline);
    myTextView.setTypeface(myTypeface);

                setListAdapter(adapter);
                ListView lv = getListView();
                lv.setDividerHeight(0);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {

                e.printStackTrace();
            }

        }


        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
             if (rowView == null) {
                  LayoutInflater inflater = context.getLayoutInflater();
                  rowView = inflater.inflate(R.layout.list_item, null);
                  ViewHolder viewHolder = new ViewHolder();
                  viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
                  viewHolder.image = (ImageView) rowView
                      .findViewById(R.id.ImageView01);
                  rowView.setTag(viewHolder);
                }
            return rowView;
        }
    }
Community
  • 1
  • 1
Netra
  • 73
  • 5
  • 13

2 Answers2

0

I had the same problem a few weeks ago. I think using directories in this "createFromAsset" method is not possible. Copy your font file into your parent asset directory.

Typeface myTypeface = Typeface.createFromAsset(getAssets(), "myFont.ttf");

What is your logcat saying?

Simon Schubert
  • 2,010
  • 19
  • 34
0

Your code is ok. I also used the custom font in the same way. As:

Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/LCALLIG.TTF");          
textView.setTypeface(typeFace); 

Just check the font name is correct or not and is it properly copied in assets folder.
If yes than show your error or logcat.

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95