Search code examples
androidonclicklistenerandroid-toolbar

Add onclick listener to icons on toolbar


[my tool bar][1]

Do anyone have any solution?


Solution

  • I've seen your toolbar. Here is my solution:

    • First create a menu xml resource file. (This will contain your "heart icon", res/menu/menu_example.xml)

    Name it as you like, For example purpose I will name it menu_example.

    menu_example.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
           android:id="@+id/action_addfav"
           android:title=""
           app:showAsAction="always"
           android:icon="@drawable/YOUR_ADDFAV_DRAWABLE" />
    </menu>
    
    • Then create your activity code

    activity_example.java

    package com.example;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    
    public class activity_example extends AppCompatActivity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            //Layout xml init
            setContentView(R.layout.activity_example);
    
            //Actionbar
            //Back button
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
    
    
        //Inflate the menu
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_example, menu);
            return true;
        }
    
        //Handling Action Bar button click
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle item selection
            switch (item.getItemId()) {
                //Back button
                case android.R.id.home:
                    //If this activity started from other activity
                    finish();
    
                /*If you wish to open new activity and close this one
                startNewActivity();
                */
                    return true;
                case R.id.action_addfav:
                    //addfav (heart icon) was clicked, Insert your after click code here.
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    
        private void startNewActivity(){
            Intent intent = new Intent(this,ACTIVITY_YOU_WANT_TO_START.class);
            startActivity(intent);
            finish();
        }
    
    }
    
    • Activity layout xml

    (You can change the root layout to whatever layout you want)

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <!-- YOUR LAYOUT HERE -->
        </RelativeLayout>
    
    • Last thing, make sure your app has toolbar in the theme go to res/values/styles.xml

    styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <! -- Parent got to have actionbar in order for toolbat to appear -->
    
    </style>
    

    If you wish that the toolbar will be only in one specific activity take a look at: Apply a theme to an activity in Android?