0

enter image description hereI have a activity and fragment ,i want to set a menu to mytoolbar in pre run time the icon has set but in run application it does not show anything.

in my activity:

public class MainActivity extends AppCompatActivity {

private TabLayout mTabLayout;
private TextView mTextViewTabOne;
private TextView mTextViewTabTwo;
private TextView mTextViewTabThree;
private android.support.v7.widget.Toolbar mToolbar;
private TextView mTextViewToolbarTitle;
private AlertDialog mAlertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setLocale("fa");


    getSupportFragmentManager().beginTransaction().replace(R.id.framelayout_mainactivity_fragmentcontainer, new AuthenticationPasswordFragment()).commit();
    setToolBar(getString(R.string.addbank_toolbartitle));

}

public void setToolBar(String title) {


    mToolbar = findViewById(R.id.toolbar_everywhere_toolbar);
    mTextViewToolbarTitle = findViewById(R.id.toolbar_title);

    mTextViewToolbarTitle.setText(title);

}

and in my fragment :

public class AuthenticationPasswordFragment extends BaseFragment implements BaseAuthenticationContract.View {

private TextInputEditText mEditTextPassword;
private TextInputLayout mTextInputEditTextPassword;
private View mRoot;

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

    mRoot=inflater.inflate(R.layout.fragment_authenticationpassword,null);

    return mRoot;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.authenticationpassword_menutoolbar,menu);
}

in my menu is:

<item
    android:id="@+id/item_authenticationpassword_confirm"
    android:title="confirm"
    android:icon="@drawable/everywhere_confirm"
    app:showAsAction="always"
    />

milad salimi
  • 1,580
  • 2
  • 12
  • 31

3 Answers3

2

Replace this setToolbar in activity :

public void setToolBar(String title,int resourceMenu) {

        mToolbar = findViewById(R.id.toolbar_everywhere_toolbar);
        mTextViewToolbarTitle = findViewById(R.id.toolbar_title);
        mTextViewToolbarTitle.setText(title);
        mToolbar.inflateMenu(resourceMenu);

    }

With this :

public void setToolBar(String title) {

        mToolbar = findViewById(R.id.toolbar_everywhere_toolbar);
        mTextViewToolbarTitle = findViewById(R.id.toolbar_title);
        mTextViewToolbarTitle.setText(title);

    }

And add this line to your onCreateView fragment or any fragment that you want add menu toolbar to it :

((MainActivity)getActivity()).setToolBar(getString(R.string.authenticationpassword_titletoolbar),R.menu.authenticationpassword_menutoolbar);
milad salimi
  • 1,580
  • 2
  • 12
  • 31
1

In the activity's onCreate method, set the toolbar with

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Then add this method.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.yourmenufile, menu);
  return super.onCreateOptionsMenu(menu);
}

Then add menu click listener,

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case 0:
            // do whatever
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

If you want to change the menu items and listeners while in different fragments, use interfaces to let the activity know which fragment is active and change accordingly.

Sandesh Baliga
  • 579
  • 5
  • 20
0

If you want the method onCreateOptionsMenu inside a Fragment, add the codeline toolbar.setHasOptionsMenu(true) in onCreate of the activity where the Toolbar is set up. The second option is to move the onCreateOptionsMenu into the Activity.

See this related StackOverflow question.

Report back if this works, I hope it helps you.

BenjyTec
  • 1,719
  • 2
  • 12
  • 22