9

My requirement is to increase the height of the title bar, however I am not sure if that is feasible. If it is how can I increase its height so I would have more space. If it is not, I would need to add a menu option (Hamburger Style) to my customized title bar.

custom_title_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#77b48e"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="29dp"
            android:layout_height="28dp"
            android:maxWidth="1dp"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/title_bar_viewname"
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_weight="0.07"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="10sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
    }
Jack
  • 6,430
  • 27
  • 80
  • 151
  • Is [this](http://stackoverflow.com/questions/6226538/how-to-increase-the-size-of-the-title-bar-in-an-android-application) what you need ? **Check 2nd Answer** – Abdelrahman Elkady Jan 12 '15 at 23:59

2 Answers2

19
  1. Change your MainActivity.java like this:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 
            setContentView(R.layout.activity_main);
    
           actionBar = getActionBar();
            ColorDrawable colorDrawable = new ColorDrawable(
                    Color.parseColor("#373836"));
            actionBar.setBackgroundDrawable(colorDrawable);
    
        }
    

    For an Eg: now your action bar will be shown like this:

    enter image description here

  2. Then if you need to add the setting menu items in the action bar.

    Add this below code in MainActivity,java:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       switch (item.getItemId()) 
       {
         case R.id.search:
          //your code here
            return true;
         default:
            return super.onOptionsItemSelected(item);
       }
    }   
    

    Then in res/menu/main.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" >
    
        <item
            android:id="@+id/search"
            android:icon="@drawable/ic_action_settings"
            android:showAsAction="always"
            android:title="text"/>
    
    </menu>
    

    Now you can see the settings icon in the action bar

    enter image description here

  3. Finally if you have to increase the Action bar Height:

    In manifest:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" > --->set a theme 
    
    ...
    </application>
    

    Then in Styles.xml:

    <resources>
    
        <style name="AppBaseTheme" parent="android:Theme.Light"></style>
    
        <style name="AppTheme" parent="AppBaseTheme">
            <item name="android:actionBarSize">80dip</item>
        </style>
    
    </resources>  
    

    Now Action bar size is increased upto the size we given in styles.

    enter image description here

Stephen
  • 9,899
  • 16
  • 90
  • 137
2

With the release of android L, a new view has been released: Toolbar. Toolbar gives you much more flexibility to style the action Bar. It is also available for lower android versions via the appCompat library.

In this release, Android introduces a new Toolbar widget. This is a generalization of the Action Bar pattern that gives you much more control and flexibility. Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate it, and react to scroll events. You can also set it as your Activity’s action bar, meaning that your standard options menu actions will be display within it.

You can change the height of your action bar by using Toolbar and just setting the layout_height attribute to the height you want.

More info on how to use it:

http://android-developers.blogspot.be/2014/10/appcompat-v21-material-design-for-pre.html

Documentation:

http://developer.android.com/reference/android/widget/Toolbar.html

Robbe
  • 2,610
  • 1
  • 20
  • 31