1

I have a fragment from which i'd like to load a custom view in the actionbar, so i do the following.

In the form activity:

public class FormActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_form);

        try {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

            fragmentTransaction.replace(R.id.content_frame, new FormFragment()).addToBackStack(null).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

In the Fragment itself:

public class FormFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View actionBarButtons = inflater.inflate(R.layout.form_custom_actionbar,
                new LinearLayout(getActivity()), false);
        View cancelActionView = actionBarButtons.findViewById(R.id.action_cancel);
        cancelActionView.setOnClickListener(this);
        View doneActionView = actionBarButtons.findViewById(R.id.action_done);
        doneActionView.setOnClickListener(this);

        ActionBar ab = ((ActionBarActivity) getActivity()).getSupportActionBar();
        ab.setCustomView(actionBarButtons);

        return inflater.inflate(R.layout.fragment_form, null);
    }
...

The CustomActionBar res layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:divider="?android:attr/dividerVertical"
    android:dividerPadding="12dip"
    android:showDividers="middle">

    <!-- id must match corresponding menu item id -->
    <LinearLayout
        android:id="@+id/action_cancel"
        style="@style/FormCustomActionButton">

        <ImageView
            android:src="@drawable/ic_action_remove"
            style="@style/FormCustomActionButtonImage" />
        <TextView
            android:text="@string/discard_label"
            style="@style/FormCustomActionButtonText"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <!-- id must match corresponding menu item id -->
    <LinearLayout
        android:id="@+id/action_done"
        style="@style/FormCustomActionButton">

        <ImageView
            android:src="@drawable/abc_ic_cab_done_holo_light"
            style="@style/FormCustomActionButtonImage" />
        <TextView
            android:text="@string/save_label"
            style="@style/FormCustomActionButtonText"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>


</LinearLayout>

The fragment loads fine, but i do not get the custom action bar buttons on top. What am i missing ?

spacebiker
  • 3,777
  • 4
  • 30
  • 49
  • 1
    http://stackoverflow.com/questions/17521745/manually-inflating-custom-view-yields-different-layouts-for-actionbar-custom-vie. check this if it helps – Raghunandan Nov 26 '13 at 13:15
  • Yes, that helped!! i added `actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);`and `actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);` and now it works ! – spacebiker Nov 26 '13 at 13:30
  • Please reply the question and i will mark it as accepted, the problem was that i set NAVIGATION_MODE_LIST in the main activity, i needed to set it to STANDARD, also set the flag DISPLAY_SHOW_CUSTOM – spacebiker Nov 26 '13 at 13:56
  • i posted it as an answer for the benefit of others – Raghunandan Nov 26 '13 at 13:58

1 Answers1

2

Converting comment to answer

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)

Also check this link

Manually inflating custom view yields different layouts for ActionBar custom view

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256