Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

ActionBar in Android with Example

ActionBar is a major part of the user interface in an Android app. It's used to display an app's title, provide navigation (with Up button or tabs), and contain action items and an overflow menu. With the introduction of Toolbar and AppCompatActivity, it has become even more flexible, allowing developers to place it anywhere within their app's UI.

Below is a simple example to demonstrate how to use an ActionBar:

  • Create a new Android project in Android Studio and choose an Empty Activity template.

  • Modify your res/values/styles.xml to inherit from a theme that supports ActionBar:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>
  • In res/layout/activity_main.xml, have a simple layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello ActionBar!" />

</RelativeLayout>
  • In src/MainActivity.java, set up the ActionBar:
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;

public class MainActivity extends AppCompatActivity {

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

        // Set up the ActionBar
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("My ActionBar Example");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            // Handle settings click
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  • Create a new menu resource file under res/menu named main_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="Settings"
        android:showAsAction="never" />

</menu>

Now, when you run your app, you will see an ActionBar at the top of the screen with a title and an overflow menu that contains a "Settings" item.

  1. ActionBar example code in Android:

    The ActionBar is a component that provides a consistent navigation and action bar design across Android applications. Here's a simple example of using ActionBar in an activity:

    // MainActivity.java
    import android.app.ActionBar;
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Get the ActionBar
            ActionBar actionBar = getActionBar();
    
            // Set ActionBar title
            actionBar.setTitle("My ActionBar Example");
        }
    }
    
    <!-- activity_main.xml -->
    <RelativeLayout 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">
    
        <!-- Your main content goes here -->
    
    </RelativeLayout>
    
  2. Android ActionBar styling and theming:

    Styling the ActionBar involves customizing its appearance using styles and themes. You can define styles in the res/values/styles.xml file and apply them to your ActionBar. Here's an example:

    <!-- styles.xml -->
    <resources>
        <style name="MyActionBarStyle" parent="@android:style/Widget.Holo.ActionBar">
            <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
        </style>
    
        <style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
            <item name="android:textColor">#FF0000</item>
        </style>
    </resources>
    

    Apply this style in the activity:

    // MainActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Get the ActionBar and apply custom style
        ActionBar actionBar = getActionBar();
        actionBar.setTitle("Styled ActionBar");
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_back_arrow);
    
        // Apply custom theme
        setTheme(R.style.MyAppTheme);
    }
    
  3. ActionBarDrawerToggle example in Android:

    ActionBarDrawerToggle is used to add a navigation drawer toggle button to the ActionBar. Here's a simple example:

    // MainActivity.java
    import android.app.ActionBar;
    import android.app.Activity;
    import android.os.Bundle;
    import androidx.appcompat.app.ActionBarDrawerToggle;
    import androidx.drawerlayout.widget.DrawerLayout;
    
    public class MainActivity extends Activity {
    
        private ActionBarDrawerToggle drawerToggle;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Get the ActionBar
            ActionBar actionBar = getActionBar();
    
            // Create a DrawerLayout
            DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
    
            // Create ActionBarDrawerToggle and set it as the DrawerListener
            drawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                R.string.drawer_open,
                R.string.drawer_close
            );
            drawerLayout.addDrawerListener(drawerToggle);
    
            // Enable the Up button for navigation
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    
        // Override onOptionsItemSelected to handle the Up button
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (drawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    
  4. Android ActionBar with tabs example code:

    Using tabs with ActionBar involves creating a TabListener and adding tabs programmatically. Here's a basic example:

    // MainActivity.java
    import android.app.ActionBar;
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Get the ActionBar
            ActionBar actionBar = getActionBar();
    
            // Create tabs
            ActionBar.Tab tab1 = actionBar.newTab().setText("Tab 1");
            ActionBar.Tab tab2 = actionBar.newTab().setText("Tab 2");
    
            // Set TabListeners for handling tab selection
            tab1.setTabListener(new MyTabListener(new Fragment1()));
            tab2.setTabListener(new MyTabListener(new Fragment2()));
    
            // Add tabs to the ActionBar
            actionBar.addTab(tab1);
            actionBar.addTab(tab2);
        }
    
        // Define a custom TabListener
        private class MyTabListener implements ActionBar.TabListener {
    
            private Fragment fragment;
    
            public MyTabListener(Fragment fragment) {
                this.fragment = fragment;
            }
    
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                // Show the selected tab content
                ft.replace(R.id.fragment_container, fragment);
            }
    
            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
                // Hide the unselected tab content
                ft.remove(fragment);
            }
    
            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
                // Do nothing when reselected
            }
        }
    }
    
  5. How to hide ActionBar in Android app:

    To hide the ActionBar, you can use the following method:

    // MainActivity.java
    import android.app.ActionBar;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Get the ActionBar
            ActionBar actionBar = getActionBar();
    
            // Hide the ActionBar
            if (actionBar != null) {
                actionBar.hide();
            }
        }
    }
    

    In this example, actionBar.hide() is used to hide the ActionBar. Ensure that you check for null to avoid a potential NullPointerException.