In android how to set views of "action buttons" in actionbar -
my requirement is:--
and when click search button want :- here code:--
snacks.java
public class snacks extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_snacks); getactionbar().setbackgrounddrawable(new colordrawable(color.parsecolor("#33b5e5"))); getactionbar().settitle("snacks"); getactionbar().seticon(r.drawable.ic_launcher); getactionbar().sethomebuttonenabled(true); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main_actions, menu); menuitem searchmenuitem = menu.finditem(r.id.action_search); searchview searchview = (searchview) menuitemcompat.getactionview(searchmenuitem); searchview.seticonified(true); int searchimgid = getresources().getidentifier("android:id/search_button", null, null); imageview searchimg = (imageview) searchview.findviewbyid(searchimgid); searchimg.setimageresource(r.drawable.ic_action_search); int searchdeleteimgid = getresources().getidentifier("android:id/search_close_btn", null, null); imageview searchdeleteimg = (imageview) searchview.findviewbyid(searchdeleteimgid); searchdeleteimg.setimageresource(r.drawable.ic_action_delete); int searchsrctextid = getresources().getidentifier( "android:id/search_src_text", null, null); edittext searchedittext = (edittext) searchview.findviewbyid(searchsrctextid); searchedittext.settextcolor(color.white); searchedittext.sethinttextcolor(color.ltgray); searchedittext.sethint("search here"); searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string s) { toast.maketext(getapplicationcontext(), "entered keyword " + s,toast.length_long).show(); return true; } @override public boolean onquerytextchange(string s) { return false; } }); return true; } }
activity_main_actions.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:actionviewclass="android.widget.searchview" android:icon="@drawable/ic_action_search" android:showasaction="always" android:title="search"/> <item android:id="@+id/action_contact" android:title="contacts" android:showasaction="always"/> <item android:id="@+id/settings" android:title="contacts" android:showasaction="never"/> </menu>
styles.xml
<!-- base application theme, dependent on api level. theme replaced appbasetheme res/values-vxx/styles.xml on newer devices. --> <style name="appbasetheme" parent="android:theme.light"> <!-- theme customizations available in newer api levels can go in res/values-vxx/styles.xml, while customizations related backward-compatibility can go here. --> </style> <!-- application theme. --> <style name="apptheme" parent="appbasetheme"> <!-- customizations not specific particular api-level can go here. --> </style> </resources>
but when run program got this:-- here,the appname snacks not showing..and when click searchview got:--
this searchview expanded over. wrong in code cannot achieveing requirement..
there setmaxwidth(int maxpixels) method can use on searchview.
searchview.setmaxwidth(100);
try different sizes, not sure enough :)
also if contacts button isn't required there time switch in xml for
<item android:id="@+id/action_contact" android:title="contacts" android:showasaction="ifroom"/>
Comments
Post a Comment