NullPointerException after replaceing actionbar with toolbar - android

I'm using badge layout in one menu item
here is some menuItems
<item
android:id="#+id/action_rdvmed"
android:title="#string/rdv"
android:icon="#drawable/ic_action_action_event"
android:showAsAction="always" />
<item
android:id="#+id/action_msg"
android:title="#string/msg"
android:actionLayout="#layout/badge_layout" <!--this one causes the problem-->
android:showAsAction="always" />
<item
android:id="#+id/action_actualiser"
android:title="#string/actualiser"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="always" />
i'm getting a null pointer exception in my onCreateOptionMenu
#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_activity_med, menu);
this.MenuMed = menu;
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.action_msg).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.hotlist_hot); //the error in this line
imgMessage = (ImageView) badgeLayout.findViewById(R.id.hotlist_bell);
badgeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listMessages.clear();
msg();
}
});
return true;
}
and here is the part of muy onCreate methode whare i'm using toolbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_med);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
thanks in advance.

I believe you're facing the same problem as one user faced here.
The trick is you need to change your android:actionLayout to app:actionLayout.
So, change your
<item
android:id="#+id/action_msg"
android:title="#string/msg"
android:actionLayout="#layout/badge_layout"
android:showAsAction="always" />
to,
<item
android:id="#+id/action_msg"
android:title="#string/msg"
app:actionLayout="#layout/badge_layout"
android:showAsAction="always" />

Related

Put an icon in the Toolbar

I have a toolbar that has only the menu icon, and it is setted programmatically, like this:
myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
myToolbar.setTitleTextColor(Color.BLACK);
ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.menu);
ab.setDisplayHomeAsUpEnabled(true);
Now, i would like to have another icon in the toolbar, and associate to it my src image and onClick Method. As you can see I don't have a xml menu file, and i also don't want to set a default android icon, so how can I perform this?
The main purpose of Toolbar is using custom layout!
You can add whatever UI elements, like button, to your Toolbar and find them in code.
You can set a CustomView to the ActionBar by supplying a layout file or View object. In that custom view you can do whatever you want.
Here is how you can set custom view to the action bar
protected void setCustomActionBarTitle(String title) {
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.layout_action_bar_title);
TextView titleView = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title);
titleView.setText(title);
ImageView customIconImageView = (ImageView) actionBar.getCustomView().findViewById(R.id.custom_icon);
customIconImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call my method on the activity
}
});
ImageView imageView = (ImageView) actionBar.getCustomView().findViewById(R.id.up_icon);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
You're going to have to use a separate xml file to setup your menu and define any extra buttons (item) in there. Here is sample menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="#+id/action_favorite"
android:icon="#drawable/ic_favorite_black_48dp"
android:title="#string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
Add the icon which you want in your menu --> 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=".ui.MainActivity">
<item
android:id="#+id/action_scan"
android:icon="#drawable/camera_icon"
android:orderInCategory="100"
android:title="#string/action_scan"
android:visible="true"
app:showAsAction="always" />
and in your activity
#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;
}
This code is work for me,
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Use this to add your icons into your tool bar. Override these methods and use the code in your project..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_about));
menu.getItem(1).setIcon(getResources().getDrawable(R.drawable.street_view_icon));
return true;
}
//Notification Icon
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_notify_found:
Intent aboutIntent=new Intent(DashboardActivity.this, HomeAboutActivity.class);
startActivity(aboutIntent);
case R.id.action_notify_found1:
Intent ARStreetviewintent = new Intent(DashboardActivity.this, ARStreetviewActivity.class);
startActivity(new Intent(DashboardActivity.this, StreetviewActivity.class));
default:
return super.onOptionsItemSelected(item);
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Notification Found -->
<item android:id="#+id/action_notify_found"
android:icon="#drawable/about"
android:title="About"
app:showAsAction="ifRoom"
/>
<!-- Notification Found -->
<item android:id="#+id/action_notify_found1"
android:icon="#drawable/street_view_icon"
android:title="StreetView"
app:showAsAction="ifRoom"
/>

Adding Action Icons to toolbar in an activity that extends ListActivity

