2

When a user holds down an ActionBar item a Toast appears displaying a the item's title if one was specified in the android:title attribute in the menu's layout file. In my application, the default background color is the same as the default text color rendering the text nearly illegible. How do I change the background color or the text color of these Toasts?

I am using Android 5.1.

illegible toast

Community
  • 1
  • 1

1 Answers1

2

If you would like to change the TextColor of a Toast, you can reference this question, as it has a good example.

To do this on an action bar item, you'll first need to add a custom view to your action bar since it doesn't support a way to handle long clicks.

I would create a layout file used for your action bar, and then inside your activities onCreate() you can do this:

View actionBarView = getLayoutInflater().inflate(R.layout.my_action_bar, null)
ImageView actionItem = (ImageView) actionBarView.findViewById(R.id.myImageViewIcon);
actionItem.setOnLongClickListener(new OnLongClickListener(){
   @Override
   public boolean onLongClick(View v){
      // Show toast with custom text
   }
});
actionItem.setOnClickListener(new OnClickListener(){
   @Override
   public boolean onClick(View v){
      // Handle regular click
   }
});
getSupportActionBar().setCustomView(actionBarView);

For more information on the second part, You can reference this question.

Community
  • 1
  • 1
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • Where does this answer the original question? Creating a custom view in the action bar and implementing `onLongClick()` is not "changing the text color of a menu's default toast". – Austyn Mahoney Jan 27 '16 at 07:17
  • 1
    @AustynMahoney The answer builds upon one in the other question I linked. The other question shows how to change the Toast text, I went into more detail on how to handle onLongClick. The OP needs onLongClick in order to show their custom Toast and not the default one. – AdamMc331 Jan 27 '16 at 15:04
  • He asked how you style the default one, not how to create one to replace it. I'm looking to fix this issue and creating a custom action item just to fix the toast seems like a hack. – Austyn Mahoney Jan 27 '16 at 21:44
  • I didn't think that you could style the existing Toast that appears, since it is something that is created for you, so I thought this was the only way you could 'override' it so to speak. I will look into other options, but OP felt this was okay. – AdamMc331 Jan 28 '16 at 03:09