Last active
February 12, 2024 07:14
-
-
Save sunmeat/593e6194805e4eaa30f8c2ddc9e87319 to your computer and use it in GitHub Desktop.
main menu android example
This file contains 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
res \ values \ strings.xml: | |
<resources> | |
<string name="app_name">Menu Example</string> | |
<string name="file">File</string> | |
<string name="edit">Edit</string> | |
<string name="view">View</string> | |
<string name="navigate">Navigate</string> | |
</resources> | |
================================================================== | |
res \ menu \ menu.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<menu xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<item | |
android:id="@+id/action_file" | |
android:orderInCategory="1" | |
android:title="@string/file" | |
app:showAsAction="never" /> | |
<item | |
android:id="@+id/action_edit" | |
android:orderInCategory="2" | |
android:title="@string/edit" | |
app:showAsAction="never" /> | |
<item | |
android:id="@+id/action_view" | |
android:orderInCategory="3" | |
android:title="@string/view" | |
app:showAsAction="never" /> | |
<item | |
android:id="@+id/action_navigate" | |
android:orderInCategory="4" | |
android:title="@string/navigate" | |
app:showAsAction="never" /> | |
</menu> | |
================================================================== | |
MainActivity.java: | |
package com.sunmeat.myapplication; | |
import android.os.Bundle; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(@NonNull MenuItem item) { | |
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show(); | |
return true; | |
} | |
} | |
================================================================== | |
AndroidManifest.xml: | |
... | |
<application | |
... | |
android:theme="@style/AppTheme" | |
... | |
================================================================== | |
res \ values \ themes.xml: | |
<resources> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | |
<item name="android:windowBackground">@color/white</item> | |
<item name="android:textColor">@color/black</item> | |
</style> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment