Question

How can i add/change the typeface for ContextMenu Item? Please guide me.

@Override  
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo);  
    menu.setHeaderTitle("Select");  
    menu.add(0, v.getId(), 0, "Item1");  
    menu.add(0, v.getId(), 0, "Item2");
    menu.add(0, v.getId(), 0, "Item3");  
}  

I want to change the typeface of my items using custom typeface (ttf) files.

Was it helpful?

Solution

How can i add/change the typeface for ContextMenu Item?

I don't think there is in-built support for such things. Though you can achive your goal by implementing an AlertDialog.

Create new Android project and copy+paste following files in relavant folders and run the application; then long click on TextView.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/hello" android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

context_menu_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent" android:textStyle="bold">
    <!-- specify android:typeface="" -->
</TextView>

BaseActicity.java

public class BaseActicity extends Activity {

    protected void registerForCustomContextMenu(View targetView, String title, final String[] items) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.context_menu_item, items);
        // You can also specify typeface at runtime by overriding the
        // getView(int position, View convertView, ViewGroup parent) method of ArrayAdapter
        // convertView = super.getView(int position, View convertView, ViewGroup parent)
        // TextView textView = (TextView) convertView;
        // textView.setTypeface(tf, style)
        DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                onCustomContextMenuItemSelected(items, which);
            }
        };
        builder.setAdapter(arrayAdapter, onClickListener);
        OnLongClickListener onLongClickListener = new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                builder.show();
                return true;
            }
        };
        targetView.setOnLongClickListener(onLongClickListener);
    }

    protected void onCustomContextMenuItemSelected(String[] items, int which) {
    }

}

CustomContextMenuActivity.java

public class CustomContextMenuActivity extends BaseActicity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView hello = (TextView) findViewById(R.id.hello);
        registerForContextMenu(hello);

        final String[] items = {"Red", "Green", "Blue"};
        registerForCustomContextMenu(hello, "Pick a color", items);
    }

    @Override
    protected void onCustomContextMenuItemSelected(String[] items, int which) {
        Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
    }

}

UPDATE

Could you please tell me How to specify Typoeface at run time?

Replace following code

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.context_menu_item, items);

by putting following code

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.context_menu_item, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = super.getView(position, convertView, parent);
                TextView textView = (TextView) convertView;
                Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/BENGOTHB_0.TTF");
                textView.setTypeface(typeface);
                return convertView;
            }
        }

OTHER TIPS

create fonts folder in assets and add your ttf file,after u can access your new typeface wher ever u want

Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/BENGOTHB_0.TTF");
txt.setTypeface(tf).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top