How to add a Toolbar to Application in Android Studio.

How to add a Toolbar to Application in Android Studio and add menu items to it.

How to add a Toolbar to Application in Android Studio.
In this tutorial am going to show you how to add a Toolbar to the application.Then add Search and settings menu.

Open Android Studio and create a new project

Project name: ToolbarExample
Next Open Manifest.xml file and add this.
Create the MainActivity.java and ensure you check Backwards compatibility(AppCompat) checkbox.



  android:theme="@style/Theme.AppCompat.Light.NoActionBar">

This code will ensure that NoActionBar is created this giving us the chance to define our own Toolbar.

AndroidManifest.xml code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.techoverload.www.toolbarexample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


Create a folder under res> and name it menu.
Inside menu create a xml file and name it toolbar_menu.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom"
/>


</menu>



activity_main.xml code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.techoverload.www.toolbarexample.MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"


/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


MainActivity.java

package net.techoverload.www.toolbarexample;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

Toolbar toolbar=(Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.toolbar_menu,menu);
return true;



}

}


Note:
For this project to work, make sure you add image asset icon and label it "ic_action_search"


Download Source code here


Output Screenshots


COMMENTS

Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to add a Toolbar to Application in Android Studio.
How to add a Toolbar to Application in Android Studio.
How to add a Toolbar to Application in Android Studio and add menu items to it.
https://4.bp.blogspot.com/-nkeBEh_Cetw/WpaNxRT-VnI/AAAAAAAAKTA/dCQpAwqGsO8oRnbwTx1u41-SUIxnEtk5wCEwYBhgL/s320/android-toolbar-example.PNG
https://4.bp.blogspot.com/-nkeBEh_Cetw/WpaNxRT-VnI/AAAAAAAAKTA/dCQpAwqGsO8oRnbwTx1u41-SUIxnEtk5wCEwYBhgL/s72-c/android-toolbar-example.PNG
Java Codes
https://www.download-all.net/2018/02/how-to-add-toolbar-to-application-in.html
https://www.download-all.net/
https://www.download-all.net/
https://www.download-all.net/2018/02/how-to-add-toolbar-to-application-in.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy