BOTTOM NAVIGATION VIEW HIDES - android

I have a problem with my app. I have a bottom navigation view with 3 different fragments, although currently only the main this with things. The problem comes when I try to move from one of the two fragments of the left to the main, that is when the bottom navigation view is hidden. I attached the main code.
PD: I have all the libraries in 25.3.1 version (if useful).
Thank you for your attention.
activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerr"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#0f6b96"
app:itemIconTint="#drawable/color_state_light"
app:itemTextColor="#drawable/color_state_light"
app:menu="#menu/menu_bottom_navigation_basic">
</android.support.design.widget.BottomNavigationView>
MainActivity.java
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transation = fragmentManager.beginTransaction();
transation.replace(R.id.container, new Home()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transation = fragmentManager.beginTransaction();
switch (item.getItemId()){
case R.id.home:
transation.replace(R.id.container, new Home()).commit();
return true;
case R.id.ofertas:
transation.replace(R.id.container, new Ofertas()).commit();
return true;
case R.id.contacto:
transation.replace(R.id.container, new Contacto()).commit();
return true;
}
return false;
}
};
I add you two images too.
Ofertas Fragment
Home Fragment

the problem is in fitSystemWindow == true , i had similar issue , after deleting it the problem has gone.

Related

How to open search interface over BottomNavigationView's fragment?

I want to implement an interface having BottomNavigationView where clicking on the top search bar opens the next interface shown in figure 2. I have implemented BottomNavigationView. But unable to do that above said.
Here is the code:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNavigationView = findViewById(R.id.home_bottom_nav_view);
bottomNavigationView.setSelectedItemId(R.id.home_recent_menu_id);
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = new RecentFragment();
switch (item.getItemId()) {
case R.id.home_contact_menu_id:
selectedFragment = new ContactFragment();
break;
case R.id.home_recent_menu_id:
selectedFragment = new RecentFragment();
break;
case R.id.home_status_menu_id:
selectedFragment = new StatusFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.home_fragment_container_lyt_id, selectedFragment).commit();
return true;
}
});
}
}
Home Activity XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="#+id/home_search_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#+id/home_fragment_container_lyt_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/home_search_view_id"
android:layout_above="#+id/home_bottom_nav_view"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/home_bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="#menu/bottom_nav_menu"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
create a BottomNavigationFragment which is going to contain ViewPager2 with BottomNavigationView and that view pager will contain [favourite, recent and contacts ] fragments
create a SearchFragment which will be used for searching purpose
create a navigation graph which will contain [BottomNavigationFragment and SearchFragment] when user clicks on search view navigate him to SearchFragment
you can also set animation using Navigation Component lib
//adapter for ViewPager2 vp2 in BottomNavigationFragment
lets say your SearchView is in favourite fragment onClick or onFocus of that
findNavController().navigate(R.id.toSearchFragment)
you will land in Search fragment

Recylerview appends the old result on back pressed

I'm trying to create a application with bottom navigation view, But it looks like it is stacking up the fragments. Let me show you the picture to be clear.
[![Blank screen when back button is clicked again][3]][3]
Here is the code:
Home.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/bottomfrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetStart="0dp"
app:fabAlignmentMode="center">
<com.rawat.soccermatch.CustomBottomNav
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemRippleColor="#android:color/transparent"
app:labelVisibilityMode="labeled"
app:itemIconTint="#color/enable_disable"
app:itemTextColor="#color/enable_disable"
app:menu="#menu/bottom_nav_menu"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:contentDescription="#string/title_create"
android:src="#drawable/ic_plusn"
app:tint="#fff"
app:layout_anchor="#id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Home.java
bt = findViewById(R.id.bottomNavigationView);
loadFragment(new HomeFrag()); // at first home fragment will be loaded
bt.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment mfragment;
int item_id=item.getItemId();
if(item_id==R.id.navigation_home){
mfragment = new HomeFrag();
loadFragment(mfragment);
}
else if(item_id==R.id.navigation_feed){
mfragment = new Feed();
loadFragment(mfragment);
}
else if(item_id==R.id.navigation_create){
mfragment = new creatematch();
loadFragment(mfragment);
}
else if (item_id==R.id.navigation_following){
mfragment = new Following();
loadFragment(mfragment);
}
else {
mfragment = new Account();
loadFragment(mfragment);
}
return true;
}
});
ft = findViewById(R.id.fab);
ft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ft.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
ft.setRippleColor(Color.GREEN);
Fragment fragment = new creatematch();
bt.getMenu().findItem(R.id.navigation_create).setChecked(true);
loadFragment(fragment);
}
});
}
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.bottomfrag, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Should I post HomFrag Code too?
I have edited the question now the problem which I understand is when clicking back my Home Fragment is resuming from where it was left but the recylerview values are resetting and appends the new results with old. Also the ripple color is not changing from following to home.
Try to use this function instead of yours
public void loadFragment(Fragment fragment) {
try {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.addToBackStack(null);
ft.replace(R.id.bottomfrag, fragment);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
you are adding that fragment to backstack so when you click back detach it, you need to make sure that your fragment is not added to backstack
Override onBackPressed() method. Inside it you can check what section is opened at the current moment and what you want to do in this case (nothing/close the app/navigate to another section).
in the bottom navigation activity you have to set default fragment is select which fragment you want for eg in common Home fragment always show as a default fragment so set homefragment as default.
set these code on create
bottomNavigationView.setSelectedItemId(R.id.nav_home);
checkon backpress
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
}
}

