Bottom Navigation View gets Shrink Down - android

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.

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

Bottom Navigation Need To Clicked 2 Times Before Executing Task

I have a bottom navigation bar, I don't know why, but I need to click it 2 times before executing the task.
This is the xml for bottom navigation
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
android:focusableInTouchMode="true"
app:menu="#menu/bottom_navigation_admin" />
This is how I call it on activity
private void setBottomNavigation() {
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);
bottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_override :
fragment = new OverrideFragment();
fragmentManager.beginTransaction().replace(R.id.fragment_layout, fragment).commit();
break;
case R.id.navigation_user:
fragment = new UserListFragment();
fragmentManager.beginTransaction().replace(R.id.fragment_layout, fragment).commit();
break;
case R.id.navigation_log_out:
CustomDialog customDialog = new CustomDialog(AdminBoardActivity.this);
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.show();
break;
}
}
});
}
You use
bottomNavigationView.setOnNavigationItemReselectedListener instead of
bottomNavigationView.setOnNavigationItemSelectedListener
not Reselected only Selected ;)
You are setting a reselect listener but you need to set onitem select listener.
replace
bottomNavigationView.setOnNavigationItemReselectedListener()
with
BottomNavigationView.OnNavigationItemSelectedListener()
you are using setOnNavigationItemReselectedListener
use this
setOnNavigationItemSelectedListener

RecyclerView does not scroll in Nested Fragment

I am using NavigationDrawer and on item selection, I am changing fragment using hide/show technique instead of replace.
if(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT) != null){
//if the fragment exists, show it.
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT)).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.flContent,
BottomNavTasksFragment.newInstance(apiToken, empId), TAG_TASKS_FRAGMENT)
.commit();
}
// Hide other fragments
The Fragmanet BottomNavTasksFragmet contains two fragments, one for list (RecyclerView) and other for Google Maps. However, a fragment containing RecyclerView does not respond to any touch when opened second time. It scrolls smoothly on first load but doesn't work when shown second time.
This is xml layout of BottomNavTasksFragment
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.biocare.fragments.BottomNavTasksFragment">
<FrameLayout
android:id="#+id/bottomNavContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="#color/colorSeparator"
app:itemBackground="#drawable/navigation_bar_item_bg"
app:itemIconTint="#color/bottom_nav_icon_color_selector"
app:itemTextColor="#color/bottom_nav_icon_color_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/menu_bottom_navigation" />
The FrameLayout bottomNavContainer is replaced when clicked on BottomNavigationItem.
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_bottom_nav_tasks,
container, false);
final BottomNavigationView navigationView = view.findViewById(R.id.bottomNavigationView);
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_bottom_tasks:
if(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT) != null){
//if the fragment exists, show it.
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT)).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.bottomNavContainer,
TasksFragment.newInstance(apiToken, empId), TAG_TASKS_FRAGMENT)
.commit();
}
// Hide map fragment
if(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT) != null){
fragmentManager.beginTransaction()
.hide(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT)).commit();
}
break;
case R.id.navigation_bottom_map:
if(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT) != null){
//if the fragment exists, show it.
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT)).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.bottomNavContainer,
MapViewFragment.newInstance(apiToken, empId), TAG_MAP_FRAGMENT)
.commit();
}
// Hide Tasks fragment
if(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT) != null){
fragmentManager.beginTransaction()
.hide(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT)).commit();
}
break;
}
return false;
}
});
FragmentTransaction t = fragmentManager.beginTransaction();
TasksFragment tasksFragment = TasksFragment.newInstance(apiToken, empId);
t.add(R.id.bottomNavContainer, tasksFragment, TAG_TASKS_FRAGMENT);
t.commit();
return view;
}
When clicked on Tasks, Scroll doesn't work. See next image]
Stuck, RecyclerView not responding, however Menu is working
If further information is required, let me know.
Thanks

BOTTOM NAVIGATION VIEW HIDES

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.

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

Categories

Resources