I have 2 activities with action bar which has this code in its onCrate method:
getActionBar().setDisplayHomeAsUpEnabled(true);
And here is the onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.icon:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The first activity has a parent activity (configured in the manifest) the second one just opened from fragment (without parent fragment configured in the manifest). In the first activity, when I click the icon, the activity item.getItemId() value is the same as android.R.id.icon which then falls in the switch statement (case: Android.R.id.icon). In the second activity those values are deferent. Why this is happening? I would like to fall in the case: Android.R.id.icon in both activities.
Instead of using android.R.id.icon use android.R.id.home
Try this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
For more detail check this link
Use this type of code
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 1, "Done").setIcon(R.drawable.img_done)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(1, 2, 2, "Save").setIcon(R.drawable.img_save)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
// Write your code for the first button
break;
case 2:
// Write your code for the second button
break;
}
return true;
}
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.icon: //where 'icon' would be your item ID from menu.xml.
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you are using android.R.id.icon that might be wrong. Instead of that you may want to use R.id.icon.
There is a difference between both R.id.icon and android.R.icon files.
Also make sure that you are not using following import
import android.R;
It may lead you to unwanted results.
Related
I want to add a back button as menu in the left of the action bar in fragment. But I don't want the back arrow as my icon.
actionBar.setDisplayHomeAsUpEnabled(true);
The above code line gives the back arrow symbol. Instead i want to use some custom image. Also using that custom image should get it back to its previous activity.
I added something like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It worked for me.
Add this to your theme in styles.xml:
<item name="android:homeAsUpIndicator">#drawable/my_icon</item>
It will override the default icon. By default it uses the following id:
android.R.id.home
You can use this id to navigate back to your previous activity:
View homeView = getActionBar().getCustomView().findViewById(android.R.id.home);
homeView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Go back
}
});
or
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Return back to your activity!
return true;
}
return super.onOptionsItemSelected(item);
}
For more information check the official documentation at : https://developer.android.com/training/implementing-navigation/ancestral.html
I added a Navigation Drawer to my Activity and it seems to be working fine. The problem is that I have another icon on my Toolbar called info, which is now not responding. This info item exists in my menu.xml file:
<item
android:id="#+id/info"
android:title="Info"
android:icon="#drawable/ic_info_outline_black_24dp"
android:orderInCategory="100"
app:showAsAction="always" />
I inflate this menu in my Java code:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_items, menu);
return true;
}
And add it to my onOptionsItemsSelectedMethod, which also contains my navigation drawer:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}case R.id.info:{
Toast.makeText(getApplicationContext(),"HEY",Toast.LENGTH_LONG).show();
}
default:return super.onOptionsItemSelected(item);
}
}
The icon pops up on my ToolBar but does not respond when I click on it. Please help. Thank you.
IDs need to be kept consistent
in xml you have :
android:id="#+id/infoActionBar"
in java code, you are looking for :
case R.id.info:
Change either of those, to match with the other one.
Edit:
Replace case R.id.home: with case android.R.id.home:
Your info button id in your xml is android:id="#+id/infoActionBar" and you try to call case R.id.info: so please change one of them and try again :)
infoActionBar vs info
In onOptionsItemSelectedit must be id of the item not the title in case of item.getItemId()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}case R.id.infoActionBar:{
Toast.makeText(getApplicationContext(),"HEY",Toast.LENGTH_LONG).show();
}
default:return super.onOptionsItemSelected(item);
}
}
I have created an android project, with a couple of activities.
I have also added the back button feature into the activities, however when I click it
nothing happens.
Have I done something wrong?
I've put this code into each of the activities, and none of them work
Any opinion/input is greatly appreciated.
Code for trying to go back from my Gallery activity to the main:
//GalleryActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
// return true;
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
and.. in res/menu/
<item
android:id="#+id/back_icon"
android:icon="#drawable/ic_action_back"
android:title="#string/back_title"
android:showAsAction="always"
/>
I downloaded the proper android design icons, and have added them to the drawable folders too.
EDIT:
Main Activity
button4= (Button) findViewById(R.id.button4);//find the button
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), GalleryActivity.class);
startActivity(i);
finish();//close main activity after start info activity
}
});// links to gallery page
you have to override onOptionsItemSelected and check for the id. For instance:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back_icon:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You have to specify the action of the options, included the home/back action as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.back_icon:
onBackPressed(); //Or whatever you want to do when back_icon is pressed!
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have the following code to enable to home button to act as a back button. The problem I'm facing is from this activity if I use the real back button it simply goes back to the previous activity just as I left it. If I use the home button it's reloading the page so I lose what was previously done to it. I'm sure it's something simple I'm missing.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.census_management_search, menu);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, CensusManagementActivity.class);
NavUtils.navigateUpTo(this, intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Instead of Intent and NavUtils try using onBackPressed() method.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The Home/Up button is supposed to reload the new activity. However, if you want to emulate the back button functionality, you can call finish() to return to the previous activity:
case android.R.id.home:
finish();
return true;
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;
}