Changing options menu icon in bottomNavigationView depending on an open Fragment - java

Option Menu:
<item
android:id="#+id/home"
android:icon="#drawable/ic_home"
android:title="Home"/>
<item
android:id="#+id/companies"
android:icon="#drawable/ic_companies"
android:title="Companies"/>
<item
android:id="#+id/contact"
android:icon="#drawable/ic_contacts"
android:title="Contact"/>
I want to change this item's icon programmatically depending on the open Fragment and, obviously, have different actions when the user hits this button. I tried several things to do that, but nothing worked.
The last thing I tried was this code in my Fragment onCreateView method:
Menu menu = bottomNavigationView.getMenu();
menu.findItem(R.id.ic_home).setIcon(R.drawable.ic_home_fill);
but its not work for me.
what i tried in selectFragment(MenuItem item)
switch (item.getItemId()) {
case R.id.home:
item.setIcon(R.id.ic_home_fill);
break;
}
I want to change the icon of the bottom navigation of selected position. if user clicked one item then the icon change to another one and when i select another one then 1st icon can set as a default.
refer this link:Android: Bottom Navigation View - change icon of selected item
but it's not work for me
plz give me a other solution.
Thank you

Try this, It worked for me
BottomNavigationView mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
invalidateOptionsMenu();
switch (item.getItemId()) {
case R.id.click_to_use:
addHomeFragment(MainActivity.this);
mTitle.setText("Home");
item.setIcon(R.drawable.device);
break;
case R.id.history:
addNotifyFragment(MainActivity.this);
mTitle.setText("History");
item.setIcon(R.drawable.ios_icon);
break;
case R.id.settings:
addSettingFragment(MainActivity.this);
mTitle.setText("Settings");
break;
}
return true;
}
});

after any modification in the menu, you would have to make an "Invalidateoptionsmenu ()" in the action. I invite you to want to test this feature . see here

Related

Bottom navigation bar doesn't work properly on android studio (java)

My bottom navigation bar looks visually okay it contains 4 icons each moving you to a different activity and the default one is the "home" activity.
I made my navigation bar with a menu btw.
Whenever you click on an icon in order to move to another activity it moves you into that activity but the bottom navigation bar (who is supposed to highlight the screen you are in) doesn't update unless you press on the same icon again, whenever you click to move another activity it highlights the "home" activity first, only if you click the same icon does it update.
This is my code.
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.post:
Toast.makeText(CreateNewPost.this, "Post", Toast.LENGTH_SHORT).show();
break;
case R.id.myProfile:
Toast.makeText(CreateNewPost.this, "My Profile", Toast.LENGTH_SHORT).show();
Intent profileIntent = new Intent(CreateNewPost.this, UserProfile.class);
startActivity(profileIntent);
break;
case R.id.explore:
Toast.makeText(CreateNewPost.this, "Explore", Toast.LENGTH_SHORT).show();
Intent newPostIntent = new Intent(CreateNewPost.this, Explore.class);
startActivity(newPostIntent);
break;
case R.id.home:
Toast.makeText(CreateNewPost.this, "Home", Toast.LENGTH_SHORT).show();
Intent homeIntent = new Intent(CreateNewPost.this, HomeScreen.class);
startActivity(homeIntent);
break;
default:
}
return true;
}
And this is how it looks
I want the navigation bar to highlight the correct activity (the one you are on)

Action bar option action

