최소한의 코드로 레드 닷 흑은 뱃지를 표시해보자.
1. button layout xml menuitem_recording_settings.xml ic_notification_overlay는 안드로이드 기본 리소스 임.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_video_white_24dp" />
<ImageView
android:id="@+id/red_dot"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="5dp"
android:background="@android:drawable/ic_notification_overlay"
android:visibility="gone" />
<!-- <TextView-->
<!-- android:id="@+id/badge"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_gravity="right|end|top"-->
<!-- android:layout_marginEnd="-5dp"-->
<!-- android:layout_marginRight="-5dp"-->
<!-- android:background="@android:drawable/ic_notification_overlay"-->
<!-- android:visibility="gone"-->
<!-- android:text="9"-->
<!-- android:textColor="#FFF"-->
<!-- android:textSize="11sp" />-->
</FrameLayout>
2. menu xml : actionLayout을 지정한다.
<item
android:id="@+id/action_recording_settings"
android:icon="@drawable/ic_video_white_24dp"
android:title="@string/recording_settings"
app:actionLayout="@layout/menuitem_recording_settings"
app:showAsAction="always" />
3. Settings에 추가: 버튼에 레드닷이 나중에 또 필요해 질 수 있기 때문에 버전으로 관리한다.
public static final SettingInt recordingSettingsRedDotVersion = new SettingInt("recordingSettingsRedDotVersion", 0).setResetable();
public static final int RECORDING_SETTING_RED_DOT_VERSION = 2;
4. onCreateOptionsMenu: OnClickListener와 롱 클릭시 툴팁 띄우기를 처리한다.
// custom recording settings
MenuItem recordingItem = menu.findItem(R.id.action_recording_settings);
if (recordingItem != null) {
recordingItem.getActionView().setOnClickListener(v -> onOptionsItemSelected(recordingItem));
if (Settings.recordingSettingsRedDotVersion.getValue() < Settings.RECORDING_SETTING_RED_DOT_VERSION) {
recordingItem.getActionView().findViewById(R.id.red_dot).setVisibility(View.VISIBLE);
}
TooltipCompat.setTooltipText(recordingItem.getActionView(), getString(R.string.recording_settings));
}
5. onOptionsItemSelected: 다음 부턴 red dot 숨기기 처리
if (item.getItemId() == R.id.action_recording_settings) {
startActivity(new Intent(this, RecordingSettingsActivity.class));
View redDotView = item.getActionView().findViewById(R.id.red_dot);
if (redDotView.getVisibility() == View.VISIBLE) {
Settings.recordingSettingsRedDotVersion.setValue(Settings.RECORDING_SETTING_RED_DOT_VERSION);
redDotView.setVisibility(View.GONE);
}
return true;
}