I am trying to add action icons to my toolbar but in vain. I have followed all the suggestion from this thread.
Maybe the problem is that my Activity doesn't extend ActionBarActivity directly because it already extends ListActivity as follows:
public class ShopsCatalogueActivity extends ListActivity {
private Toolbar toolbar; // Declaring the Toolbar Object
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//using delegates because this activity already extends ListActivity and we want to add toolbar
AppCompatCallback callback = new AppCompatCallback() {
#Override
public void onSupportActionModeStarted(ActionMode actionMode) {
}
#Override
public void onSupportActionModeFinished(ActionMode actionMode) {
}
#Nullable
#Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
};
AppCompatDelegate delegate = AppCompatDelegate.create(this, callback);
delegate.onCreate(savedInstanceState);
//get the layout
delegate.setContentView(R.layout.activity_shops_catalogue);
//adding toolbar following material design approach
toolbar= (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
delegate.setSupportActionBar(toolbar);
...
}
here is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyOwnAppNameSpace="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
MyOwnAppNameSpace:showAsAction="never" />
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:orderInCategory="200"
android:title="Search"
MyOwnAppNameSpace:showAsAction="always"></item>
<item
android:id="#+id/action_user"
android:icon="#drawable/ic_user"
android:orderInCategory="300"
android:title="User"
MyOwnAppNameSpace:showAsAction="ifRoom"></item>
</menu>
In the Activity:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shops_catalogue_toolbar_menu, menu);
return true;
}
In build.gradle I am using:
compile 'com.android.support:appcompat-v7:22.2.0'
One approach you could use is to use a FragmentActivity as your base activity then have it load in a ListFragment. In the list fragment you can simply add this line in the onCreate method:
getActivity().getActionBar().setIcon(R.drawable.ic_main);
However Budius is right you should use a RecyclerView, its a new android class which simplifies the old viewholder pattern.
Android List and Card Guide ,
RecyclerView Android Doc

MenuItemCompat.getActionView(searchItem) return null

I am trying to add actions items in my action bar using appcompat. I added search action when I click on it my application crash with NullPointerException. In onCreateOptionsMenu I am getting this view reference but there I am always getting null. Any suggestion to resolve this ??
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_bar_refresh"
android:icon="#drawable/ic_action_refresh"
app:showAsAction="ifRoom"
android:title="#string/action_refresh" />
<item
android:id="#+id/action_bar_search"
android:icon="#drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:title="#string/action_search" />
<item
android:id="#+id/action_bar_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_bar_search:
mSearchView.setIconified(false);
return true;
}
return false;
}
}
Here I am getting null value in searchView
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
Screenshot
<item
android:id="#+id/action_bar_search"
android:icon="#drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"
android:title="#string/action_search" />
You change showAsAction attribute.
I fixed issue by setting Application Theme to Theme.AppCompat.Light
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light">

Android - ActionBar item - onClickListener

I'm trying to implement onClickListener for the item which is a submenu of the ActionBar. Whatever I'm trying to do the result is the same - "Unfortunatelly, application has stopped." However there are no errors during compilation. All seems to be ok, but it is't. What goes wrong here? Thanks for help.
This is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
View view = (View) menu.findItem(R.id.delete).getActionView();
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Execute when actionbar's item is touched
}
});
return true;
}
And here is the main.xml file where ActionBar and its item is created
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:id="#+id/delete"
android:title="#string/delete"
android:showAsAction="always"
android:orderInCategory="200"/>
</menu>
</item>
</menu>
getActionView() returns a valid object (not null) only you have a custom action view (with setActionView)

MultiSelect Menu in Action Bar Sherlock

I wan to have one action bar with one "menu" button. (Until here I know how to do) But Now I want to do that, when the users Clicks on any subitem of the menu, don't close this menu.
Said in other words, I want to enable the multiselect menu in action bar sherlock, but I dont know how to do it.
Can someone explain me some way to achive it?
Thats what I have
public class MainActivity extends SherlockActivity{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setTitle("Testing");
actionBar.setSubtitle("test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
And this is the XML Menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu"
android:orderInCategory="0"
android:title="Menu"
android:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/subitem1"
android:title="SubMenu1"/>
<item
android:id="#+id/subitem2"
android:title="SubMenu2"/>
<item
android:id="#+id/subitem3"
android:title="SubMenu3"/>
<item
android:id="#+id/subitem4"
android:title="SubMenu4"/>
</menu>
</item>
</menu>

Categories

Resources