How to get SelectedItemId of BottomNavigationView in android












0














I have implemented BottomNavigationView in my app. Everything going easy and perfect but there is one problem i.e. on back press I want to get the currently active tab selected but now selected tab does not change when back press. Fragment changes but tab selection does not change. How can I detect the current tab and change the selected tab on back press. I tried a lot of things to do but not able to get selected tab ID.Please help.



Code:



      int i =   getSelectedItem(bottomNavigationView);
Log.e("TAG", "onCreate:tab "+i );

private int getSelectedItem(BottomNavigationView bottomNavigationView){
Menu menu = bottomNavigationView.getMenu();
for (int i=0;i<bottomNavigationView.getMenu().size();i++){
MenuItem menuItem = menu.getItem(i);
if (menuItem.isChecked()){
return menuItem.getItemId();
}
}
return 0;
}









share|improve this question
























  • take a global variable for id and update it when your select the tab from BottomNavigationView. hence when you press back button it will give you last selected tab item id.
    – Aniruddh Parihar
    Nov 21 '18 at 7:25
















0














I have implemented BottomNavigationView in my app. Everything going easy and perfect but there is one problem i.e. on back press I want to get the currently active tab selected but now selected tab does not change when back press. Fragment changes but tab selection does not change. How can I detect the current tab and change the selected tab on back press. I tried a lot of things to do but not able to get selected tab ID.Please help.



Code:



      int i =   getSelectedItem(bottomNavigationView);
Log.e("TAG", "onCreate:tab "+i );

private int getSelectedItem(BottomNavigationView bottomNavigationView){
Menu menu = bottomNavigationView.getMenu();
for (int i=0;i<bottomNavigationView.getMenu().size();i++){
MenuItem menuItem = menu.getItem(i);
if (menuItem.isChecked()){
return menuItem.getItemId();
}
}
return 0;
}









share|improve this question
























  • take a global variable for id and update it when your select the tab from BottomNavigationView. hence when you press back button it will give you last selected tab item id.
    – Aniruddh Parihar
    Nov 21 '18 at 7:25














0












0








0







I have implemented BottomNavigationView in my app. Everything going easy and perfect but there is one problem i.e. on back press I want to get the currently active tab selected but now selected tab does not change when back press. Fragment changes but tab selection does not change. How can I detect the current tab and change the selected tab on back press. I tried a lot of things to do but not able to get selected tab ID.Please help.



Code:



      int i =   getSelectedItem(bottomNavigationView);
Log.e("TAG", "onCreate:tab "+i );

private int getSelectedItem(BottomNavigationView bottomNavigationView){
Menu menu = bottomNavigationView.getMenu();
for (int i=0;i<bottomNavigationView.getMenu().size();i++){
MenuItem menuItem = menu.getItem(i);
if (menuItem.isChecked()){
return menuItem.getItemId();
}
}
return 0;
}









share|improve this question















I have implemented BottomNavigationView in my app. Everything going easy and perfect but there is one problem i.e. on back press I want to get the currently active tab selected but now selected tab does not change when back press. Fragment changes but tab selection does not change. How can I detect the current tab and change the selected tab on back press. I tried a lot of things to do but not able to get selected tab ID.Please help.



Code:



      int i =   getSelectedItem(bottomNavigationView);
Log.e("TAG", "onCreate:tab "+i );

private int getSelectedItem(BottomNavigationView bottomNavigationView){
Menu menu = bottomNavigationView.getMenu();
for (int i=0;i<bottomNavigationView.getMenu().size();i++){
MenuItem menuItem = menu.getItem(i);
if (menuItem.isChecked()){
return menuItem.getItemId();
}
}
return 0;
}






android android-studio android-fragments bottomnavigationview android-bottom-nav-view






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 7:21









Aniruddh Parihar

2,15911027




2,15911027










asked Nov 21 '18 at 7:03









mishtimishti

305




305












  • take a global variable for id and update it when your select the tab from BottomNavigationView. hence when you press back button it will give you last selected tab item id.
    – Aniruddh Parihar
    Nov 21 '18 at 7:25


















  • take a global variable for id and update it when your select the tab from BottomNavigationView. hence when you press back button it will give you last selected tab item id.
    – Aniruddh Parihar
    Nov 21 '18 at 7:25
















take a global variable for id and update it when your select the tab from BottomNavigationView. hence when you press back button it will give you last selected tab item id.
– Aniruddh Parihar
Nov 21 '18 at 7:25




take a global variable for id and update it when your select the tab from BottomNavigationView. hence when you press back button it will give you last selected tab item id.
– Aniruddh Parihar
Nov 21 '18 at 7:25












1 Answer
1






active

oldest

votes


















0














If you want to find the checked item id, try this:



int checkedItemId = bottomNavigationView.getSelectedItemId();


To change current selected item, use this:



bottomNavigationView.getMenu().findItem(R.id.target_item_nemu).setChecked(true);





share|improve this answer























  • not working. I also debugged.
    – mishti
    Nov 21 '18 at 7:09










  • You want its position or menu id?
    – aminography
    Nov 21 '18 at 7:10










  • please share more code, where do you want to use it?
    – aminography
    Nov 21 '18 at 7:11










  • whatever I can get. Menu id or position. I just need to change tab selection on backpress. but i tried in oncreate and onresume both but not working
    – mishti
    Nov 21 '18 at 7:12










  • If I understand correctly, you want to show home fragment in onBackPressed if another fragment is showing, else (if the home fragment is the currently showing), exit the app. Right?
    – aminography
    Nov 21 '18 at 7:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406818%2fhow-to-get-selecteditemid-of-bottomnavigationview-in-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you want to find the checked item id, try this:



