2

Currently, the tool tip which shows when an action bar item is long pressed doesn't look so goodthis is how it is

How can I give it a similar look to this(with some padding around and closer to the button)? whatsapp

This is the layout of my toolbar if that would help

<androidx.appcompat.widget.Toolbar
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:paddingStart="0dp"
    android:paddingEnd="10dp"
    android:splitMotionEvents="false"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintBottom_toTopOf="@id/scrollable"
    app:popupTheme="@style/ThemeOverlay.AppCompat.DayNight" />

Edit

I realised all my tool tips look like that so I'm sure it has something to do with the theme

Here's my theme

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="android:fitsSystemWindows">true</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item>
</style>
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Kofi
  • 525
  • 6
  • 15
  • Are you looking for tooltip? https://developer.android.com/guide/topics/ui/tooltips – Mohammad Tabbara Jan 03 '20 at 20:30
  • That works for api level 26 and higher. My current minimum is 21 – Kofi Jan 03 '20 at 20:39
  • iirc, that little popup is its own window. Does your app theme define any styles that affect your windows (removing margins, etc)? – Ben P. Jan 03 '20 at 20:49
  • 1
    you can try snackbar – Yunus Kulyyev Jan 03 '20 at 20:50
  • @Ben.P this is my theme ` ` – Kofi Jan 03 '20 at 21:01
  • You're giving your tool bar the `@style/ThemeOverlay.AppCompat.ActionBar` style. Can you post what you have in your styles file for `ThemeOverlay.AppCompat.ActionBar`? I assume you unintentionally remove padding there. – Bruno Bieri Oct 18 '21 at 12:08

1 Answers1

0

At first make an xml file for the custom toast view that you want (in this case i think just having a TextView is enough):

XML file name is view_custom_toast

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="#AA000000"
    android:setTextColor="#ffffff" />

in MainActivity inflate your custom view like this and set your text on it:

TextView toastTextView = (TextView) getLayoutInflater()
                               .inflater.inflate(R.layout.view_custom_toast,
                                        (ViewGroup) findViewById(R.id.view_custom_toast));
toastTextView.setText("Hello World!");

At last you should set this view to like this:

Toast toast = new Toast(getApplicationContext);
toast.setView(toastTextView);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
  • Can you provide further explanation as to how to override long click of action item and display the toast near the item? – Kofi Jan 04 '20 at 07:43
  • You can use Snackbar for your purpose. But if you want to use `Toast message` it is best helped [here](https://stackoverflow.com/a/21026866/11037116) – mohammad fakhraee Jan 04 '20 at 14:22