getActionView() always returning null - android

I am trying to add an EditText to my action bar. However when I try to search for the item using getActionView() it always returns null.
I just spent some 5 hours searching the web for a solution but all what I found relates to SearchView which does not apply to my case.
It seems that I need to use the MenuItemCompat.getActionView() because my minSDK is 8, but it always returns null.
Another solution suggested changing the proguard rules but that did not help neither.
Please help.
menu_email.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="il.co.talkie.q.SettingsEmailActivity">
<item
android:id="#+id/action_save"
android:title="#string/save_action"
android:orderInCategory="100"
app:showAsAction="always"
tools:ignore="AppCompatResource"/>
<item
android:id="#+id/action_err_email"
android:title="#string/email_err_prompt"
android:visible="true"
android:orderInCategory="1"
app:showAsAction="always"
tools:ignore="AppCompatResource"
android:actionLayout="#layout/menu_header"
>
</item>
</menu>
menu_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/simple_blue">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:text="#string/service_providers_menu_header"
android:layout_weight="1"
android:editable="false"
android:inputType="none" />
</LinearLayout>
and the relevant code from the activity
Java code
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_email, menu);
MenuItem email_err_menu = menu.findItem(R.id.action_err_email);
View v = (View) MenuItemCompat.getActionView(email_err_menu);
...
return true;
}
The object v above is always null

Try app:actionLayout="#layout/menu_header" instead of android:actionLayout="#layout/menu_header" if you are using support libraries.

Related

How to get a refrence to a switch inside the actionbar in android?

This is the code of the menu main_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newy"
>
<item
android:id="#+id/myswitch"
app:actionLayout="#layout/switch_layout"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/empty"
android:title="options"
android:enabled="true"
android:visible="true"
android:orderInCategory="101"
app:showAsAction="always"
android:icon="#drawable/ic_launcher_overflow1">
<menu>
<item android:id="#+id/action_help"
app:showAsAction="ifRoom"
android:title="help" />
<item
android:id="#+id/action_settings"
android:title="settings"
app:showAsAction="ifRoom" />
</menu>
</item>
</menu>
The first item of id "myswitch" is the switch,
this is it's actionlayout:
<?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"
android:paddingRight="18dp"
android:text=""
android:visibility="visible" />
</RelativeLayout>
In MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return true;
}
Is there a way to get a reference to this switch in onCreate?
I want full access to the switch not only when it is clicked(using onOptionsItemSelected which doesn't work by the way; it was unable to detect when the switch was pressed unless clickable is set to false).
I tried too many ways from similar questions but in vain...
The function of this button is to toggle the wifi (WifiManager.setWifiEnabled(false) or WifiManager.setWifiEnabled(true)).
When the program starts it should detect whether the wifi is initially turned on or off and set the switch to the corresponding state.

attempting to display icon with text android ActionBar

I am attempting to display icon with search bar in Android nav bar but it just keeps displaying just the icon. Here is my xml code
<item android:id="#+id/action_login"
android:title="Login"
android:icon="#android:drawable/ic_lock_lock"
app:showAsAction="always|withText" />
here the xml full code
<?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/action_login"
android:title="Login"
android:icon="#android:drawable/ic_lock_lock"
app:showAsAction="always|withText" />
</menu>
what could be wrong
Your code worked properly in Android 5.0 and above.
But in android 4.2 display only icon, not text!!
So if you worked on below 5.0 then use actionLayout.
Refer this answer:
How to get text on an ActionBar Icon?
Hope this will help you
Change this -:
android:icon="#android:drawable/ic_lock_lock"
To-:
android:icon="#drawable/ic_lock_lock"
or
android:icon="#mipmap/ic_lock_lock"
I have resolved this problem by following code:
1- Create menu in menu folder
<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/action_sign"
android:title="Sing In"
app:showAsAction="always|withText"
app:actionLayout="#layout/menu_sign" />
2- app:actionLayout is the key word, Then create layout named menu_sign and put textView in it. the icon will displayed by drawableLeft
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:drawableLeft="#mipmap/ic_user_sign"
android:gravity="center"
android:orientation="vertical"
android:text="#string/text"
android:drawablePadding="#dimen/margin_5"
android:paddingLeft="#dimen/margin_10"
android:paddingRight="#dimen/margin_10"
android:textColor="#android:color/white" />
3- Add listener for your menu
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_sign, menu);
signItem = menu.findItem(R.id.action_sign);
signItem.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onOptionsItemSelected(signItem);
}
});

Why am i getting NullPointerException when trying to use spinner in actionbar? [duplicate]