Before Clicking:
After Clicking:
I am having an issue with my code where clicking on an actionbar action will bring up the title of the action. If you then click on the title that is how you get to the onclick listener. Very strange... I know it is hard to tell without code but I am away and will post it soon. Just wanted to ask around to see if anyone has experienced similar.
ActionBar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:title="#string/hello1"
app:showAsAction="never"/>
</menu>
onClickListener:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_settings:
onCreateDialog();
return true;
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onCreateDialog() {
String[] string= new String[]{"Add to Calendar", "Share"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options")
.setItems(string, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:...................}
This is the expected behavior for the way you have the menu setup currently.
<item
android:id="#+id/action_settings"
android:title="#string/hello1"
app:showAsAction="never"/> <!-- Don't show this action until the overflow menu is shown -->
With the showAsAction set to never, the action is not shown until the overflow menu is shown. The available options for showAsAction are:
["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
In your case, because you only have one menu item, you can use always.
Refer to the menu resource documentation for more info.
You should be able to preview this in Android Studio (see screenshot below)
I fixed it by extending the activity to appcompatactivity instead of activity. Weird bug. Then it didn't do it.

Android Tooltip menu item style

I can't find anything on the google and stackoverflow so I had to ask.
I have menu on Toolbar:
<item
android:icon="#drawable/help_button_selector"
android:title="#string/Menu_Toolbar_Help"
android:id="#+id/MENU_HELP"
app:showAsAction="always"/>
When user long press icon, system shows tooltip (popup/context menu) with title. However on most devices it has black border (like in Toast). Is there any way to style this border/background? I have already created style for toolbar theme and popup, but I can't find a proper item name.
1.- You can follow this post to create your ToolTip link
2.-You can do this to show a Toast as a ToolTip, then you can customize it as you want.
view.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "I'm a ToolTip", Toast.LENGTH_SHORT).show();
return true;
}
}
Hope it helps :)
Use below link to customize your Action Bar:
http://romannurik.github.io/AndroidAssetStudio/
open url -> GoTo Android Action Bar Style Generator -> Select the options -> click DOWNLOAD.ZIP -> Copy all the componets into their correspondence folders -> In your styles.xml file remove all the code and copy all the code of styles_**.xml file and delete this file.
Menu tooltip
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_skip) {
View view = findViewById(R.id.menu_skip);
Tooltip.make(this, new Tooltip.Builder(101)
.anchor(view, Tooltip.Gravity.BOTTOM)
.closePolicy(mClosePolicy, 5000)
.text("Tooltip on a TabLayout child...Tooltip on a TabLayout child...")
.fadeDuration(200)
.fitToScreen(true)
.activateDelay(2000)
.withCallback(this)
.floatingAnimation(Tooltip.AnimationBuilder.DEFAULT)
.showDelay(400)
.build()
).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Visit more

Start an Activity from Item (ActionBar)?

I'm using ActionBarCompat to make an actionbar for devices under API 11.
It works great and was easy to setup, but I'm stuck.
I have some items on the Actionbar, and it looks great.
Some Items are behind the three dots (ifRoom) and some you always can see.
How do I make so when you click on one of these items so it starts an new Activity?
I tried with switch/case and other methods, but didnt work to send from one Activity to another through the Items. I know how to send from button, imagebutton to another Activity, but not from Items.
My main.xml looks like this:
<item
android:id="#+id/add"
android:title="Lägg till"
android:icon="#drawable/new"
android:orderInCategory="1"
budsnabben:showAsAction="always"/>
And the code in MainActivity looks like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.:
Intent intent = new Intent(this, MapActivity.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
The problem is on case R.id.... After Id, I dont get my class map or main, its not there.
Thank you.
SOLUTION:
Just want to thank you Gerard.
I created new Strings in strings.xml.
After that, I did change the title in my main.xml to this:
android:title="#string/add"
I did hard-coded that line like before, therefore it didn't work I think:
android:title="#+id/add"
Thank you once more.
Use public boolean onOptionsItemSelected(MenuItem item) { ... } in your activity using the actionbar and create a switch-case matching item.getItemId() with the IDs from the menu layout. After that creating the appropriate intent like you would on a regular button.

"Back" button on Action bar - Android . How to go "back"?

Action Bar
I'm talking about the (Number 1, in the pic), button with a little arrow and the app icon and the top left side of the screen. It is automatically defined when we select the "Black activity" template.
My app has a pretty huge hierarchy graph, got about 25 activities now.
I'm basically just showing a few tutorials and one can navigate to it according to the categories.
Now that "Back" (?) button thingy on action bar is on every screen I have, and I do want to keep it. The code shows no error, but when I actually press that button, the app stops working.
What I want, is to just replicate the actual back button function with that (Number 1) button, that I showed in the image.
When I press it, the top most screen should close, and the last one should open. Just close the screen.
What I tried :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
this is the the function that inflates that buggy button along with the action bar. I tried to replace the entire code, and call "Finish" function, but that failed miserably.
I was unable to find a function specifically made for that top left most button...
I want the top most screen on the stack(the one in the foreground) to close, when this button is touched.
How to do this ?
I think the easiest way out is follows:
I am assuming that from activity A you are starting the activity B. Now from activity B you want to go back to activity A on pressing the top left back button on action bar. simply call this.finish() or ActivityName.this.finish() from there:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
This should finish your current activity. However if you have lot of activities, then you might have to do this in all the activities. To save yourself from that effort, you can make a class lets call it AbstractActivity; that extends Activity. Then you can extend all your other activity classes to extend that class(AbstractActivity). Inside AbstractActivity you can put the above code. So now that piece of code would be valid for all your activities and that feature would be implemented for all of them. Basically this sort of thing (Inheritance)can be used any time, when there are some common features which would be applicable to your many classes.
If you are receiving any errors, please do post your LogCat if you require further help.
Hope this helps you.
just giving basic code given by #shobhit puri...
for invoking the action bar back button..add the following code in the onCreate() method along with the onOptionsItemSelected....
protected void onCreate(Bundle savedInstanceState)
{
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}

Categories