How to get onClickListener() event on custom actionbar

27,828

Solution 1

after in Inflater its just like view in layout file

so, you have to add android:onClick="clickEvent" in ActionBar custom layout file

enter image description here

here is demo:

my mainactivity:

package com.example.testdemo;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private View viewList;
    private Dialog dialogMarketList;
    String a[] = { "a", "aa" };
    private View header;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        View cView = getLayoutInflater().inflate(R.layout.header, null);
        actionBar.setCustomView(cView);

    }

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

    public void clickEvent(View v) {
        if (v.getId() == R.id.button1) {
            Toast.makeText(MainActivity.this, "you click on button1",
                    Toast.LENGTH_SHORT).show();
        }

        if (v.getId() == R.id.button2) {
            Toast.makeText(MainActivity.this, "you click on button2",
                    Toast.LENGTH_SHORT).show();
        }

        if (v.getId() == R.id.textView1) {
            Toast.makeText(MainActivity.this, "you click on textView1",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

my layout header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickEvent"
        android:text="Button 1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickEvent"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:text="My action bar"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickEvent"
        android:text="Button 2" />

</LinearLayout>

Solution 2

    View cView = getLayoutInflater().inflate(R.layout.header, null);
    cView.findViewById(R.id.btn_id).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
                         // Do stuff here.
        }
    });
    actionBar.setCustomView(cView);
Share:
27,828
Anupam
Author by

Anupam

Android Developer

Updated on May 20, 2020

Comments

  • Anupam
    Anupam almost 4 years

    I'm developing an application in which I have to get onClick() event on click of actionbar custom view. So far I'm able to achieve the following layout.

    Custom Action Bar

    Here is my code for achieving this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    
        getActionBar().setCustomView(R.layout.custom_image_button);
        getActionBar().setDisplayOptions(
                ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
    
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
    
        case android.R.id.home:
            Toast.makeText(getApplicationContext(), "Clicked on ActionBar",
                    Toast.LENGTH_SHORT).show();
    
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    

    Here is my custom_image_button layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >
    
        <TextView
            android:id="@+id/points"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/points_yellow"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:textColor="#887141"
            android:textIsSelectable="false"
            android:textSize="22sp"
            android:textStyle="bold" >
        </TextView>
    
        <ImageView
            android:id="@+id/badge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|right"
            android:layout_marginRight="5dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/badge_notification" >
        </ImageView>
    </FrameLayout>
    
    </RelativeLayout>
    

    I was trying to have a click listener on the custom layout. For that I have tried the following code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    
        getActionBar().setCustomView(R.layout.custom_image_button);
        getActionBar().setDisplayOptions(
                ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
    
        final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        View v = inflater.inflate(R.layout.custom_image_button, null);
    
        frameLayout = (FrameLayout) v.findViewById(R.id.frame_layout);
    
        frameLayout.setOnTouchListener(new View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(getApplicationContext(), "Clicked on 1",
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
    
        case android.R.id.home:
            Toast.makeText(getApplicationContext(), "Clicked on ActionBar",
                    Toast.LENGTH_SHORT).show();
    
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    

    }

    But, I'm unable to get onClick() event on the custom image. What I'm doing wrong here, please guide.

    Any kind of help will be appreciated.

  • anddev
    anddev almost 11 years
    Hey I am using DrawerLayout in my main xml file and then for Actionbar I am using custom layout as per you suggest. But when I am trying with your code my DrawerLayout is not working because of actionBar.setDisplayShowCustomEnabled(true); and if I am not write this line then I am not able to see the custom layout in my action bar. Please suggest me the right way. I want to use both together. Thanks.
  • anddev
    anddev almost 11 years
    please see this stackoverflow.com/questions/17161609/… Do you have any idea about this question? If I found the solution of this question then no need to use custom layout. Please help me I am stuck on this. Thanks.
  • Mahdi
    Mahdi over 9 years
    but i give error that say not such method ( my click method) and app closes why?
  • Mahdi
    Mahdi over 9 years
    yes I do that, but I don't know why inflated layout can not reach that. I create custom view for my action bar in this way: @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSherlock().getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); menu.add(Menu.NONE, 0, Menu.NONE, "custom") .setActionView(R.layout.header) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return true; }
  • Mahdi
    Mahdi over 9 years
    @DhawalSodhaParmar did you see?