i am setting an actionLayout on a menu item and setting background color and image, but it's not respected. in my activity, i have:
getMenuInflater().inflate(R.menu.submit_action, menu);
my submit_action is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
android:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>
my check layout is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e8e8e8"
android:clickable="true"
android:contentDescription="lol" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerInside"
android:src="#drawable/ic_action_tick" />
</RelativeLayout>
but even with all of this setup, this is how the action bar appears, not showing my menuitem at all (but it's there, because it responds to the click, but doesn't appear):
Try app:actionLayout="#layout/check" instead of android:actionLayout="#layout/check".
If you're using ActionbarSherlock or AppCompat, the android: namespace will not work for MenuItems. This is because these libraries use custom attributes that mimic the Android APIs since they did not exist in earlier versions of the framework.
when using Appcompact ,menu item will be like
<item android:id="#+id/cart"
app:actionLayout="#layout/actionbar_cart"
android:title="#string/action_cart"
app:showAsAction="always"
/>
The answer from Ben Harris is absolutely correct. However, in some case like when using attributes like:
app:showAsAction="ifRoom|collapseActionView"
used in SearchView (in my case), the layout view isn't shown and that caused a lot of headache to me. It seems that collapseActionView is not supported with action view in appcombat. So consider this as well while doing your stuffs.
use app namespace instead of android and it will work fine.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
app:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>

actionlayout on menuitem does nothing

i am setting an actionLayout on a menu item and setting background color and image, but it's not respected. in my activity, i have:
getMenuInflater().inflate(R.menu.submit_action, menu);
my submit_action is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
android:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>
my check layout is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e8e8e8"
android:clickable="true"
android:contentDescription="lol" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerInside"
android:src="#drawable/ic_action_tick" />
</RelativeLayout>
but even with all of this setup, this is how the action bar appears, not showing my menuitem at all (but it's there, because it responds to the click, but doesn't appear):
Try app:actionLayout="#layout/check" instead of android:actionLayout="#layout/check".
If you're using ActionbarSherlock or AppCompat, the android: namespace will not work for MenuItems. This is because these libraries use custom attributes that mimic the Android APIs since they did not exist in earlier versions of the framework.
when using Appcompact ,menu item will be like
<item android:id="#+id/cart"
app:actionLayout="#layout/actionbar_cart"
android:title="#string/action_cart"
app:showAsAction="always"
/>
The answer from Ben Harris is absolutely correct. However, in some case like when using attributes like:
app:showAsAction="ifRoom|collapseActionView"
used in SearchView (in my case), the layout view isn't shown and that caused a lot of headache to me. It seems that collapseActionView is not supported with action view in appcombat. So consider this as well while doing your stuffs.
use app namespace instead of android and it will work fine.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
app:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>

Is it possible to Implement Toggle Button in Action Menu Item using Actionbar sherlock in android

I have an app, which have toggle button in action menu item, though i'm using Actionbar Sherlock, I don't know, how to place the toggle button in the action menu item. I don't want to place as a custom layout in action bar, but i want to place it as a Menu item. If anyone find solution, Please help me out.
Purpose, If I change the state of toggle button, it will sort the person based on ALphabets and again in Date of Birth.
Thanks in Advance!
Just add it like a normal Menu Button, check its state with a boolean variable, and you can change the icon and title when changing the sortmode
boolean birthSort=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_toggle:
if(birthSort){
//change your view and sort it by Alphabet
item.setIcon(icon1)
item.setTitle(title1)
birthSort=false;
}else{
//change your view and sort it by Date of Birth
item.setIcon(icon2)
item.setTitle(title2)
birthSort=true;
}
return true;
}
return super.onOptionsItemSelected(item);
}
Don't forget to add it in xml like any other menu button and configure android:showAsAction if you want to show it in overflow or outside of it.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_toogle"
android:showAsAction="ifRoom"
android:title="Share"
/>
</menu>
Other approach would be to use a custom layout for your ActionBar:
Basically you define a layout that contains your Toggle:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="#+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
ALTERNATIVE 1:
Then in your Activity or Fragment container you do:
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
ToggleButton button = (ToggleButton) findViewById(R.id.actionbar_service_toggle);
Notice that you are having a real ToggleButton and you are handling it in code as a real object ToggleButton, which has lots of advantages compared to having you re-implement your own toggle (theme, reliability, views hierarchy, native support...).
Source code here.
ALTERNATIVE 2:
Another way to do it is embed your custom view into a regular menu view:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="#layout/actionbar_service_toggle" />
</menu>
If like me the actionLayout isn't working for you, try app:actionLayout="#layout/actionbar_service_toggle" instead of android:actionLayout="#layout/actionbar_service_toggle" as well as app:showAsAction="always" instead of android:showAsAction="always"
This is because if you use appCompat the android namespace won't be used.
So here's the final version:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="#+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
and
<?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/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="#layout/actionbar_service_toggle" />
</menu>
create xml file in menu:
menu.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="si.ziga.switchinsideab.MainActivity">
<item android:id="#+id/switchId" android:title="" android:showasaction="always" android:actionlayout="#layout/switch_layout">
<item android:id="#+id/action_settings" android:orderincategory="100" android:title="#string/action_settings" app:showasaction="never">
</item></item></menu>
And then navigate to your layout folder make a new xml file
and name it switch_layout.xml
Here's the code:
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/switchAB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true">
</switch></relativelayout>
In your MainActivity class copy and paste this code:
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
switchAB = (Switch)menu.findItem(R.id.switchId)
.getActionView().findViewById(R.id.switchAB);7
Or follow this link about this stuff
Here somthing easy
<?xml version="1.0" encoding="utf-8"?>
<item
android:title="#string/mode"
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="56dp"
android:layout_marginTop="120dp"
app:showAsAction="ifRoom"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
android:checked="false"/>

Categories

Resources