0% found this document useful (0 votes)
4 views21 pages

Action Bar

Uploaded by

Hyma Thottathyl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Action Bar

Uploaded by

Hyma Thottathyl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

• Android ActionBar is a menu bar that runs across the top of the

activity screen in android. Android ActionBar can contain menu


items which become visible when the user clicks the “menu”
button. In general an ActionBar consists of the following four
components:
• App Icon: App branding logo or icon will be displayed here
• View Control: A dedicated space to display Application title.
Also provides option to switch between views by adding spinner
or tabbed navigation
• Action Buttons: Some important actions of the app can be
added here
• Action Overflow: All unimportant action will be shown as a
menu
1
The action bar displays the application icon together with the
activity title. On the right side of the action bar, there are action
items.

Implementation

Create a new project by selecting File->New->New Project.

2
Modify the code in the onCreate() method to hide the ActionBar.
protected void onCreate(Bundle savedInstanceState) {
//getSupportActionBar().setDisplayHomeAsUpEnabled(false);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
@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_main, menu);
return true;
} 3
• Debug the Application and you will see the
result.

4
• Now, we see how to add action items to the action bar. Action
items are the shortcut to some of the commonly performed
operations in the Application.
• A student registration application can have some action items
such as “Add New Student”, “Update Student Info”, “Delete
Student Info” etc.

5
Modify the noCreateOptionsMenu() method.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is presen
//getMenuInflater().inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}

6
Create 2 methods CreateMenu() and MenuChoice() to add more
options in the menu.
private void CreateMenu(Menu menu)
{
MenuItem menu1=menu.add(0,0,0,"Add New Student");
{
menu1.setIcon(R.drawable.ic_launcher);
menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
MenuItem menu2=menu.add(0, 1, 1, "Update Student Info");
{
menu2.setIcon(R.drawable.ic_launcher1);
menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
7
MenuItem menu3=menu.add(0,2,2,"Delete Student Info");
{
menu2.setIcon(R.drawable.ic_launcher2);
menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

}
}

8
private boolean MenuChoice(MenuItem opt)
{
switch(opt.getItemId())
{
case 0:
Toast.makeText(this,"You clicked Ist option",Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(this,"You clicked 2nd option",Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this,"You clicked 3rd option",Toast.LENGTH_LONG).show();
return true;
}
return false;
}
9
• Debug the application by pressing F11 on the Android emulator
or the mobile device. You will see the icons on the right side of
the action bar and clicking on each menu item will cause the
Toast class to display the name of the item selected, as given
below.

10
We can also customize the Action Items and application icon.
private void CreateMenu(Menu menu)
{
MenuItem menu1=menu.add(0,0,0,"Add New Student");
{
menu1.setIcon(R.drawable.ic_launcher);
menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TE
XT);
}
MenuItem menu2=menu.add(0, 1, 1, "Update Student Info");
{
menu2.setIcon(R.drawable.ic_launcher1);
menu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
);
} 11
12
EXAMPLE
•You can display additional items on the Action
Bar. These additional items are called action
items. Action items are shortcuts to some of the
commonly performed operations in your
application.

13
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>

14
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
15
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
return MenuChoice(item);
}
16
private void CreateMenu(Menu menu)
{
MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
{
mnu1.setIcon(R.drawable.ic_launcher);

mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
{
mnu2.setIcon(R.drawable.ic_launcher);

mnu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
} 17
MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
{
mnu3.setIcon(R.drawable.ic_launcher);
mnu3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
{
mnu4.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
MenuItem mnu5 = menu.add(0, 4, 4, "Item 5");
{
mnu5.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
18
private boolean MenuChoice(MenuItem item)
{
switch (item.getItemId()) {
case 0:
Toast.makeText(this, "You clicked on Item 1",
Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(this, "You clicked on Item 2",
Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this, "You clicked on Item 3",
Toast.LENGTH_LONG).show();
return true; 19
case 3:
Toast.makeText(this, "You clicked on Item 4",
Toast.LENGTH_LONG).show();
return true;
case 4:
Toast.makeText(this, "You clicked on Item 5",
Toast.LENGTH_LONG).show();
return true;
}
return false;
}

} 20
21

You might also like