Android ActionBar Example

The action bar is an important design element, usually at the top of each screen in an app, that provides a consistent familiar look between Android apps. It is used to provide better user interaction and experience by supporting easy navigation through tabs and drop-down lists. It also provides a space for the app or activity’s identity, thus enabling the user to know their location in the app, and easy access to the actions that can be performed.

1. Main Activity.java

package com.tutorialsee.actionbar;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.Toast;

public class MainActivity extends Activity {
// Action bar
private ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Custom Action Bar
/*getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  getActionBar().setCustomView(R.layout.custom_actionbar);*/

//Action Bar
actionBar = getActionBar();
//Action Bar Text color change
getActionBar().setTitle(Html.fromHtml("<font color="#ffffff">" + getString(R.string.app_name) + "</font>"));
// set the icon
actionBar.setIcon(R.drawable.ic_launcher);


//Action Bar some change
/*actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.color.white);
actionBar.setHomeButtonEnabled(true);*/

Fragment main = new HomeFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, main);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);
        return true;
       
        //Search
/*getMenuInflater().inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
                .getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        return super.onCreateOptionsMenu(menu);*/
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
switch (id) {
case R.id.action_call:
Intent dialer= new Intent(Intent.ACTION_DIAL);
startActivity(dialer);
return true;
case R.id.action_speech:
Bundle args1 = new Bundle();
args1.putString("Menu", "You pressed Speech button.");
Fragment detail1 = new TextFragment();
detail1.setArguments(args1);
FragmentManager fragmentManagers = getFragmentManager();
fragmentManagers.beginTransaction().replace(R.id.content_frame, detail1).commit();
return true;
case R.id.action_done:
Bundle args = new Bundle();
args.putString("Menu", "You pressed done button.");
Fragment detail = new TextFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
return true;
/*//OverFlow Menu
case R.id.action_contacts:
            Toast.makeText(getApplicationContext(),"Contacts Clicked",Toast.LENGTH_SHORT).show();
            return true;
        case R.id.action_settings:
            Toast.makeText(getApplicationContext(),"Settings Clicked",Toast.LENGTH_SHORT).show();
            return true;
        case R.id.action_status:
            Toast.makeText(getApplicationContext(),"Status Clicked",Toast.LENGTH_SHORT).show();
            return true;
*/
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1234 && resultCode == RESULT_OK) {
String voice_text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).get(0);
Toast.makeText(getApplicationContext(),voice_text,Toast.LENGTH_LONG).show();
}
}
}

2. activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

3. menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

     <item
        android:id="@+id/action_speech"
        android:icon="@drawable/microphone"
        android:showAsAction="ifRoom"
        android:title="@string/Speech"/>
    <item
        android:id="@+id/action_call"
        android:icon="@drawable/call"
        android:showAsAction="ifRoom"
        android:title="@string/call"/>
    <item
        android:id="@+id/action_done"
        android:icon="@drawable/done"
        android:showAsAction="ifRoom"
        android:title="@string/Done"/>
<item android:id="@+id/action_contacts"
        android:showAsAction="never"
        android:title="Contacts"/>
    <item android:id="@+id/action_settings"
        android:showAsAction="never"
        android:title="Settings"/>
    <item android:id="@+id/action_status"
        android:showAsAction="never"
        android:title="Status" />
</menu>

4. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorialsee.actionbar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"/>

    <uses-permission android:name="android.permission.INTERNET" />
   
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
          <!-- Search results activity -->
        <activity android:name=".SearchResultsActivity"
            android:parentActivityName="info.androidhive.actionbar.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
    </application>

</manifest>

Comments

Email Address: 

<p>It showing me error on import com.tutorialsee.NumberProgressBar</p>