Android: Sherlock action bar drop down - android

I'm trying to implement a drop down list as navigation for the action bar in Android.
I can see the drop down list and the items, but I can't get the clicking event.
I'm not sure what I'm missing since I was following the tutorial in http://developer.android.com/guide/topics/ui/actionbar.html
This is my code:
public void onCreate(Bundle savedInstanceState) {
OnNavigationListener mOnNavigationListener;
super.onCreate(savedInstanceState);
// setContentView(R.layout.info_layout);
// getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(getSupportActionBar().NAVIGATION_MODE_LIST);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.navigation_array, android.R.layout.simple_dropdown_item_1line);
mOnNavigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch (itemPosition) {
case 1:
Intent i = new Intent();
i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
// return super.onOptionsItemSelected(itemPosition);
return true;
}
};
getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
}
Thanks a lot in advance!

Are you sure that you don't get click events? You're creating intent but doesn't do anything with it. Try something like this:
switch (itemPosition) {
case 1:
Intent i = new Intent();
i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
startActivity(i);
break;
...
}
or add writing to log to be sure:
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Log.d("SomeTag", "Get click event at position: " + itemPosition);
switch (itemPosition) {
...
}
}
and see in the logcat output for message with "SomeTag" when you click on items.

I think the return statement must be false inside the switch case, and your case must have brackets.. Hope it helps :)))

Related

How to prevent onNavigationItemSelected fires when the activity is launched?

I want to use spinner in the action bar in my activity below is the onCreateOptionsMenu: I use this tutorial to achieve this approach. My problem is when the activity is lunch, the onNavigationItemSelected method fires and the code on the switch/case run and the activity that I set for the position 0 opens. what should I do to prevent to run the switch/case when the activity is lunch?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
SpinnerAdapter mSpinnerAdapter;
if(Build.VERSION.SDK_INT <= 10)
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item);
}
else
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item);
}
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener()
{
#Override
public boolean onNavigationItemSelected(int position, long itemId)
{
switch (position)
{
case 0:
Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class);
startActivity(searchIntent);
break;
case 2:
Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class);
startActivity(dealsIntent);
break;
case 3:
Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class);
startActivity(eventsIntent);
break;
}
return true;
}
};
actionBar.setListNavigationCallbacks(mSpinnerAdapter,
return super.onCreateOptionsMenu(menu);
}
You don't need the following code inside onCreateOptionsMenu(Menu):
....
....
Remove it and place it in your activity's onCreate(Bundle) method.
Edit:
Declare a global boolean variable:
boolean initializing = true;
Place the following code inside onCreate(Bundle):
SpinnerAdapter mSpinnerAdapter;
if(Build.VERSION.SDK_INT <= 10)
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item);
}
else
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item);
}
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener()
{
#Override
public boolean onNavigationItemSelected(int position, long itemId)
{
if (initializing) {
initializing = false;
} else {
switch (position)
{
case 0:
Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class);
startActivity(searchIntent);
break;
case 2:
Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class);
startActivity(dealsIntent);
break;
case 3:
Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class);
startActivity(eventsIntent);
break;
}
}
return true;
}
};
//actionBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
getActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);

How to start different activity using switch case in the menu item?

public void onPopup(View view)
{
final PopupMenu menu=new PopupMenu(this,view);
menu.getMenuInflater().inflate(R.menu.menu1,menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Toast toast=Toast.makeText(MainActivity.this,
item.getTitle()+"Selected",Toast.LENGTH_SHORT);
//Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class);
//startActivity(intent2);
//startActivity(new Intent(MainActivity.this,YourSpotActivity.class));
toast.show();
return true;
}
});
menu.show();
}
When i click any one of the list item then it will start another activity.
How can i do that by modify an above code. explain me please.
I have use four car model in the menu. when i choose any one of that car then it will go to particular activity.
You need to use switch as below
switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(getApplicationContext(), "StartActiviy 1", Toast.LENGTH_SHORT).show();
// start activity 1
return true;
case R.id.menuitem2:
Toast.makeText(getApplicationContext(), "StartActiviy 2", Toast.LENGTH_SHORT).show();
// start activity 2
return true;
default:
//default intent
return true;
}
http://developer.android.com/reference/android/widget/PopupMenu.html
You can use switch statement as below inside onMenuItemClick:
switch (item.getItemId()) {
case R.id.menuitem1:
//calling intent ( activity1 )
case R.id.menuitem2:
//calling intent ( activity 2)
default:
//default intent
}

