Question

I am trying to set fonts to my slide menu listview but I dont know how do that. I try this but it did not work:

ListView lvLeft = (ListView) leftView.findViewById(R.id.lvLeft);
lvLeft.settypeface(myface);

Here is my xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_left"
    android:layout_width="180dp"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/grey21"
        android:padding="10dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/optins"
            android:textColor="@color/gold"
            android:textSize="17sp" />
    </LinearLayout>

    <com.carlos.myslidingmenu.view.COFixListViewBugLinearLayout
        android:id="@+id/mylaout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/lvLeft"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>
    </com.carlos.myslidingmenu.view.COFixListViewBugLinearLayout>

</LinearLayout>

and this a part of my main activity:

ListView lvLeft = (ListView) leftView.findViewById(R.id.lvLeft);

          //lvLeft.set
          ArrayAdapter<String> adapter1;
          adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tv_item, title);
          lvLeft.setAdapter(adapter1);
          lvLeft.setOnItemClickListener(new OnItemClickListener() {

              @SuppressWarnings("deprecation")
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                  if(arg2==0){
                      coSlidingMenu.showViewState(COSlidingState.SHOWCENTER);
                      showDialog(dialog_num);
                  } }

and my list item name is item.xml. I have searched the net but didn't find a proper answer. Any solution?

Was it helpful?

Solution

public class myadpter extends BaseAdapter {
    private ArrayList<String> listCountry;
    private Activity activity;

    public myadpter (Activity activity, ArrayList<String> listCountry)
    {
        super();
        this.listCountry = new ArrayList<String>();
        this.listCountry = listCountry;

        this.activity = activity;
        System.out.println("this is contry name " + this.listCountry);


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        System.out.println("len " + listCountry.size());
        return listCountry.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder {

        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();

            convertView = inflator.inflate(R.layout.custom_textview, null);

            view.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.list_content);

            view.txtViewTitle.setTextColor(Color.BLACK);
            // Typeface typeface =
            // Typeface.createFromAsset(getAssets(),"fonts/DroidSerif.ttf");
            view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
                    .getContext().getAssets(), "fonts/DroidSerif.ttf"));

            // view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
            convertView.setTag(view);

        }

        else {
            view = (ViewHolder) convertView.getTag();
        }
        System.gc();
        try {
            view.txtViewTitle.setText(listCountry.get(position));

        } catch (Exception e) {
            System.out.println("this is error " + e.getMessage());
        }

        return convertView;
    }

}

and for use this

mAdapter = new myadpter (this, listCountry);
        lv_diseases = (ListView) findViewById(R.id.lv_diseases);


        lv_diseases.setAdapter(mAdapter);

and

listCountry = new ArrayList<String>();
listCountry.addAll(list);

OTHER TIPS

final Typeface mFont = Typeface.createFromAsset(getAssets(), "myfont.otf"); 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewGroup view = (ViewGroup) super.getView(position, convertView, parent);
            if(convertView == null) MyActivity.setAppFont(view, mFont);
            return view;
        }
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top