This one activity but i try to close the bottom navigation when the user selected one of the side menu fragment. Example when i click my account the bottom not hide.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_account:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AccountFragment()).commit();
break;
case R.id.nav_support:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SupportFragment()).commit();
break;
case R.id.nav_aboutus:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AboutusFragment()).commit();
break;
case R.id.nav_logout:
firebaseAuth.signOut();
startActivity(new Intent(getApplicationContext(),Signin.class));
break;
}
mDrawerlayout.closeDrawer(GravityCompat.START);
return true;
}
I hope that will simple code or maybe add another activity?
This my layout
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".sidemenu">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
android:background="?android:attr/windowBackground"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
app:headerLayout="#layout/header"
android:id="#+id/nav_View"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
app:itemTextColor="#color/Black"
app:itemIconTint="#color/gray"
app:menu="#menu/drawermenu"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
Maybe put some code in navigation or bottom navigation
As your container and BottomNavigation in same activity layout and you are using your container for fragment.
Create one interface
public interface IBottomBarListener{
public void showBottomBar();
public void hideBottomBar()
}
Implement this interface in your activity and override these two methods.
public void showBottomBar(){
// make your bottom bar visibility visible
}
public void hideBottomBar(){
// make your bottom bar visibility gone
}
Now in your fragment declare global variable
public IBottomBarListener listener;
Now in onAttach() method , init your listener
listener = (IBottomBarListener)context
now in onCreateView(), call
listener.hideBottomBar() // this will hide your bottombar
also in onDetach() method, if you want to again make visible your bottombar,
onDetach(){
listener.showBottomBar() // this will show your bottombar
}
This is basically showing/hiding your bottombar from your fragment. So do same in all fragment from where you want to perform this action.
Related
I am trying to make a bottom navigation bar to navigation bar between activities but I find using fragments very confusing so I tried using but its not working tried to look for something that I can use but I did not find anything online so as people who have experience can I use bottom navigation bar without fragments.
My code:
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
Intent intent1 = new Intent(RedirectedsearchActivity.this,
SearchActivity.class);
startActivity(intent1);
return true;
It didn’t work.
You can try the navigation component.
With it, it is much easier to work with fragments.
There are a couple of links to documentation and articles.
Documentation https://developer.android.com/guide/navigation
And tutorial https://www.androidauthority.com/android-navigation-architecture-component-908660/
Try this code and make activity_main layout.. without fragment it not working that time you need each and every activity define bottom navigation.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<include
android:id="#+id/amToolbar"
layout="#layout/app_toolbar"
/>
<FrameLayout
android:id="#+id/activity_main_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintTop_toBottomOf="#+id/amToolbar"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="#drawable/bottom_navigation_colors"
app:itemTextColor="#drawable/bottom_navigation_colors"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
after that mainActivity call click event..
// Set action to perform when any menu-item is selected.
bottom_navigation.setOnNavigationItemSelectedListener { item ->
item.isChecked = true
selectFragment(item)
false
}
private fun selectFragment(item: MenuItem?) {
item?.isChecked = true
when (item?.itemId) {
R.id.action_uploadFile -> {
// here define fragment change
}
}
}
Here is required screen design:
As per the above screenshot, I need to design the layout: when i click on add button of recyclerview item row, add options need to show.
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorLightWhite">
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="visible"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:weightSum="100">
<EditText
android:id="#+id/et_add_member"
android:layout_width="wrap_content"
android:layout_weight="95"
android:layout_height="42dp"
android:padding="#dimen/padding_8"
android:hint="Person"
android:background="#drawable/edit_box"/>
<ImageView
android:id="#+id/iv_addMember"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/default_margin"
android:layout_centerVertical="true"
android:layout_weight="5"
android:src="#drawable/add_x" />
</LinearLayout>
<LinearLayout
android:id="#+id/include_add"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_below="#+id/ll_main"
android:layout_alignTop="#+id/ll_main"
android:layout_marginTop="35dp"
android:layout_marginRight="#dimen/margin_10"
android:gravity="center"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/include_members" />
</LinearLayout>
</RelativeLayout>
Here is Adapter Code of showing the popup:
holder.ivAddMember.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.include_add.setVisibility(View.VISIBLE);
}
});
include_add LinearLayout is the pop code, but I'm not able to get the exact UI with this code.
Can anyone please suggest me how to show pop on top of respective item click, how to close popup at previously click item position & Popup design as well.
You have to create xml file under menu in res folder and then, make function like below to show pop-up on desired location
public void showPopup() {
PopupMenu popup = new PopupMenu(context, <PASS ID WHERE YOU WISH TO SHOW POPUP>);
popup.getMenuInflater().inflate(R.menu.menu_option, popup.getMenu());
Object menuHelper;
Class[] argTypes;
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.member:
// call member class or function here
return true;
case R.id.guest:
// call Guest class or function here
return true;
case R.id.my_buddies:
// call My Buddies class or function here
return true;
}
return true;
}
});
popup.show();
}
Hope you understand.
I'm trying to implement a bottom nav bar that changes fragments when the nav items are clicked. However, when I click on the nav bar items, the fragments don't change. Using log.d, I noticed onNavigationItemSelected is not being called. How do I fix this?
To note, the startFeedFragment, startScheduleFragment, & startSettingsFragmentare implemented the same way and they work for the buttons in the toolbar. I also referenced this tutorial and this question for help.
MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setUpRecycler();
startFeedFragment();
BottomNavigationView bottomNavBar = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavBar.bringToFront();
bottomNavBar.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Log.d("NAV BAR", "onNavigationItemSelected hit");
switch (item.getItemId()) {
case R.id.feedMenu:
startFeedFragment();
Log.d("NAV BAR", "feed");
break;
case R.id.myScheduleMenu:
startScheduleFragment();
Log.d("NAV BAR", "schedule");
break;
case R.id.settingsMenu:
startSettingsFragment();
Log.d("NAV BAR", "settings lol");
break;
default:
Log.d("NAV BAR", "false");
return false;
}
return true;
}
/**
* Switches out the fragment for {#link FeedFragment} and sets an appropriate title. startScheduleFragmens & startSettingsFragment are implemented the same way
*/
private void startFeedFragment()
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, new FeedFragment());
transaction.commit();
getSupportActionBar().setTitle(R.string.title_fragment_feed);
}
}
Snippet from xml file
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#android:color/white"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav"
android:elevation="8dp"/>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
Solved! It was because I had set the buttons in the menu to android:enabled="false". That meant they couldn't be pressed, which is why onNavigationItemSelected wasn't being called. I set all buttons to android:enabled="true" to fix this.
Correct code for bottom_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/bottom_nav_my_schedule"
android:title="#string/menu_my_schedule"
android:icon="#drawable/ic_event_available_white_24dp"
android:enabled="true"/> <!--make sure enabled is set to true!-->
<item android:id="#+id/bottom_nav_feed"
android:title="#string/menu_feed"
android:icon="#drawable/ic_view_list_white_24dp"
android:enabled="true" /> <!--make sure enabled is set to true!-->
<item android:id="#+id/bottom_nav_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_white_24dp"
android:enabled="true"/> <!--make sure enabled is set to true!-->
</menu>
As for me the problem was when my activity main layout was LinearLayout, so I've solved it by changing it to RelativeLayout, I have absolutely no thoughts why it's working, because RelativeLayout layouts reply only for elements location, not its view or something else, but it's working.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#drawable/selector"
android:background="#color/white"
app:menu="#menu/menu_bottom"
app:itemIconTint="#drawable/menu_trainings"
app:labelVisibilityMode="unlabeled"
/>
</RelativeLayout>
I have a nav drawer which in its first fragment i have a news page where the user clicks a button to see the whole news, and at that moment it enters another fragment.
I want to know how I implement an arrow to return to the previous fragment instead of the icon that opens the Navigation Drawer.
I've never worked with this before, I have no idea.
This is my Toolbar in MainActivity
appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
//toolbar title
toolbar.setTitle(null);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Toolbar logo
logoToolbar = (ImageView) findViewById(R.id.logoToolbar);
logoToolbar.setImageResource(R.drawable.newstoolbar);
When the user click in "Read More" in the MainFragment
newsMore.get(0).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
newsFrag1 newsFragment1 = new newsFrag1();
ft.replace(R.id.fragment_container, newsFragment1);
ft.commit();
}
});
And then it goes to newsFrag1, which I want to have the back arrow
My Toolbar XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="studio.com.archeagemanager.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
android:gravity="center_vertical"
android:minHeight="56dp"
android:weightSum="1"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:id="#+id/logoToolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Fragments don't normally intercept back/up presses. That is something that's normally done at the activity level. The reasoning behind that is because you can have multiple fragments on screen at the same time... so which one handles the back press first?
To handle it in your activity, consider keeping track of your before and after state. Let's use the fragment count as an example state:
#Override
public void onBackPressed() {
// for every back press, if there is a fragment to remove, then remove it first
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
return;
}
super.onBackPressed();
}
You also need to make sure you add your original fragment transaction to the backstack:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
newsFrag1 newsFragment1 = new newsFrag1();
ft.replace(R.id.fragment_container, newsFragment1);
ft.addToBackStack();
ft.commit();
Edit: I think from your xml what you're asking for is UP navigation, not BACK navigation. If that is the case, then you still need to leverage the backstack but all the work needs to be done by the parent activity (because the activity owns the toolbar)
getSupportFragmentManager()
.addOnBackStackChangedListener(new OnBackStackChangedListener(){
#Override
public void onBackStackChanged() {
updateToolbarUp();
}
});
public void updateToolbarUp(){
boolean showUp = getSupportFragmentManager().getBackStackEntryCount() > 0;
getSupportActionBar().setDisplayHomeAsUpEnabled(showUp);
}
Then override the up press to simply pop the backstack:
#Override
public boolean onSupportNavigateUp() {
getSupportFragmentManager().popBackStack();
return true;
}
I'm using a DrawerLayout for the navigation of my app:
Taking this menu for the example:
I'd like the Home button to launch the HomeActivity and so on. But clearing the activity stack, that is: If I press People, then Photos, then Locations and then the back button, the app should close. I'm trying intent flags when launching the activities but I always get the same behaviour: People->Photos->Location->Back goes to Photos instead closing the app
How can I achieve that?
As per you said that "If I press People, then Photos, then Locations and then the back button, the app should close".
For achieve that you should user Fragment for Home,People,Location and Photos etc.
and when you press back button of Device you can clear Fragment back stack and finish activity.
I think this code may help you. When you press Back button of a device it will close the application.
In MainActivity.java,
public void SelectItem(int possition)
{
//Using Fragment here
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition)
{
//Can use Fragment for Home,People,Location and Photos etc. and when you press back button of Device you can clear Fragment back stack and finish activity.
case 0:
fragment=new FragmentNewsFeed();
break;
case 1:
fragment = new FragmentMessages();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
listView_drawer.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
drawerLayout.closeDrawer(listView_drawer);
}
In FragmentNewsFeed class,
public class FragmentNewsFeed extends Fragment
{
public FragmentNewsFeed()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_news_feeds, container,false);
return view;
}
}
Whereas you can find content frame in a layout/activity_main.xml,
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffff"/>
</android.support.v4.widget.DrawerLayout>
In R.layout.fragment_news_feeds,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_Parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#008B8B"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="0.30"
android:background="#2F4F4F"
android:orientation="vertical" >
<ListView
android:id="#+id/listView_OwnItems"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</LinearLayout>