I found this post How to add a switch to android action bar? and this works for me. But I can't get its event. I'm using appcompat, and I used app namespace for actionLayout and showAsAction, but I'm not able to handle its click on onOptionsItemSelected method?. My app is crashing on start up. how to handle it or what is wrong with this code?
here is my code
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item
android:id="#+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="#layout/switch_layout"/>
switch_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
</RelativeLayout>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
MenuItem menuItem= menu.findItem(R.id.myswitch);
Switch switcha = (Switch)menuItem.findViewById(R.id.switchForActionBar);
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do anything here on check changed
}
});
return super.onCreateOptionsMenu(menu);
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.myswitch) {
//do something
}
return super.onOptionsItemSelected(item);
}
Any help is highly appreciated. Thanks in advance.
I found my error. I have to use
View view = MenuItemCompat.getActionView(menuItem);
extra line in onCreateOptionsMenu to find out the switch button
. Here is my full method
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.myswitch);
View view = MenuItemCompat.getActionView(menuItem);
Switch switcha = (Switch) view.findViewById(R.id.switchForActionBar);
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do anything here on check changed
}
});
return super.onCreateOptionsMenu(menu);
}
Related
I have a switch in my app toolbar to provide an option to the user to change the language.
When I am using the switch, the onCheckedChanged method is not working.
Further, it looks to me that onOptionsItemSelected is also not being hit as I don't see any log message on the screen.
Please note that the app is not crashing when I use the switch.
The clicks on options present inside the overflow menu are working properly. I am getting the log in those cases.
Switch aSwitch;
RelativeLayout langSwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = this;
// Removed non relevant code here.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.d("tag_id",String.valueOf(id));
langSwitch = findViewById(R.id.app_bar_switch);
aSwitch = langSwitch.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.savedPost){
Intent intent = new Intent(this,SavedPost.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Below is my menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app_name.android.app_name.MainActivity">
<item
android:id="#+id/app_bar_switch"
android:orderInCategory="2"
android:title=""
app:actionLayout="#layout/switch_item"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/savedPost"
android:checkable="false"
android:orderInCategory="4"
android:title="#string/saved_post" />
</menu>
This is my layout file for switch item.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="#+id/langSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textOff="#string/switch_text_off"
android:textOn="#string/switch_text_on"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
Additional information.
If I create another menu item (code below) with showAsAction 'always' and click on this once, now when I use the switch the Toast message comes. I am clueless here as to why is it not happening for the first time. Also, how do I make it work without this menu item.
<item
android:id="#+id/english"
android:orderInCategory="3"
android:title="#string/switch_text_on"
app:showAsAction="always" />
To inflate an external layout over ActionBar that seems to first creating a view that holds your ChildView for your layout
like Example
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
Try this in your code
Switch aSwitch;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
View sw = menu.findItem(R.id.app_bar_switch).getActionView();
aSwitch = (Switch) sw.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
return true;
}
Try
(compoundButton.isChecked())
In boolean
Or
just try another version of setonCheckedChangedListner
Example:
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){#Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (!compoundButton.isSelected()) { Log.i("Yeah" , "Is Not Selected"); invertLock(-1); } else { if (Utilities.isLockEnabled(context)) { Log.i("Yeah" , "Is Locked"); Utilities.showLockEnabled(context); } else { Log.i("Yeah" , "Is Not Locked"); invertLock(1); } } }
I have a menu that has checkbox menu item type and whenever i check it. It doesn't trigger anything. Here is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/delete"
android:actionViewClass="android.widget.CheckBox"
android:checkableBehavior="single"
android:title="All"
android:titleCondensed="All"
app:showAsAction="ifRoom"></item>
</menu>
Here is my onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
Toast.makeText(this,"Hello",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Any idea?
You can handle CheckBox click inside onCreateOptionsMenu by listen setOnCheckedChangeListener like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
CheckBox checkBox = (CheckBox) menu.findItem(R.id.delete).getActionView();
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// perform logic
}
}
});
return true;
}
And remember change android:actionViewClass="android.widget.CheckBox" to app:actionViewClass="android.widget.CheckBox"
I want to add a searchview and checkbox into action bar menu. And this checkbox will be visible if searchview is opened. And in it's opposite case it will be hidden. How I can do this?
I do something below . But it doesnt work correctly. I want hide checkbox (In my notes) when searchview is closed.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_button"
android:icon="#drawable/ic_icon_search"
android:title="Arama"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/search_in_my_notes_checkbox"
app:showAsAction="ifRoom"
android:title="#string/search_in_my_notes"
android:checkable="true"
android:visible="false"
/>
</menu>
HomeActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.search_button){
MenuItem searchInMyNotesCheckbox = (MenuItem)menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
I found an alternative solution. I will use two checkbox image with checked and unchecked. And I will override onCreateOptionsMenu like this :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = (MenuItem)menu.findItem(R.id.search_button2);
final MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
MenuItemCompat.setOnActionExpandListener(search,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(false);
return true;
}
});
searchInMyNotesCheckbox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() == R.drawable.checked_checkbox){
item.setIcon(R.drawable.unchecked_checkbox);
}
else {
item.setIcon(R.drawable.checked_checkbox);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
I have a menu which is inflated from main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/act_sync"
android:showAsAction="always"
android:actionLayout="#layout/sync_action"
android:icon="#android:drawable/ic_popup_sync" />
</menu>
and here is the code in the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
But onOptionsItemSelected is not called when I touch the menu item. When I remove the actionLayout attribute of the menu item, it works fine. How can I fix this? Thanks.
you should use below snippet ( Just for reference )
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
sync();
}
});
return true;
}
In addition to the answer provided by Vipul Shah.
Make sure you disable the clickable option of all items in your action layout:
android:clickable="false"
Otherwise, it may steal the click so that you still cant receive onClick call back.
I have added Share option to my ActionBarSherlock, this way:
public boolean onCreateOptionsMenu(final Menu menu) {
menu.add("Share")
.setIcon(R.drawable.ic_title_share_default)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
and on clicking this Icon i want to do something. how can i track the click on this ShareIcon??
You should create an XML file to define menu items.
For example mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/share"
android:icon="#drawable/ic_title_share_default"
android:title="#string/share"
android:showAsAction="ifRoom"/>
</menu>
Then in the onCreateOptionsMenu you will do:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
To handle the item selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
//do something for share
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can see more information in here:
http://developer.android.com/guide/topics/ui/menus.html
http://actionbarsherlock.com/usage.html