ppl am trying to do menu option...and i got it,but after clicking the option in the slide menu...the control should go the respective page..i tried ..but its not working..please help me
switch(position){
case '0': {
startActivity(new Intent(home.this, home.class));
finish();
}
case '1':
{
startActivity(new Intent(home.this, info_values.class));
finish();
}
}
You should try like this
switch(position){
case 0:
startActivity(new Intent(home.this, home.class));
finish();
break;
case 1:
startActivity(new Intent(home.this, info_values.class));
finish();
break;
default:
break;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 'R.id.a':
startActivity(new Intent(home.this, home.class));
case 'R.id.b':
startActivity(new Intent(home.this, info_values.class));
}
return true;
}
Related
I am creating a app with navigation drawer which is used to navigate through activities.
Here's my code or drawer
private void initInstances() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerToggle = new ActionBarDrawerToggle(busr.this, drawerLayout, R.string.hello_world, R.string.hello_world);
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.closeDrawers();
navigation = (NavigationView) findViewById(R.id.navigation_view);
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.navigation_item_1:
startActivity(new Intent(busr.this, MainActivity.class));
break;
case R.id.navigation_item_2:
startActivity(new Intent(busr.this, aff.class));
break;
case R.id.navigation_item_3:
startActivity(new Intent(busr.this, webs.class));
break;
case R.id.navigation_item_4:
startActivity(new Intent(busr.this, admnActivity.class));
break;
case R.id.navigation_item_5:
startActivity(new Intent(busr.this, busr.class));
break;
case R.id.navigation_item_6:
startActivity(new Intent(busr.this, trng.class));
break;
case R.id.navigation_item_7:
startActivity(new Intent(busr.this, prospct.class));
break;
case R.id.navigation_item_8:
startActivity(new Intent(busr.this, erp.class));
break;
case R.id.navigation_item_9:
startActivity(new Intent(busr.this, result.class));
break;
}
return false;
}
});
try this: For closing the drawerlayout
case R.id.navigation_item_1:
if (drawerLayout.isDrawerOpen(Gravity.START))
drawerLayout.closeDrawer(Gravity.START);
startActivity(new Intent(busr.this, MainActivity.class));
break;
or
case R.id.navigation_item_1:
drawerLayout.closeDrawers();
startActivity(new Intent(busr.this, MainActivity.class));
break;
In my application i don't want to recreate activity on navigation drawer menu click when i am on same activity.
user on map activity after navigation drawer item click map reloading. i don't want to reload map or refresh activity.
private void displayView(int position)
{
switch (position)
{
case 0:
break;
case 1:
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
break;
case 2:
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
break;
case 3:
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
break;
default:
break;
}
You can add the following to your function
private int current=-1;
private void displayView(int position)
{
switch (position)
{
case 0:
current =0;
break;
case 1:
if(current!=1){
current =1;
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
}
break;
case 2:
if(current!=2){
current=2;
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
}
break;
case 3:
if(current!=3){
current=3
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
}
break;
default:
break;
}
I am getting and displaying images after doing Parse Queries and caching images. My problem is that when i click on the clear cache it doesn't delete the images and my pageviewer with i use to display them. It displays duplicates.I'm using Image Fetcher https://developer.android.com/samples/DisplayingBitmaps/src/com.example.android.displayingbitmaps/util/ImageFetcher.html from Caching Bitmaps from http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html saving files to the phone memory
private ImageFetcher mImageFetcher;
...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.logout:
ParseUser.logOut();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish(); // logout code
return true;
case R.id.prefs:
Intent i = new Intent(this, SharedPrefsActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
// intent = new Intent(this, SharedPrefsActivity.class);
// startActivity(intent);
// finish(); //logout code
return true;
case R.id.clear_cache:
mImageFetcher.clearCache();
Toast.makeText(this, R.string.clear_cache_complete_toast,
Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
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 ()
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