I am working on android application where i need a button in action bar. These are my activity and java files. So, i need a button at right side of my tool bar. Thanks in advance.
content_b2_bdeliveries.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.ideabiz.riderapplication.ui.B2BDeliveriesActivity" tools:showIn="@layout/activity_b2_bdeliveries"> //remaining code; </LinearLayout>
activity_b2_bdeliveries.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" android:fitsSystemWindows="true" tools:context="com.ideabiz.riderapplication.ui.B2BDeliveriesActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_b2_bdeliveries" /> </android.support.design.widget.CoordinatorLayout>
B2BDeliveriesActivity.java:
public class B2BDeliveriesActivity extends AppCompatActivity { private final String TAG_NAME="B2BActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b2_bdeliveries); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); ActionBar actionBar = getSupportActionBar(); //Log.d(TAG_NAME,actionBar.toString()); if (actionBar != null) { try { actionBar.setDisplayHomeAsUpEnabled(true); } catch (Exception e) { Crashlytics.logException(e); Log.d(TAG_NAME, "Null Pointer from setDisplayHomeAsUpEnabled" + e.getMessage()); Utilities.issueTokenUpload("Null Pointer from setDisplayHomeAsUpEnabled" + e, sharedPreferences.getInt("tripId", 999999), sharedPreferences.getString("driverPhoneNo", "9999999999"), sharedPreferences.getString("driverName", "default"), getResources().getString(R.string.version)); } } }
Add Action Buttons To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.
This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Right-click on the res folder and selects New -> Directory. Give the name “menu” to the new directory. Further, create a new Menu Resource File by right click on the menu directory. As the ActionBar is being created for the main Activity, type the name as “main” to the Menu Resource File.
In your activity class, override the following methods if they are not there by default:
// create an action bar button @Override public boolean onCreateOptionsMenu(Menu menu) { // R.menu.mymenu is a reference to an xml file named mymenu.xml which should be inside your res/menu directory. // If you don't have res/menu, just create a directory named "menu" inside res getMenuInflater().inflate(R.menu.mymenu, menu); return super.onCreateOptionsMenu(menu); } // handle button activities @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.mybutton) { // do something here } return super.onOptionsItemSelected(item); }
mymenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/mybutton" android:title="" app:showAsAction="always" android:icon="@drawable/mybuttonicon" /> </menu>
If you want to customize UI of your ActionBar, then you can use Toolbar as ActionBar. Here are the steps to do the same :
Add below code as a child of AppBarLayout in your activity layout xml.
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="1"> <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ActionBar Title"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </android.support.v7.widget.Toolbar>
Make your Toolbar as ActionBar by adding the below code in your Activity onCreate() method.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In AndroidManifest.xml, add following theme to your activity:
android:theme="@style/AppTheme.NoActionBar"
In styles.xml add following code:
<style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With