How to add App Bar to Android application Example

Another name for App Bar is action bar.The main use of App bar is to make the application visual structure consistent.

Another name for App Bar is action bar.The main use of App bar is to make the application visual structure consistent.
You can add search actions, give app identity and location to users.In this tutorial am going to use v7 appcompat support library to add Toolbar because it can work in many devices.


Adding Toolbar to activity.

  • Make sure you add v7 appcompat support library
  • In your activity extend class AppCompatActivity public
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

}
  • Prevent the theme from using its default ActionBar by adding below code I manifest file.
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
</application>

Add Toolbar to layout 


<?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.appbarexample.MainActivity">

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

MainActivity.java

package net.techoverload.www.appbarexample;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolBar=(Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolBar);
}
}

By now the application should run and show a Toolbar with just the application title.
Let now go ahead and add some menu to the Toolbar.
Open res>create a new resource menu and XML menu file called example_menu.xml and paste below code.


<?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:icon="@drawable/ic_search"
android:title="action_search"
app:showAsAction="ifRoom"
/>
<item android:id="@+id/action_favorite"
android:icon="@drawable/ic_action_favorite"
android:title="action_favorite"
app:showAsAction="ifRoom"
/>
<item android:title="Settings"
android:id="@+id/action_settings"
app:showAsAction="never"
/>
<item android:title="Most Viewed"
android:id="@+id/most_viewed"
app:showAsAction="never"/>
</menu>

After creating our menu now we need to add it to our MainActivity.java
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.example_menu,menu);

return true;

}

NB: You will be required to import

import android.view.MenuInflater;
import android.view.Menu;

Let now add some action to the menu items.
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_favorite:
//Do something
return true;

case R.id.action_search:
//Do something
return true;
case R.id.action_settings:
//Do something
return true;
}


return super.onOptionsItemSelected(item);
}


Whole MainActivity.java Code.

package net.techoverload.www.appbarexample;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolBar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolBar);


}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.example_menu,menu);

return true;

}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_favorite:

//Do something
return true;

case R.id.action_search:
//Do something
return true;
case R.id.action_settings:
//Do something
return true;
}


return super.onOptionsItemSelected(item);
}



}




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 App Bar to Android application Example
How to add App Bar to Android application Example
Another name for App Bar is action bar.The main use of App bar is to make the application visual structure consistent.
Java Codes
https://www.download-all.net/2018/02/how-to-add-app-bar-to-android.html
https://www.download-all.net/
https://www.download-all.net/
https://www.download-all.net/2018/02/how-to-add-app-bar-to-android.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