int checkedItemId = bottomNavigationView.getSelectedItemId();


To change current selected item, use this:



bottomNavigationView.getMenu().findItem(R.id.target_item_nemu).setChecked(true);





share|improve this answer























  • not working. I also debugged.
    – mishti
    Nov 21 '18 at 7:09










  • You want its position or menu id?
    – aminography
    Nov 21 '18 at 7:10










  • please share more code, where do you want to use it?
    – aminography
    Nov 21 '18 at 7:11










  • whatever I can get. Menu id or position. I just need to change tab selection on backpress. but i tried in oncreate and onresume both but not working
    – mishti
    Nov 21 '18 at 7:12










  • If I understand correctly, you want to show home fragment in onBackPressed if another fragment is showing, else (if the home fragment is the currently showing), exit the app. Right?
    – aminography
    Nov 21 '18 at 7:16
















0














If you want to find the checked item id, try this:



int checkedItemId = bottomNavigationView.getSelectedItemId();


To change current selected item, use this:



bottomNavigationView.getMenu().findItem(R.id.target_item_nemu).setChecked(true);





share|improve this answer























  • not working. I also debugged.
    – mishti
    Nov 21 '18 at 7:09










  • You want its position or menu id?
    – aminography
    Nov 21 '18 at 7:10










  • please share more code, where do you want to use it?
    – aminography
    Nov 21 '18 at 7:11










  • whatever I can get. Menu id or position. I just need to change tab selection on backpress. but i tried in oncreate and onresume both but not working
    – mishti
    Nov 21 '18 at 7:12










  • If I understand correctly, you want to show home fragment in onBackPressed if another fragment is showing, else (if the home fragment is the currently showing), exit the app. Right?
    – aminography
    Nov 21 '18 at 7:16














0












0








0






If you want to find the checked item id, try this:



int checkedItemId = bottomNavigationView.getSelectedItemId();


To change current selected item, use this:



bottomNavigationView.getMenu().findItem(R.id.target_item_nemu).setChecked(true);





share|improve this answer














If you want to find the checked item id, try this:



int checkedItemId = bottomNavigationView.getSelectedItemId();


To change current selected item, use this:



bottomNavigationView.getMenu().findItem(R.id.target_item_nemu).setChecked(true);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 7:39

























answered Nov 21 '18 at 7:06









aminographyaminography

5,76321130




5,76321130












  • not working. I also debugged.
    – mishti
    Nov 21 '18 at 7:09










  • You want its position or menu id?
    – aminography
    Nov 21 '18 at 7:10










  • please share more code, where do you want to use it?
    – aminography
    Nov 21 '18 at 7:11










  • whatever I can get. Menu id or position. I just need to change tab selection on backpress. but i tried in oncreate and onresume both but not working
    – mishti
    Nov 21 '18 at 7:12










  • If I understand correctly, you want to show home fragment in onBackPressed if another fragment is showing, else (if the home fragment is the currently showing), exit the app. Right?
    – aminography
    Nov 21 '18 at 7:16


















  • not working. I also debugged.
    – mishti
    Nov 21 '18 at 7:09










  • You want its position or menu id?
    – aminography
    Nov 21 '18 at 7:10










  • please share more code, where do you want to use it?
    – aminography
    Nov 21 '18 at 7:11










  • whatever I can get. Menu id or position. I just need to change tab selection on backpress. but i tried in oncreate and onresume both but not working
    – mishti
    Nov 21 '18 at 7:12










  • If I understand correctly, you want to show home fragment in onBackPressed if another fragment is showing, else (if the home fragment is the currently showing), exit the app. Right?
    – aminography
    Nov 21 '18 at 7:16
















not working. I also debugged.
– mishti
Nov 21 '18 at 7:09




not working. I also debugged.
– mishti
Nov 21 '18 at 7:09












You want its position or menu id?
– aminography
Nov 21 '18 at 7:10




You want its position or menu id?
– aminography
Nov 21 '18 at 7:10












please share more code, where do you want to use it?
– aminography
Nov 21 '18 at 7:11




please share more code, where do you want to use it?
– aminography
Nov 21 '18 at 7:11












whatever I can get. Menu id or position. I just need to change tab selection on backpress. but i tried in oncreate and onresume both but not working
– mishti
Nov 21 '18 at 7:12




whatever I can get. Menu id or position. I just need to change tab selection on backpress. but i tried in oncreate and onresume both but not working
– mishti
Nov 21 '18 at 7:12












If I understand correctly, you want to show home fragment in onBackPressed if another fragment is showing, else (if the home fragment is the currently showing), exit the app. Right?
– aminography
Nov 21 '18 at 7:16




If I understand correctly, you want to show home fragment in onBackPressed if another fragment is showing, else (if the home fragment is the currently showing), exit the app. Right?
– aminography
Nov 21 '18 at 7:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406818%2fhow-to-get-selecteditemid-of-bottomnavigationview-in-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin