0

how to change the tabs container on click Home item?

bottom_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
        android:id="@+id/action_item1"
        android:icon="@drawable/ic_format_list_bulleted_black_24dp"
        android:title="Status"
        android:onClick="go_page('Stat')"/>
</menu>

From tabs.xml

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@color/greyish"
        app:itemIconTint="@color/black"
        app:itemTextColor="@color/black"
        app:menu="@menu/bottom_menu"/>

tabs.java

public void go_page(String fragmentId) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentId);
        if (fragment == null) {
            if (fragmentId.equals("Stat")) {
                fragment = StatTab.newInstance();
            }
            if (fragmentId.equals("Other")) {
                fragment = OtherTab.newInstance();
            }
            ft.replace(R.id.container, fragment, fragmentId).commit();
        }
    }

StatTab fragment:

public class StatTab extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_stat_tab, container, false);
    }
}

Cannot resolves name newInstance()

thank you for any help

Fresco
  • 343
  • 3
  • 13
  • 1
    You do not have a `static` method `newInstance()` declared in `StatTab.java`. What do you expect to happen you you do not have such a method? – azizbekian Dec 26 '17 at 10:28

1 Answers1

1

You need to craete newInstance() method in your StatTaband OtherTab fragment like below code

 public class StatTab extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_stat_tab, container, false);
        }

        public  static StatTab newInstance() {
        return new StatTab();
      }
    }


 public class OtherTab extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_other_tab, container, false);
        }

        public static OtherTab newInstance() {
        return new OtherTab();
      }
    }
Goku
  • 9,102
  • 8
  • 50
  • 81
  • Something wrong on adding onClick in xml file `android.view.InflateException: Binary XML file line #53: Error inflating class android.support.design.widget.BottomNavigationView` – Fresco Dec 26 '17 at 10:38
  • @user3449848 didn't get u – Goku Dec 26 '17 at 10:44
  • I can't implement `android:onClick="go_page('Stat')"` from `bottom_menu.xml` for `public void go_page(String fragmentId)`. Any other alternative way to do it? – Fresco Dec 26 '17 at 10:49
  • @user3449848 remove `android:onClick="go_page('Stat')"` from menu.xml – Goku Dec 26 '17 at 10:55
  • @user3449848 this will help u https://stackoverflow.com/questions/40202294/set-selected-item-in-android-bottomnavigationview – Goku Dec 26 '17 at 10:56
  • Got it. Can you tell me how to call this function if I remove it? Something like `OnClickListener`? Or can you refer me a tutorial where `Tablayout`and `BottomNavigationView` implemented together? – Fresco Dec 26 '17 at 11:07
  • @user3449848 in that link the information give please check aghain – Goku Dec 26 '17 at 11:10