Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
Add items/menu in actionbar/toolbar android studio
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_one" android:icon="@drawable/ic_baseline_person_24" app:showAsAction="always" android:title="Item One"/> <item android:id="@+id/action_two" android:title="Item Two"/> <item android:id="@+id/action_three" android:title="Item Three"/> </menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center" tools:context=".MainActivity"> <TextView android:text="Hellow World!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.usmtip.myapplication; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; 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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id==R.id.action_one){ Toast.makeText(this, "Item One Clicked", Toast.LENGTH_SHORT).show(); return true; } else if (id==R.id.action_two){ Toast.makeText(this, "Item Two Clicked", Toast.LENGTH_SHORT).show(); return true; } else if (id==R.id.action_three){ Toast.makeText(this, "Item Three Clicked", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } }
0 Comments: