Created
September 30, 2019 13:54
-
-
Save darwind/4bd71c410a593a6eb136bf5b83c07a80 to your computer and use it in GitHub Desktop.
Custom tooltip (PopupWindow)
This file contains hidden or 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
Create and show tooltip right above BottomNavigationBar: | |
val popup = PopupWindow(this@MainActivity).apply { | |
ItemPopupBinding.inflate(LayoutInflater.from(this@MainActivity)).apply { | |
contentView = this.root | |
root.setOnClickListener { | |
dismiss() | |
} | |
} | |
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)); | |
animationStyle = R.style.Animation | |
} | |
binding.bottomNavigation.waitForLayout { | |
popup.showAtLocation(binding.bottomNavigation, Gravity.BOTTOM, (binding.bottomNavigation.width / 5), // Put it above number 2 item from the left. | |
(binding.bottomNavigation.height * 1.8).toInt()) // Multiply by 1.8 because our layout has a bit of margin inside the bottom image. | |
} | |
Item_popup.xml: | |
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:elevation="8dp" | |
android:gravity="center" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@drawable/tooltip_background" | |
android:paddingStart="8dp" | |
android:paddingTop="2dp" | |
android:paddingEnd="8dp" | |
android:paddingBottom="4dp" | |
android:text="Get lucky" | |
android:textColor="@color/white" /> | |
<ImageView | |
android:layout_width="12dp" | |
android:layout_height="12dp" | |
android:layout_marginTop="-5dp" | |
android:scaleType="center" | |
android:src="@drawable/ic_arrow_drop_down_black_24dp" /> | |
</LinearLayout> | |
</layout> | |
tooltip_background.xml: | |
<shape xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shape="rectangle"> | |
<corners android:radius="4dp" /> | |
<solid android:color="@android:color/black" /> | |
</shape> | |
ic_arrow_drop_down_black_24dp.xml: | |
<vector xmlns:android="http://schemas.android.com/apk/res/android" | |
android:width="24dp" | |
android:height="24dp" | |
android:viewportWidth="24.0" | |
android:viewportHeight="24.0"> | |
<path | |
android:fillColor="#FF000000" | |
android:pathData="M7,10l5,5 5,-5z" /> | |
</vector> | |
styles.xml: | |
<style name="Animation"> | |
<item name="android:windowEnterAnimation">@anim/fragment_fade_enter</item> | |
<item name="android:windowExitAnimation">@anim/fragment_fade_exit</item> | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment