M. Johnson
M. Johnson

Reputation: 839

Custom Actionbar in fragment android

I create simple project using actionbar ,i've one class and one fragment class, i define custome actionbar in class, and my problem, how to call method actionbar in fragment class from class, this code like below :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_holder);
    fragmentArray = new ConferenceFragment[8];
    // Load main fragment
    fragment = new HomeFragment();
    fragmentArray[0] = fragment;
    currentFragmentIndex = 0;
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.holder, fragment).commit();
//        initMenuBar();
}

public void initMenuBar(){
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(R.layout.menu_bar);

    ImageButton buttonSideMenu = (ImageButton) findViewById(R.id.bt_menu);
    buttonSideMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        toggle();
//            Toast.makeText(getApplicationContext(), "Clicked!",Toast.LENGTH_LONG).show();
        }
    });
    actionBar.show();
}

// fragment class

public class HomeFragment extends Fragment{

    View v;
    MainActivity mainactivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mainActivity.initMenuBar(); // ERROR IN THIS LINE
        super.onCreateView(inflater, container, savedInstanceState);
        v = inflater.inflate(R.layout.home_fragment, container, false);

        return v;
    }

}

Upvotes: 0

Views: 3237

Answers (2)

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

You need to get your MainActivity's context reference in your fragment to access activity's method.

Try this.

public class HomeFragment extends Fragment{

    View v;
    Context mContext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreateView(inflater, container, savedInstanceState);
        v = inflater.inflate(R.layout.home_fragment, container, false);
        mContext = getActivity();
        ((MainActivity) mContext).initMenuBar();
        return v;
    }

}

Upvotes: 0

Neeraj Kumar
Neeraj Kumar

Reputation: 943

ActionBar is traditionally the part of Activity and is available to activity only. You can get Activity instance by calling getActivity() from fragment and cast it to your activity then call public method to do whatever you want.

But the better option is to use ToolBar explained here support library v7

Upvotes: 2

Related Questions