How to Add Button in Actionbar(Android)

Add button in android action bar

Add toolbar inside your xml . and use NoActionBar in your activity theme .

add these toolbar in your xml on top

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131313"
android:minHeight="?attr/actionBarSize">

<LinearLayout
android:layout_width="wrap_content"
android:background="#00aaaaaa"
android:layout_gravity="right"
android:layout_height="match_parent">

<Button
android:text="Delete"
android:layout_width="wrap_content"
android:background="#000"
android:textColor="#fff"
android:layout_height="wrap_content"
android:textSize="16dp" />

</LinearLayout>

</android.support.v7.widget.Toolbar>

add these theme in your activity in Menifest.xml

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

and add these code in your activity ....

Toolbar topToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
ActionBar actionBar = getSupportActionBar();;
actionBar.setDisplayHomeAsUpEnabled(true);

Output:-

fg

OR

I think it is possible with the icon only .....

use this item in 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/delete"
android:icon="@drawable/YourImage"
app:showAsAction="always"></item>
</menu>

Note:- use android:icon="@drawable/YourImageWithTextDelete" put here your image .....

Add a button to the top right of action bar

You can add a button by editing/create the menu xml file:

<?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_name"
android:icon="@drawable/you_resource_here"
android:title="Text to be seen by user"
app:showAsAction="always"
android:orderInCategory="0"/>

</menu>

Then in your activity, if you created a new file your need to edit onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

and you can edit what the actions do in the following method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_name) {
return true;
}

return super.onOptionsItemSelected(item);
}

How to add an customized button on the actionbar?

Another quick and clean way to go about this is specifying your own layout for that menu button. something like this

<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"
tools:context="com.example.MainActivity">

<item
android:id="@+id/action_custom_button"
android:title="Custom Button"
android:icon="@drawable/ic_custom_button"
app:actionLayout="@layout/layout_to_custom_button"
app:showAsAction="always" />

</menu>

there should be

layout_to_custom_button.xml

layout file which contains the padding and style you wanted.

and also do this in your activity for the click event

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_product_detail, menu);
final Menu mMenu = menu;
final MenuItem item = menu.findItem(R.id.action_custom_button);
item.getActionView().setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
mMenu.performIdentifierAction(item.getItemId(), 0);
}
});
return true;
}

Adding a button to the Action Bar in Android?

Try with this change:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/logout"
android:icon="@drawable/logout"
android:title="Logout"
android:orderInCategory="100"
yourapp:showAsAction="always" />
</menu>

how to use Buttons on action bar With Fragments?

You have to call setHasOptionsMenu(true) to make it work.

Do something like:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

and then use the onCreateOptionsMenu() method with the inflater.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu items for use in the action bar
inflater.inflate(R.menu.search_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}

Take a look at setHasOptionsMenu() and onCreateOptionsMenu().

Kotlin - Add action button to ActionBar

You need to override onCreateOptionsMenu function in your Activity like this:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menuTest, menu)
return true
}

To handle click events on menu items you need to override onOptionsItemSelected:

override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
R.id.action_fav -> {
// do stuff
true
}
else -> super.onOptionsItemSelected(item)
}

How to add button in ActionBar(Android)?

you have to create an entry inside res/menu,override onCreateOptionsMenu and inflate it

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.yourentry, menu);
return true;
}

an entry for the menu could be:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_cart"
android:icon="@drawable/cart"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>


Related Topics



Leave a reply



Submit