-1

I would like to know , how to add custom buttons to the activitybar and set OnClick method to it? If someone know the answer, can he/she please post it to me...

Like this:

enter image description here

Onhar
  • 49
  • 1
  • 1
  • 7

2 Answers2

0

You should read through this, and as mentioned before, use toolbar. This way adding a Button becomes way more easy.

Martin Jäkel
  • 322
  • 6
  • 21
0

you should create a menu xml resource, like this (let's call it example_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_one"
        android:icon="@drawable/one"
        android:title="actione one"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_two"
        android:icon="@drawable/two"
        android:title="actione two"
        app:showAsAction="ifRoom" />

</menu>

then in your activity, add this:

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

if you want your items to always be visible as icons, just change app:showAsAction="ifRoom" to app:showAsAction="always"

Shalev Moyal
  • 644
  • 5
  • 12
  • But how to add a action (onclick method) to that? – Onhar May 26 '16 at 05:17
  • I should do what...? – Onhar May 26 '16 at 05:34
  • You should do something like this: http://pastebin.com/f5BUAVDG – Shalev Moyal May 26 '16 at 05:39
  • I mean i need a button to the activity bar that like has OnClick Method and that link doesnt help me at all... – Onhar May 26 '16 at 11:46
  • Of course it is. The function I gave you is called when one of the menu items is clicked. I'm using switch-case to determine which button was clicked. In the xml I gave you above I gave id's to the items. Then in the function that I put on pastebin.com I check which of the id's in the xml belong to the item that was pressed. – Shalev Moyal May 26 '16 at 13:11
  • Oh... I didnt realize that.... Thanks! – Onhar May 26 '16 at 13:12