Created
June 8, 2019 15:29
-
-
Save senamit2708/a12862142c45cd33f8bf5443fc206684 to your computer and use it in GitHub Desktop.
How to add menu in fragment having differnet text color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inside onViewCreated method | |
setHasOptionsMenu(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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