How to get SelectedItemId of BottomNavigationView in android
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
add a comment |
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
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
add a comment |
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
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
android android-studio android-fragments bottomnavigationview android-bottom-nav-view
edited Nov 21 '18 at 7:21
Aniruddh Parihar
2,16911027
2,16911027
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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);
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
|
show 5 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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
|
show 5 more comments
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);
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
|
show 5 more comments
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);
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);
edited Nov 21 '18 at 7:39
answered Nov 21 '18 at 7:06
aminographyaminography
5,98521331
5,98521331
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
|
show 5 more comments
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
|
show 5 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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