How to change item in a spinner in ActionBar programmatically

I have a spinner in my ActionBarSherlock with 5 elements in it. By clicking one of the two buttons at the bottom of the screen (see picture) I want to change the item id of the spinner.
This is the relevant code:
#Override
public void onCreate(Bundle savedInstanceState) {
/* ... some code ...*/
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.arr_contents, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setTitle(title);
/*... some other code ...*/
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
// Do stuff
break;
case 1:
// Do stuff
break;
case 2:
// Do stuff
break;
case 3:
// Do stuff
break;
case 4:
// Do stuff
break;
default:
// Do stuff
break;
}
return true;
}
I've tried calling onNavigationItemSelected(3, R.array.arr_contents); for example to move to the third element of the spinner but it doesn't work.
How can I do it?
Not sure but probably:
getSupportActionBar().setSelectedNavigationItem(itemPosition);
Docs

Get listview clicks from sidenavigation

I have implemented the Sliding Menu in my App. Source: https://github.com/johnkil/SideNavigation
It works like it should but when I click on any item in my menu, the click wont work some reason. I added onClick listener and all that.
Code snippet:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
sideNavigationView.toggleMenu();
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
#Override
public void onSideNavigationItemClick(int itemId) {
switch (itemId) {
case R.id.side_navigation_menu_item1:
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
case R.id.side_navigation_menu_item2:
Intent intent = new Intent(this, DiffAdapter.class);
this.startActivity(intent);
break;
case R.id.side_navigation_menu_item3:
invokeActivity(getString(R.string.title3), R.drawable.ic_action_storage);
break;
case R.id.side_navigation_menu_item4:
invokeActivity(getString(R.string.title4), R.drawable.ic_action_settings);
break;
case R.id.side_navigation_menu_item5:
invokeActivity(getString(R.string.title5), R.drawable.ic_launcher);
break;
default:
return;
}
finish();
}
Any help would be nice. It just wont do anything when I click on a item.
Thanks
I forgot to implement some strings, which caused bugs. Got this solved by adding:
icon = (ImageView) findViewById(android.R.id.icon);
sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
sideNavigationView.setMenuItems(R.menu.ribbon_menu);
sideNavigationView.setMenuClickCallback(this);
if (getIntent().hasExtra(EXTRA_TITLE)) {
String title = getIntent().getStringExtra(EXTRA_TITLE);
int resId = getIntent().getIntExtra(EXTRA_RESOURCE_ID, 0);
setTitle(title);
icon.setImageResource(resId);
}
to my code: onCreate ()

onOptionsItemSelected issue

i have created an options menu for my database class. Upon launching the options menu, I would like to the desired activity at the click of the specified button.
But the issue is that if i click on any option , i get directed to the MainMenu.class . Any ideas why this is happening ?
code:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
new MenuInflater(this).inflate(R.menu.optionmenu, menu);
return(super.onCreateOptionsMenu(menu));
}
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
}
case R.id.takesurvey:
{
Toast toast=Toast.makeText(this, "check", 2000);
toast.show();
Intent r1=new Intent(Database.this,SurveyActivity.class);
startActivity(r1);
}
case R.id.viewstats:
{ Intent r2=new Intent(Database.this,Stats.class);
startActivity(r2);
}
case R.id.changesort:
{ Intent r3=new Intent(Database.this,MainMenu.class);
startActivity(r3);
}
case R.id.menuexit:
{ Intent r4=new Intent(Database.this,MainMenu.class);
startActivity(r4);
}
}
return true;
}
It looks like you are missing a break statement in every case.
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.takesurvey:
Toast.makeText(this, "check", 2000).show();
startActivity(new Intent(Database.this,SurveyActivity.class));
break;
case R.id.viewstats:
startActivity(new Intent(Database.this,Stats.class));
break;
case R.id.changesort:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.menuexit:
startActivity(new Intent(Database.this,MainMenu.class));
break;
return true;
}
For each of your conditions in the Switch statement in onOptionsItemSelected() you must return true. If you handle the case then you must return true, if you don't then you should call the super class implementation of it.
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
return true;
}
Go through this for more details
http://developer.android.com/guide/topics/ui/menus.html#options-menu

Categories

Resources