android - How to open New activity on click on Button in Action Bar? -
this actionbar
, has 2 buttons:
private void showactionbar() { layoutinflater inflator = (layoutinflater) .getsystemservice(context.layout_inflater_service); view v = inflator.inflate(r.layout.activity_main_actions, null); actionbar actionbar = getactionbar(); actionbar.setdisplayhomeasupenabled(false); actionbar.setdisplayshowhomeenabled(false); actionbar.setdisplayshowcustomenabled(true); actionbar.setdisplayshowtitleenabled(false); actionbar.setcustomview(v); }
i used method show buttons. called method in oncreate.
now want when click on button in action bar new activity open.
for example have askactivity.java , messageactivity.java
now when click on ask button askactivity.java opens.
is possible?
i have used not working.
@override public boolean onoptionsitemselected(menuitem item) { // take appropriate action each action item click switch (item.getitemid()) { case r.id.action_ask: intent = new intent(getapplicationcontext(), askactivity.class); startactivity(i); return true; case r.id.action_message: intent ij = new intent(getapplicationcontext(), messageactivity.class); startactivity(ij); return true; default: return super.onoptionsitemselected(item); } }
i think it's because onoptionsitemselected
method related menuitem
, not customview
. 2 buttons are not option menu items, buttons inside layout activity_main_actions
. have 2 choices - either create new on click listener, follows:
button action_ask = (button) findviewbyid(r.id.action_ask); action_ask.setonclicklistener(new onclicklistener() { @override public void onclick(view v){ // } }
or, use on click attribute method:
<button android:id="@+id/action_ask" ... android:onclick="actionaskclicked" />
and inside activity:
public void actionaskclicked() { // }
same other button action_message
. hope helps.
Comments
Post a Comment