Bottom Navigation View gets Shrink Down

I have made an app with bottom navigation view with Promo, Store, Reward, Coupon and Account tabs when I am changing from Coupon fragment to any other fragment, the bottom navigation view gets shrunk down as shown in the image, I tried changing layout width, height and converting coordinator layout to linear layout but it didn't help. The problem is occurring when I'm switching from home to any other tab only.
Layout File activity_main.xml
<android.support.constraint.ConstraintLayout
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp"
android:text="#string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemTextColor="#color/selector_bottom_navigation"
app:itemIconTint="#color/selector_bottom_navigation"
app:menu="#menu/navigation" />
Java File MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//loading the default fragment
loadFragment(new PromoFragment());
//getting bottom navigation view and attaching the listener
BottomNavigationView navigation = findViewById(R.id.navigation);
BottomNavigationViewUtils.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
getMenuInflater().inflate(R.menu.menu_wallet, menu);
return super.onCreatePanelMenu(featureId, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_wallet1:
return true;
case R.id.menu_qrcode:
Intent intent = new Intent(this, ScannerActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_promo:
fragment = new PromoFragment();
break;
case R.id.navigation_store:
fragment = new StoreFragment();
break;
case R.id.navigation_reward:
fragment = new RewardFragment();
break;
case R.id.navigation_coupon:
fragment = new CouponFragment();
break;
case R.id.navigation_account:
fragment = new AccountFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
This is the picture I meant:
I previously added SearchView to the coupon fragment and fragment store
It turns out that if you are using a coordinator layout and viewpager inside a fragment, you will notice that the viewpager extends the screen a bit. Just disable the scroll features of the coordinator layout inside the fragment and you will notice the bottom bar doesn't get shrunk. Weird I know, but it works.
I add android:fitsSystemWindows="false" in CoordinatorLayout and it worked
Find android:fitsSystemWindows="true" in your fragment xml and remove this line
or change it to android:fitsSystemWindows="false". Your problem will be solved.

How to reference to more than 1 layout in Android?

I hope I'm explaining this correctly. Basically I have a basic Android app with navigation bar on the left, and I'm trying to make the content page change layout based on what I select there.
What I did was to include both layouts on the application main layout, but when I try selecting from the nav bar, the layouts are on top of each other.
Can I get a pointer of to make them not to lay on top of each other? Am I approaching this correctly?
Here's my 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="com.test.justforfun.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<include layout="#layout/content_second"/>
</android.support.design.widget.CoordinatorLayout>
Show one layout one time, and show another when you select sth.
Set the visibility to GONE when you need to hide the layout. And then again to VISIBLE when you want to show it.
You need to use Fragment instead of hiding and showing layout/views. Set listeners on your Navigation Bar and use FragmentManager to replace your current fragment.
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = FirstFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SecondFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = ThirdFragment.class;
break;
default:
fragmentClass = FirstFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
source: https://guides.codepath.com/android/Fragment-Navigation-Drawer

Fragment elements remain even when the fragment has been changed

I'm using a navigation drawer to switch fragments, but elements from the first fragment stay present even on changing fragments.
The code of the main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
Drawer result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.addDrawerItems(
item1,
new DividerDrawerItem(),
item2,
new SecondaryDrawerItem().withName(R.string.drawer_item_settings)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment fragment = new Fragment();
switch (position)
{
case 0:
fragment = new HomeFragment();
Log.d("Switch", "Search");
break;
case 1:
break;
case 2:
fragment = new InventoryFragment();
Log.d("Switch", "Inventory");
break;
case 3:
fragment = new SettingsFragment();
Log.d("Switch", "Settings");
break;
}
transaction.replace(R.id.main_frame, fragment);
transaction.commit();
return false;
}
})
.build();
}
The following is the code for activity_main.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"
android:background="?android:windowBackground"
tools:context="com.example.nihal.xchange.MainActivity">
...
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/main_frame">
<fragment
android:name="com.example.nihal.xchange.HomeFragment"
android:id="#+id/current_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Elements from Home Fragment remain present across fragments but this behavior is not exhibited by elements of other fragment.
How do I prevent this from happening?
Don't include the Home Fragment in the xml. Keep your container FrameLayout empty and add the initial fragment as soon as the activity starts. In your Activity's onCreate method, add the Home fragment dynamically in the beginning.
activity_main.xml -
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/main_frame">
<!-- Removed the fragment from here -->
</FrameLayout>
In the onCreate(), add it dynamically -
if (findViewById(R.id.main_frame) != null) {
if (savedInstanceState != null)
return;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main_frame, new HomeFragment());
transaction.commit();
}

Categories

Resources