Switch button в menu java
Всем привет. Возник вопрос. Как я могу сделать в menu java приложения switch кнопку (переключатель). И отследить в каком положении она сейчас находится. Если именно switch кнопку сделать нельзя, то как можно сделать какой-то другой переключатель. Нужно, чтобы была надпись и после нее переключатель. Подскажите пожалуйста!
Ответы (2 шт):
Автор решения: Dareten
→ Ссылка
Примерно так:
Внутри activity_main_drawer
<menu>
<item
android:id="@+id/nav_notifications"
android:icon="@drawable/ic_notifications"
android:title="@string/menu_notifications"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
...
Сам переключатель
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/notifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Автор решения: Maks
→ Ссылка
https://stackoverflow.com/questions/44487789/how-to-add-switch-listener-in-action-bar-android
Только не забудьте заменить строку
Switch mainSwitchOnOffSw = (Switch) view.findViewById(R.id.switchForActionBar);
На
SwitchCompat mainSwitchOnOffSw = (SwitchCompat) view.findViewById(R.id.switchForActionBar);