android - How to get onClickListener() event on custom actionbar -
i'm developing application in have onclick()
event on click of actionbar custom view. far i'm able achieve following layout.
here code 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 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 trying have click listener on custom layout. have tried 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 onclick()
event on custom image. i'm doing wrong here, please guide.
any kind of appreciated.
after in inflater view in layout file
so, have add android:onclick="clickevent"
in actionbar
custom layout file
here 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 menu; adds items action bar if 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>
Comments
Post a Comment