Professional Documents
Culture Documents
Android action bar was introduced to maintain a consistent navigation across the application.
It has the powerful capabilities like
Action bar mainly contains four functional areas. They are app icon, view control, action
buttons and action overflow.
Step - 2 :
Add onCreateOptionsMenu() method in main activity class. M a i n A c t i v i t y . j a v a
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
RESULT :
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
case R.id.action_location_found:
// location found
LocationFound();
return true;
case R.id.action_refresh:
// refresh
return true;
case R.id.action_help:
// help action
return true;
case R.id.action_check_updates:
// check for updates action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Launching new activity
* */
private void LocationFound() {
Intent i = new Intent(MainActivity.this, LocationFound.class);
startActivity(i);
}
}
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location is found. Drop a Message here"/>
</LinearLayout>
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_found);
Also we need to add the new activity in AndroidManifest.xml file. Here we have to mention the
parent activity to which the up navigation is pointing to by using
android:parentActivityName property.
android:parentActivityName="info.androidhive.actionbar.MainActivity" >
</activity>
RESULT :
RESULT :