
Action Bar in Android is used to provide navigation and to perform some actions. In this tutorial we are going to implement a Simple Action Bar in our Android Application and perform actions such as switching fragments , speech recognition. More Advanced implementation will be covered in another article. To support older version of Android use Android Support Library.
Step1:
Just crate provide with some name, after creating the project nothing is required in MainActivity.java & activity_main.xml like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="androiindian.customtoast.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Action Bar Example" android:layout_gravity="center" /> </LinearLayout> |
ActivityMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package androiindian.customtoast; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
Step2:
After this in create one folder with “menu.xml” in in res
Right click on res and select new and directory give name asvmenu then click ok
Step3:
Create one xml file in menu folder
Right click on menu and select new and layoutresouce give name as main-menu then click ok
After that add below code in main_menu.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="About" android:id="@+id/abt" android:orderInCategory="3"/> <item android:title="Android" android:id="@+id/ab" android:orderInCategory="1"/> <item android:title="Java" android:id="@+id/java" android:orderInCategory="2"/> <item android:title="PHP" android:id="@+id/php" android:orderInCategory="0" > </item> </menu> |
Step4:
Create One java file in src with the name of menuoptions.java and write below code in java file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package androiindian.customtoast; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; /** * Created by janga on 21-10-2016. */ public class Menuoptions extends AppCompatActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.abt: Toast.makeText(Menuoptions.this, "about", Toast.LENGTH_SHORT).show(); case R.id.ab: Toast.makeText(Menuoptions.this, "androi", Toast.LENGTH_SHORT).show(); case R.id.java: Toast.makeText(Menuoptions.this, "Java", Toast.LENGTH_SHORT).show(); case R.id.php: Toast.makeText(Menuoptions.this, "PHP", Toast.LENGTH_SHORT).show(); } return super.onOptionsItemSelected(item); } } |
Step5:
Finally Which are the other classes are their you change instead of “extends AppCompatActivity” you change “extends Menuoptions” like below in all classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package androiindian.customtoast; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends Menuoptions { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
Step6:
Just run and check the output