Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add menu and settings option to the customized title bar of android?

Tags:

android

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);
    }
like image 325
Jack Avatar asked Jan 09 '15 07:01

Jack


People also ask

How do I show the menu bar on Android?

This is going to be in res/menu/main_menu . Right click the res folder and choose New > Android Resource File. Type main_menu for the File name. Choose Menu for the Resource type.

Where is the menu bar in Android Studio?

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.


1 Answers

  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

like image 103
Steve Avatar answered Oct 18 '22 15:10

Steve