【ANDROID STUDIO】 Runtime Menu

package com.example.runtimemenu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private final int MENU_DOWNLOAD = 1;
private final int MENU_SETTINGS = 2;
private boolean showDownloadMenu = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void toggleMenu(View view) {
showDownloadMenu = !showDownloadMenu;
invalidateOptionsMenu();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_DOWNLOAD, 0, R.string.menu_download);
menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings);
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItem = menu.findItem(MENU_DOWNLOAD);
menuItem.setVisible(showDownloadMenu);
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_DOWNLOAD:
Toast.makeText(this, R.string.menu_download, Toast.LENGTH_LONG).show();
break;
case MENU_SETTINGS:
Toast.makeText(this, R.string.menu_settings, Toast.LENGTH_LONG).show();
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button android:id="@+id/buttonToggleMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Menu"
android:onClick="toggleMenu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

No comments:

When Bad People Make Good Art

I offer six guidelines on cancel culture ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏...

Contact Form

Name

Email *

Message *