I am using custom ActionBar library in my Android App. there i want to show a searchView but can not find any solution, i m working in library that is
https://github.com/johannilsson/android-actionbar
This is a class
private class ExampleAction extends AbstractAction {
public ExampleAction() {
super(R.drawable.ic_title_export_default);
}
#Override
public void performAction(View view) {
Toast.makeText(OtherActivity.this, "Example action",
Toast.LENGTH_SHORT).show();
}
}
//use for signle action how to use for multiactions
ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
actionBar.addAction(new ExampleAction());
actually i found code for searchbar but it is in .cs format how to use this searchview
https://github.com/zleao/MonoDroid.ActionBar
Thanks bro & sis
Take a look at this answer.
According to this answer SearchView is available support library.
add dependency to build.gradle
compile 'com.android.support:appcompat-v7:22.0.0'
and make imports
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
According to this answer it worked on API 8 on emulator. For more details, check the link with the question and answer itself.
Hope it helps.
UPDATE
By adding this code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hmkcode="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_action_search"
android:title="Search"/>
<item
android:id="#+id/action_copy"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_content_copy"
android:title="Copy"/>
<item
android:id="#+id/action_share"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_social_share"
android:title="Share"/>
you can achieve this:
You can customize items in the way you want. For more details see this example.
Related
I use the new NavigationView in one of my recent projects. However I have a problem for the update data.
Previously, I used a ListView in my DrawerLayout and when I needed to change my data I called notifyDataSetChanged() method of my Adapter.
Currently NavigationView does not notifyDataSetChanged() method and when I want to update an item on my menu nothing is happening, for example:
Menu menuAccount = navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu();
menuAccount.findItem(R.id.drawer_item_login).setVisible(!isLoggedIn);
Do you have a solution ? Thanks you for your help.
UPDATE: This was fixed in v23.0.0 so you don't need to do anything by youself, just update your dependency. Got from this answer: https://stackoverflow.com/a/32181083/2489474
Old solution (just to see how stupid it could be):
But I've found working solution in this answer https://stackoverflow.com/a/30604299/2489474. It uses reflection to call updateMenuView(boolean) of NavigationView's presenter.
I've modified code from the answer for my purposes. Check also a method from the answer and choose which one is better for you.
//HACK
private void updateNavigationView() {
try {
Field presenterField = NavigationView.class.getDeclaredField("mPresenter");
presenterField.setAccessible(true);
((NavigationMenuPresenter) presenterField.get(navigationView_)).updateMenuView(false);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
P.S. it is interesting that updateMenuView ignores a value was given to it.
#Moinkhan Thanks you for your helper but doesn't work for me. Here my menu_drawer.xml
<group
android:checkableBehavior="single"
android:id="#+id/group1">
<item
android:id="#+id/drawer_item_publications_list"
android:icon="#drawable/ic_drawer_publications_24dp"
android:title="#string/drawer_menu_item_publications" />
</group>
<group android:id="#+id/drawer_group_account">
<item
android:title="#string/drawer_menu_sub_item_account"
android:id="#+id/drawer_item_account">
<menu>
<item
android:icon="#drawable/ic_drawer_login_24dp"
android:id="#+id/drawer_item_login"
android:title="#string/drawer_menu_item_login" />
<group
android:id="#+id/group_actions_user">
<item
android:icon="#drawable/ic_drawer_add_publication_24dp"
android:id="#+id/drawer_item_add_publication"
android:title="#string/drawer_menu_item_add_publication" />
<item
android:icon="#drawable/ic_drawer_my_publications_24dp"
android:id="#+id/drawer_item_my_publications"
android:title="#string/drawer_menu_item_my_publications" />
<item
android:icon="#drawable/ic_drawer_edit_profil_24dp"
android:id="#+id/drawer_item_edit_profil"
android:title="#string/drawer_menu_item_edit_profil" />
<item
android:icon="#drawable/ic_drawer_delete_account_24dp"
android:id="#+id/drawer_item_delete_account"
android:title="#string/drawer_menu_item_delete_account" />
<item
android:icon="#drawable/ic_drawer_logout_24dp"
android:id="#+id/drawer_item_logout"
android:title="#string/drawer_menu_item_logout" />
</group>
</menu>
</item>
</group>
And my method to update my NavigationView
private void setUpNavigationDrawer()
{
boolean isLoggedIn = sessionManager.isLoggedIn();
navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().findItem(R.id.drawer_item_login).setVisible(!isLoggedIn);
navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().setGroupVisible(R.id.group_actions_user, isLoggedIn);
}
After some operations I called setUpNavigationDrawer() but the menu was not updated !
Instead of referencing group and then finding a item from that group and setting it's visibility try to reference the item directly like this ...
navView.getMenu().findItem(R.id.drawer_item_login).setVisible(!isLoggedin);
navView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().setGroupVisible(R.id.group_actions_user, !isLoggedin);
It works for me. I hope it helps you.
I would like to remove ActionBarSherlock from my application and replace it with the standard ActionBarCompat.
How do I implement ActionBarCompat?
How do I migrate the Activites?
Which imports replace the ActionBarSherlock imports?
What are typical problems?
I did some migrating and wrote down all the issues I encountered. None were serious but took time to research. I was able to migrate a quite big application in a couple hours after knowing all this. May this help to speed up migration process.
How do I convert from ActionBarSherlock to ActionBarCompat?
Note: Since the Support Library's v22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity instead. Read here for more information: What's the enhancement of AppCompatActivity over ActionBarActivity?
== Switch the libraries ==
Go to app properties and remove ActionBarSherlock and add ActionBarCompat instead. This requires the v7 appcompat library to be present, see http://developer.android.com/tools/support-library/setup.html for details. Follow the instructions precisely, ActionBarCompat needs to be a library project.
Parallel does not work (easily) as a lot of attributes are in both libraries.
Do not be discouraged by hundreds of errors after replacing the libraries. The vast majority goes away automatically.
== Fix XML errors ==
First thing is to fix all XML errors to allow compiling and find other errors.
Replace the sherlock theme with ActionBarCompat Theme, e.g.
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
Remove double attr, e.g. <attr name="buttonBarStyle" format="reference" />.
For now remove all your individual action bar styles. See further down how to handle these.
== Fix build errors ==
Pick the easiest activities first. ActionBarCompat does not distinguish Activity and FragmentActivity, both are now ActionBarActivity.
Remove the ActionBarSherlock imports and extend to ActionBarActivity (import android.support.v7.app.ActionBarActivity;)
After saving, this should dramatically reduce errors in the activity.
Fix the errors around the menues first and disregard fragment errors for now, they should be going away later.
== Replacements ==
Imports:
import com.actionbarsherlock.app.SherlockActivity; -> import android.support.v7.app.ActionBarActivity;
import com.actionbarsherlock.app.SherlockFragmentActivity; -> import android.support.v7.app.ActionBarActivity;
import com.actionbarsherlock.app.SherlockFragment; -> import android.support.v4.app.Fragment;
import com.actionbarsherlock.app.SherlockListFragment; -> import android.support.v4.app.ListFragment;
import com.actionbarsherlock.app.SherlockListActivity; -> import android.support.v7.app.ActionBarActivity; (see ListActivity / SherlockListActivity)
import com.actionbarsherlock.view.Menu; -> import android.view.Menu;
import com.actionbarsherlock.view.MenuItem; -> import android.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater; -> import android.view.MenuInflater;
import com.actionbarsherlock.view.Window; -> import android.view.Window;
import com.actionbarsherlock.widget.SearchView; -> import android.support.v7.widget.SearchView;
import com.actionbarsherlock.widget.SearchView.OnQueryTextListener -> import android.support.v7.widget.SearchView.OnQueryTextListener;
Code Replacements:
SherlockActivity -> ActionBarActivity
SherlockFragmentActivity -> ActionBarActivity
SherlockListActivity -> ListActivity (see ListActivity / SherlockListActivity)
SherlockListFragment -> ListFragment;
getSupportMenuInflater -> getMenuInflater
getSherlockActivity() -> getActivity()
com.actionbarsherlock.widget.SearchView.OnQueryTextListener() -> OnQueryTextListener (see SearchView)
m.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); -> MenuItemCompat.setShowAsAction(m, MenuItem.SHOW_AS_ACTION_ALWAYS);
Typical Code changes for ActionBarCompat
getActionBar() -> getSupportActionBar()
invalidateOptionsMenu() -> supportInvalidateOptionsMenu()
== Fragment ==
The fragment does not cater for ActionBarCompat functionality. This is a problem when trying to call getSupportActionBar.
This can be solved by using the onAttach method:
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((ActionBarActivity)activity).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
Usually this is better controlled in the FragmentActivity.
== SearchView ==
This turned out to be a bit of a hassle.
Replace something like this:
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
with
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
You also have to adjust your menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="80"
android:showAsAction="always|collapseActionView"
android:title="#string/action_search"/>
</menu>
with
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="80"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>
</menu>
app: needs to be defined to have compatibility with android versions before 11.
SearchView needs to be support class v7.
== ListActivity / SherlockListActivity ==
The ListActivity is not supported ActionBarCompat, therefore the crucial functions of the ListActivity need to be implemented manual, which is rather simple:
private ListView mListView;
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
}
return mListView;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
== Styles ==
A styled action bar can be achieved, see original google posting:
http://android-developers.blogspot.de/2013/08/actionbarcompat-and-io-2013-app-source.html
A styled searchView box is more difficult:
This works:
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchView.SearchAutoComplete theTextArea = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
theTextArea.setTextColor(getResources().getColor(R.color.yourColor));
See these posts:
Changing the cursor color in SearchView without ActionBarSherlock
Change appcompat's SearchView text and hint color
== Example ==
Google Navigation Drawer with Action Bar Sherlock includes all original code (now aiming to support library) and formatting. Only some attributes had to be replaced with similar ones as they are only available from v11 onwards.
Download at: https://github.com/GunnarBs/NavigationDrawerWithActionBarCompat
== See also ==
http://android-developers.blogspot.de/2013/08/actionbarcompat-and-io-2013-app-source.html
http://developer.android.com/reference/android/support/v7/app/ActionBar.html
http://www.grokkingandroid.com/migrating-actionbarsherlock-actionbarcompat/
It is worth mentioning that there is no support version of the PreferenceActivity, so if you are using SherlockPreferenceActivity, you need to refactor to a support PreferenceFragment.
More info: How to add Action Bar from support library into PreferenceActivity?
I am trying to implement the SearchView ActionBar item as android developers says but I am having some trouble.
(http://developer.android.com/guide/topics/ui/actionbar.html).
There are two mistakes that although I have looked for a lot, I have not been able to find the solution.
1) I have a problem with the class MenuItemCompat. It says:
The method getActionView(MenuItem) is undefined for the type MenuItemCompat
I can only use for this class the following methods:
setShowAsAction(item, actionEnum)
setActionView(item, view)
Here it is the code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.restloader, menu);
MenuItem searchItem = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
return super.onCreateOptionsMenu(menu);
}
2) There is a problem with this:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
I don't understand why it is used but if google says it, it must be appropriate.
Error message:
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'actionViewClass' in package
'com.example.pruebahttp3'
- error: No resource identifier found for attribute 'showAsAction' in package
'com.example.pruebahttp3'
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/search_menu"
android:orderInCategory="100"
android:title="#string/search"
android:icon="#drawable/ic_search_category_default"
myapp:showAsAction="ifRoom|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView">
</item>
Thank you very much!
i have got the same problem, i solved it by using the follow code. Be care of your namespace.`
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
share:showAsAction="ifRoom"
share:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/abc_searchview_description_search" />
`
For the 1st:Fixing the second one will fix this :)
For the 2nd:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
Change myapp to you application namespace com.xxx.xxx
Try to copy the lib files directly from yourFolder\sdk\extras\android\support\v7\appcompat\libs
I have a similar problem,but It occurs to me when i directly copy the JAR library file rather than following the android support library procedure. Try the opposite it might work for you.
Kinda weird if you ask me.
I added the SherlockActionBar to my project, added a few menu items and it works fine in the emulator (4.0.3), but it does not work on my phone (Galaxy S3); it just doesn't show up.
I'm targeting 15 with a minimum sdk version of 9 in my manifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/dimmiicon"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light.DarkActionBar" >
Code is rather straightforward, but perhaps i'm missing something. Relevant bits...
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
public class Launcher extends SherlockFragmentActivity implements OnUserAuthenticatedListener, OnDialogChatterListener, OnAnimationCompleteListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
final ActionBar ab = getSupportActionBar();
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ab.setCustomView(R.layout.title_bar);
ab.setStackedBackgroundDrawable(getResources().getDrawable(R.drawable.titlebarbackground));
TextView txt = (TextView) findViewById(R.id.myTitle1);
Typeface font = Typeface.createFromAsset(getAssets(), "caviar_dreams_bold.ttf");
txt.setTypeface(font);
TextView txt2 = (TextView) findViewById(R.id.myTitle2);
Typeface font2 = Typeface.createFromAsset(getAssets(), "caviardreams.ttf");
txt2.setTypeface(font2);
}
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.commonmenus, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return false;
case R.id.search_for_product:
performProductReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_business:
performCompanyReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_service:
performServiceReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_person:
performPersonReview(findViewById(android.R.id.content));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the commonmenus.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item android:id="#+id/search_for_product" android:showAsAction="never" android:orderInCategory="10" android:title="#string/menu_search_for_product"></item>
<item android:id="#+id/search_for_business" android:showAsAction="never" android:orderInCategory="11" android:title="#string/menu_search_for_business"></item>
<item android:id="#+id/search_for_service" android:showAsAction="never" android:orderInCategory="12" android:title="#string/menu_review_service"></item>
<item android:id="#+id/search_for_person" android:showAsAction="never" android:orderInCategory="14" android:title="#string/menu_review_person"></item>
<item android:id="#+id/view_my_reviews" android:showAsAction="never" android:orderInCategory="15" android:title="#string/menu_view_my_reviews"></item>
</menu>
I'm using the sherlock actionbar to customize the title using some custom font's and that is working on the emulator AND the phone, but the menu only shows up on the emulator. I did try it with the custom font code commented out and it made no difference.
Just so I'm clear: the 3 menu dots don't even show up.
After spending a couple of hours searching and looking for a clue, I'm at a loss. Any insight would be appreciated.
For devices with Android Honeycomb+ ActionBarSherlock will use stock ActionBar features to work with, which means you're left with official ActionBar, which only puts the legacy menu button in very specific scenarios, see here:
http://android-developers.blogspot.co.il/2012/01/say-goodbye-to-menu-button.html
Under "Action overflow button for legacy apps":
If your app runs on a device without a dedicated Menu button, the
system decides whether to add the action overflow to the navigation
bar based on which API levels you declare to support in the
manifest element. The logic boils down to:
If you set either minSdkVersion or targetSdkVersion to 11 or higher,
the system will not add the legacy overflow button.
Otherwise, the system will add the legacy overflow button when running
on Android 3.0 or higher.
The only exception is that if you set minSdkVersion to 10 or lower,
set targetSdkVersion to 11, 12, or 13, and you do not use ActionBar,
the system will add the legacy overflow button when running your app
on a handset with Android 4.0 or higher.
I am developing a simple demo . Here in this demo, I am just creating one simple custom alert dialog . It works fine.
It shows me the perfect result when i build application in 1.6, but when i change the android version from 1.6 to 2.2, it shows the unexpected result. It doesn't show the background screen on which i display the custom alert dialog.
Here is my xml file. Custom Dialog Theme File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="#android:style/AlertDialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">#android:style/Theme.Dialog</item>
</style>
</resources>
Here is My CustomConfirmOkDialog Class
package com.utility.org;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class CustomConfirmOkDialog extends Dialog implements OnClickListener
{
private Button okButton = null;
private TextView infoText=null,confirmBody=null;
private int errorMessage=0;
#SuppressWarnings("unused")
private Activity activity;
public CustomConfirmOkDialog(Activity context,int customdialogtheme,int errorMessage)
{
super(context,customdialogtheme);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.confirm_ok);
this.errorMessage = errorMessage;
this.activity = context;
initControls();
}
private void initControls()
{
okButton = (Button) findViewById(R.id.ok_button);
okButton.setOnClickListener(this);
infoText = (TextView)findViewById(R.id.infoText);
confirmBody = (TextView)findViewById(R.id.confirmBody);
switch (this.errorMessage)
{
case Utility.INVALID_USERNAME_PASSWORD:
try
{
infoText.setText(R.string.signIn);
confirmBody.setText(R.string.invalidUsernameAndPassword);
}
catch (Exception e)
{
e.printStackTrace();
}
break;
default:
break;
}
}
public void onClick(View v)
{
dismiss();
}
}
Calling this class from my main activity using the below code.
CustomConfirmOkDialog dialog = new CustomConfirmOkDialog(MainActivity.this, R.style.CustomDialogTheme, Utility.INVALID_USERNAME_PASSWORD);
dialog.show();
Here you can clearly notice that 1st image shows the background . Its build in android 1.6 version while 2nd image doesn't shows the background . It shows the entire black screen. Its build in android version 2.2 . I am very thankful if anyone can solve this issue.
Can anyone help me to solve this simple and silly issue ?
Thanks in Advance.
It resolved my problem by changing the following code in Custom Dialog Theme xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="#android:style/Theme.Translucent.NoTitleBar">
<item name="android:windowFrame">#null</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
I also faced the same problem. the problem is when I called constructor of Dialog class
Dialog(Context context, int themeId)
it will hide the background activity. The only solution that i found is don't call this constructor, instead only call
Dialog(Context context)
and set your style in the layout file.
So in your code, only write
super(context)
instead of
super(context, themeid);
Apparently, this is a known issue.
This only happens when you try inheriting from the framework themes.
Using #android:style directly will still treat them as non-
fullscreen, which punches through the black background as expected.
One workaround is to start with a nearly-blank theme (like Panel or
Translucent) and then render what you need in your own layout (such as
dialog edges, etc).
Thought, I still don't fully understand this solution myself yet.
And actually, I'm no longer sure they're talking about the exact same bug you've seen, since they're talking about it not working for an older version of the sdk (not a newer one like yours). See the bug report.