Overriding onOptionsItemSelected from SherlockFragmentActivity - android

Yesterday, I found a great library that allowed me to have a "facebook menu" with a button on the top left of an action bar which, when pressed, would slide in a menu of items from the left.
The problem is that I wish to make use of the ActionBarSherlock library as well to make sure that my application is backwards compatible with the action bar. When using the library I, among other things, need to override onOptionsItemSelected as such :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home) {
rbmView.toggleMenu();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
Now I went into the library and saw that the developer had made onOptionsItemSelected final. I removed it and tried overriding it again only to find that the product was that whenever I press the button nothing happens. Nothing at all.
Any idea on how I would go about using the darvds_ribbonmenu library along with actionbarsherlock?

Turns out that when using ABS you will need to specify the namespace of MenuItem to make sure that you're overriding the correct method. My solution was as follows :
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
{
int id = item.getItemId();
Log.d("item ID : ", "onOptionsItemSelected Item ID" + id);
if (id == android.R.id.home) {
rbmView.toggleMenu();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}

Change import android.view.MenuItem; to import com.actionbarsherlock.view.MenuItem;. Otherwise, you're just using an entirely different MenuItem than the one you're importing.

Related

getSupportActionBar().setDisplayHomeAsUpEnabled(booleanValue)

I was trying to implement navigation drawer into my app.
In it I came across this line of code:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
but I am not able to understand the meaning of this code.
I tried reading the official documentation but it was not clear?
These two lines used to enable and show the home button.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
You can set your own icon by adding this line: `
getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_navigation);
To perform an action on this button you will need to override onOptionItemSelected and put below code inside but make sure you write the correct id many people confuse with it.
Home button id: android.R.id.home
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home){
// write your own code..
return true;
}
}
return super.onOptionsItemSelected(item);
}

Android ActionBar wrong home button id

I'm trying to implement a Navigation Drawer, I can open it by sliding my finger, however, when I click on the home button on my action bar nothing happens. I tried logging the item id in the onOptionsItemSelected() method and I confirm the id I retrieve is different from R.id.home. How do I retrieve the home button's id?
The id for home is not R.id.home, it is android.R.id.home because it is a home button created by android itself. using android.R.id.home in onOptionsItemSelected() will hopefully works for you.
Use
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
//do something
return true;
}else {
return super.onOptionsItemSelected(item);
}
}

How to use a navigation drawer in Android Studio?

First time here.
I need to use Navigation drawer for my app. But im working with Activities.
The question is, how can i select an item and redirect to an activity?
Thank you
If you used the Navigation Drawer Template from Android Studio, you will have a main activity with a method onNavigationItemSelected. Implement the method to check the id of the menu item passed to it and decide which Activity to start based on the menu id. For example:
public boolean onNavigationItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.nav_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}else if (id == R.id.nav_faq) {
startActivity(new Intent(this, FAQActivity.class));
return true;
}
}

how to provide proper back navigation with fragment on clicking the home button

I would like to provide back navigation while clicking on the Home button of Actionbar.
In the Host Fragment
((MainActivity)this.getActivity()).fragment_Actualités=new Fragment_Actualités();
fragmentTransaction.replace(R.id.content_frame,((MainActivity)this.getActivity()).fragment_Actualités);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
((MainActivity)this.getActivity()).fragmentManager.executePendingTransactions();
((MainActivity)this.getActivity()).supportInvalidateOptionsMenu();
In the Host Activity
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
int id=0;
if(id==item.getItemId()){
if(id== R.id.home)
{
this.onBackPressed();
return true;}
}
The super.OnBackPressed works fine when I click on the back button it goes back as expected
How can I make it worked ?
Try using
fragmentManager.popBackStackImmediate()
Try changing:
this.onBackPressed();
to:
onBackPressed();
and extends FragmentActivity if you don't do it already
Reading your code again I think found the issue, you are assigning 0 to id so when you compere with item.getItemId() always return false. Try with this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
this.onBackPressed();
return true;
}
}
I finally resolved it,
the matter was that, I had 2 MenuItems, the Settings and the Home One's .
So if only write R.id.Home it won't work but when changed to android.R.id.Home it works !!!
with the same code

Action Bar's onClick listener for the Home button

How can I implement a custom onClickListener for the Home button of the Action Bar?
I already did a getSupportActionBar().setDisplayHomeAsUpEnabled(true); and now I want to redirect the user to a certain activity in case the Home button is clicked.
I tried with:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent i = new Intent();
i.setClass(BestemmingActivity.this, StartActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
return true;
}
});
default:
return super.onOptionsItemSelected(item);
}
}
but it never enters in the onMenuItemClick.
Basically, it's done just like in this link but still it doesn't enter in the listener.
if anyone else need the solution
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed(); return true;
}
return super.onOptionsItemSelected(item);
}
I use the actionBarSherlock,
after we set supportActionBar.setHomeButtonEnabled(true);
we can override the onMenuItemSelected method:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
toggle();
// Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
break;
}
return true;
}
if we use the system given action bar following code works fine
getActionBar().setHomeButtonEnabled(true);
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
//do your action here.
break;
}
return true;
}
Fixed: no need to use a setOnMenuItemClickListener.
Just pressing the button, it creates and launches the activity through the intent.
Thanks a lot everybody for your help!
answers in half part of what is happening. if onOptionsItemSelected not control homeAsUp button when parent activity sets in manifest.xml system goes to parent activity. use like this in activity tag:
<activity ... >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" />
</activity>
You need to explicitly enable the home action if running on ICS. From the docs:
Note: If you're using the icon to navigate to the home activity, beware that
beginning with Android 4.0 (API level 14), you must
explicitly enable the icon as an action item by calling
setHomeButtonEnabled(true) (in previous versions, the icon was enabled
as an action item by default).
Best way to customize Action bar onClickListener is onSupportNavigateUp()
This code will be helpful link for helping code
you should to delete your the Override onOptionsItemSelected and replate your onCreateOptionsMenu with this code
#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_action_bar_finish_order_stop, menu);
menu.getItem(0).setOnMenuItemClickListener(new FinishOrderStopListener(this, getApplication(), selectedChild));
return true;
}

Categories

Resources