Get listview clicks from sidenavigation - android

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 ()

Related

Three dots menu OnClick Listener

i am new at android.I am using three dots menu in my android app but when i click on item on on three dots menu..my app crashes.can someone help me here is my code:
enter code here#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
break;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
break;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
break;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
return true;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
return true;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
return true;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Seems there is nothing wrong with the code.
This could be issue with declration in manifest.
Make sure the activities account, help are declared in manifest.

How to find out the String ID of an item in the Menu knowing its decimal value?

I am using android-support-v7-appcompat.
In an activity I want to show in the actionbar the back button.
I do:
public class News extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_news_screen);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
}
}
And:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println(item.getItemId()); // 16908332
System.out.println(R.id.home); // 2131034132
System.out.println(R.id.homeAsUp); // 2131034117
switch(item.getItemId())
{
case R.id.home:
onBackPressed();
break;
case R.id.homeAsUp:
onBackPressed();
break;
case 16908332:
onBackPressed(); // it's works
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
If I use numerical filter by id works, but I think that ID is generated by R and therefore can change, is therefore used R.id. . Any idea?
The home/back icon in the actionbar has the id android.R.id.home.
You can look for that id.
The values in android.R.* will never change and are linked statically.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.home:
onBackPressed();
break;
case R.id.homeAsUp:
onBackPressed();
break;
case android.R.id.home:
onBackPressed();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

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
}

Android: Sherlock action bar drop down

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 :)))

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