androidlayoutheaderandroid-contextmenu

Unable to apply custom header to context menu(android)


HI I am developing small android application. In my application I am showing one context menu. every thing is working fine only problem is that I am not able to set header view to my context menu. My code looks like

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    super.onCreateContextMenu(menu, v, menuInfo);
    LayoutInflater headerInflater = (LayoutInflater) getSherlockActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ViewGroup header = (ViewGroup) headerInflater.inflate(
            R.layout.context_menu_header, null);

    menu.setHeaderView(header);
    menu.setHeaderTitle("Edit Profile Pic");

    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.menu_camera, menu);
}

and my layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 <ImageView
        android:id="@+id/context_menu_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/actionbar" 
        />
</LinearLayout>

It's not taking that layout file as header view... I also read this Applying a custom header for ContextMenu

But that's not working for me.. Need help thank you....


Solution

  • If I set header title after set header view it's not applying that view. Insted of that set header title in xml view itself and set title when u r applying header view. like in following manner..

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    
    super.onCreateContextMenu(menu, v, menuInfo);
    LayoutInflater headerInflater = (LayoutInflater) getSherlockActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    ViewGroup header = (ViewGroup) headerInflater.inflate(
            R.layout.context_menu_header, null);
    
       // menu.setHeaderView(header);
      TextView title = (TextView) header
                    .findViewById(R.id.header_textView);
            title.setText("Edit Profile Pic");
        menu.setHeaderView(header);
    //menu.setHeaderTitle("Edit Profile Pic");
    
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.menu_camera, menu);
    }
    

    and it will work fine. Its same as this question Applying a custom header for ContextMenu.

    Thank you...