Skip to content

Instantly share code, notes, and snippets.

@senamit2708
Created June 8, 2019 15:29
Show Gist options
  • Save senamit2708/a12862142c45cd33f8bf5443fc206684 to your computer and use it in GitHub Desktop.
Save senamit2708/a12862142c45cd33f8bf5443fc206684 to your computer and use it in GitHub Desktop.
How to add menu in fragment having differnet text color
https://stackoverflow.com/questions/45831852/android-menu-text-color-change
//hint from stack overflow
int positionOfMenuItem = 0;
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
inside onViewCreated method
setHasOptionsMenu(true);
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/btnSubmit"
app:showAsAction="ifRoom"
android:title="SUBMIT"/>
</menu>
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_quiz, menu);
int positionOfMenuItem = 0;
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("Submit");
s.setSpan(new ForegroundColorSpan(Color.BLACK), 0, s.length(), 0);
item.setTitle(s);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId()==R.id.btnSubmit){
Toast.makeText(context, "button is selected", Toast.LENGTH_SHORT).show();
mQuizViewModel.setQuizResult(questionListNew);
Navigation.findNavController(btnHide).navigate(R.id.action_quizQuestionFrag_to_quizResultFrag);
// Navigation.findNavController(btnHide).popBackStack(R.id.quizResultFrag, false);
return true;
}
return super.onOptionsItemSelected(item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment