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);
}
}
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 am using actionBar.setDisplayHomeAsUpEnabled(true); for back navigation functionality but it is not working as properly as i need it and when i am using back button of my phone it works properly.
Is there any way to code on a button such that it work same as back navigation button of phone?
Along with
actionBar.setDisplayHomeAsUpEnabled(true);
or
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
you can try using this in Activity, too:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So your menu item "Home"'s click will be handled using that.
I know this has been answered, but i always do this in my method when having the same back-button-like capability in another ActionBarActivity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
and ofcourse:
actionBar.setDisplayHomeAsUpEnabled(true);
You may/may not used this, its up to you :)
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 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.
I created a simple activity (with a menu) and tried adding menu items but they don't show up when trying to actually give them some function in the onOptionsItemSelected() method. I'm not sure why it's not working, as I did the exact same thing in the menu for the main activity and it worked just fine. When typing in android.R.id.add_screen_submit_button for example, it is not recognized as existing. And if I forcefully just type it in and leave it the message "add_screen_submit_button cannot be resolved or is not a field" comes up. The menu is also in the correct folder (I actually just left it as is when creating the Activity). Thanks in advance.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/add_screen_submit_button"
android:orderInCategory="4"
android:showAsAction="always"
android:title="#string/add_screen_ok"
android:icon="#drawable/accept_icon" />
<item
android:id="#+id/add_screen_cancel_button"
android:orderInCategory="5"
android:showAsAction="always"
android:title="#string/add_screen_cancel"
android:icon="#drawable/cancel_icon" />
</menu>
Here's the code
public class AddActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.
//setupActionBar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case android.R.id.add_screen_submit_button:
Toast.makeText(this, "Map Selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Change
android.R.id.home:
to
R.id.home:
and the same with the other one.
android.R is for sdk resources and R.id.some_id is for ids you create