BottomNavigationView Doesn't come at the bottom after adding a fragment

I am starting in Android, today I created a BottomNavigationView activity, I want to show 3 different tabs with navigation buttons, so I created 3 fragments, the problem is that after adding the fragments, the BottomNavigationView shows at the top side like this, what should I do if I want BottomNavigationView at the bottom as it was before adding fragments here is my main activity code

package com.hackerinside.jaisonjoseph.polysocial;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView mTextMessage;

public BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:

                tab1 radio = new tab1();
                android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();


            case R.id.navigation_dashboard:


                tab2 radio1 = new tab2();
                android.support.v4.app.FragmentManager manager1 = getSupportFragmentManager();
                manager1.beginTransaction().replace(R.id.container, radio1, radio1.getTag()).commit();

            case R.id.navigation_notifications:

                tab3 radio2 = new tab3();
                android.support.v4.app.FragmentManager manager2 = getSupportFragmentManager();
                manager2.beginTransaction().replace(R.id.container, radio2, radio2.getTag()).commit();



        }
        return false;
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);






    tab1 radio = new tab1();
    android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

 }

      

this is activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@android:color/holo_blue_dark">

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home" />

</FrameLayout>

<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="?android:attr/windowBackground"
    app:menu="@menu/navigation" />

      

this is my first snippet of tab1

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

      

+3


source to share


2 answers


BottomNavigationView

does not appear automatically at the bottom of the window. You have to place them manually. You can use for this RelativeLayout

.

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">

  <FrameLayout
      android:id="@+id/content"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:background="@android:color/holo_blue_dark">

      <TextView
          android:id="@+id/message"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="@dimen/activity_vertical_margin"
          android:layout_marginLeft="@dimen/activity_horizontal_margin"
          android:layout_marginRight="@dimen/activity_horizontal_margin"
          android:layout_marginTop="@dimen/activity_vertical_margin"
          android:text="@string/title_home" />

  </FrameLayout>

  <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="?android:attr/windowBackground"
      android:layout_alignParentBottom="true"
      app:menu="@menu/navigation" />
</RelativeLayout>

      



If you changed your root LinearLayout

to RelativeLayout

and added a parameter android:layout_alignParentBottom="true"

to BottomNavigationView

. Hope it helps.

If you need more help, maybe this link can help: https://medium.com/@hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0

+2


source


By the way, you can also look at https://github.com/roughike/BottomBar This is a custom view like the new bottom material structure template ( https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation- behavior ).



0


source







All Articles