Bottom navigation bar doesn't work properly on android studio (java) - 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)

Related

Auto open drawable after start app android

I want Auto open drawable after start app android
used library https://github.com/mikepenz/MaterialDrawer
this code from Main Activity
If it possible
if possible
And the second question is, can I open the menu on all activities after the swipe to the right? Only the back button now works
drawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(mToolbar)
.withSelectedItem(1)
.withAccountHeader(headerResult)
.addDrawerItems(
new PrimaryDrawerItem().withIdentifier(1).withName(R.string.Home).withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withIdentifier(2).withName(R.string.News).withIcon(FontAwesome.Icon.faw_newspaper),
new PrimaryDrawerItem().withIdentifier(3).withName("About").withIcon(FontAwesome.Icon.faw_question_circle),
new PrimaryDrawerItem().withIdentifier(4).withName("Open Source").withIcon(FontAwesome.Icon.faw_github_square),
new PrimaryDrawerItem().withIdentifier(5).withName("Rate on Google Play").withIcon(FontAwesome.Icon.faw_thumbs_up)
)
.withTranslucentStatusBar(false)
.build();
drawer.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
switch (position) {
case 1:
drawer.closeDrawer();
return true;
case 2:
drawer.closeDrawer();
drawer.setSelection(1);
startActivity(new Intent(context, NewsListActivity.class));
return true;
case 3:
drawer.closeDrawer();
drawer.setSelection(1);
startActivity(new Intent(context, AboutTheDevActivity.class));
return true;
case 4:
drawer.closeDrawer();
drawer.setSelection(1);
libsBuilder.start(context);
default:
return true;
}
}
});
I suggest to refactor your application to single activity structure. This will help to solve both of your problems.
You may just open the drawer in activity onCreate method. It is called when activity created, with single activity - when app started.
Save that drawer was opened to instance state if you need more accuracy here and don't want the drawer to be opened on activity recreation events (states when system decides to destroy your activity to release memory)
use fragments for showing app screens
open drawer in this single activity and manage toolbar from activity so it could be opened on every screen
Single activity is the way google recommends to structure apps.

Changing options menu icon in bottomNavigationView depending on an open Fragment

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

Prevent ActionBar back button to recreate the MainActivity

How can I prevent the ActionBar back button (we gonna say ABBB) of my SecondActivity to recreate the MainActivitywhen clicked ?
I have a ListView in the MainActivity and it can be edited using the SecondActivity. The problem is that when the user presses the ABBB, the ListView is reset to default...
However, when the user clicks my OK button or presses physical back button, there is not any problem because I'm using finish();
#Override
public void onBackPressed() {
finish();
}
If I use this code... :
switch (item.getItemId()) {
case (android.R.id.home):
finish();
}
...there is the same problem because, I guess, this method is called after "Android's code".
How can I totally override Android ABBB's clicked code to set it to just call finish(); ?
The ideal scenario is, when are you in the SecondActivity (I take it that, this means that you are in Edit mode), and you press the device back button or ABBB, you show a subtle alert to the user saying "do they really want to dismiss the editing", or go ahead and apply the edit done as in the Contacts application.
So that being said, if all you require is to finish() the activity on press of ABBB, the code that you shown above should work fine (though you need to put return true; after finish()). It works for me. Try the below one in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed(); // or finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I put onBackPressed(); because your ABBB with now imitate your device back button.
Or, you can set your parent activity's launch mode as -
android:launchMode="singleTop"
that will do all without changing anything.
Reference

Back button in header "refresh" the previus activity

I'm new to Android and I trying to do a simple app.
In the "home", I write my name and press the "send" button.
In the second page, the app show "Hello " + inserted name.
The problem is:
When I touch the back virtual button in nav bar, I come back to the home and in the input field there is always the name; but when I go back with the back button in header bar, the input field is empty.
I've tried with:
in onOptionsItemSelected:
switch (item.getItemId()){
case android.R.id.home:
Log.d("case","go back");
NavUtils.navigateUpFromSameTask(this); //or with finish();
return true;
}
it prints "case: go back" in console, but doesnt' work correcly.
My custom theme have as parent Theme.AppCompat.Light.DarkActionBar and if i put getSupportActionBar().setDisplayHomeAsUpEnabled(true); in onCreate, Android Studio tells me "Method invocation 'getSupportActionBar().setDisplayHomeAsUpEnabled(true)' may produce 'java.lang.NullPointerException'".
I solved adding this in the second Activity:
#Override
public Intent getSupportParentActivityIntent() {
return null;
}
and edit the switch/case function like this:
switch (item.getItemId()){
case android.R.id.home:
this.finish();
return false;
default:
return super.onOptionsItemSelected(item);
}

How to call new activity when click on Drop down navigation in actionbar in android

I am following this link to implement actionbar.
i have modified this code like this
/** Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition] , Toast.LENGTH_SHORT).show();
if(actions[itemPosition] == "Bookmark")
startActivity(new Intent(this,Bookmark.class))
else if(actions[itemPosition] == "Subscribe")
startActivity(new Intent(this,Subscribe.class))
else
startActivity(new Intent(this,Share.class))
return false;
}
};
When i run this code it opens Bookmark activity . but it call over main layout like when it opens bookmark and i click back then it opens main activity. i want when i click on list for e.g bookmark then only the activity should change not the title with list navigation. or is there any example that i should follow to call new activity when list navigation is called. i have also tried ActionBarSherlock it also change the textview on clicking list navigation not calling new activity
Try This instead of above
if(actions[itemPosition].compareTo("Bookmark")==0){
startActivity(new Intent(this,Bookmark.class));
}
else if(actions[itemPosition].compareTo("Subscribe")==0){
startActivity(new Intent(this,Subscribe.class));
}
else{
startActivity(new Intent(this,Share.class));
return false;
}
You cannot open new activity on current activity. That's not how it works. If you want to change the views within your activity when the item is clicked you can, and should look into fragments to do so.

Categories