Search code examples
javaandroidvariablesandroid-activitybottomnavigationview

Android/Java: How to access to my BottomNavigationView specific variables from another activity?


I would like to access to a variable initialization from NewBottomNav in LocalNav, but I don't know how to access it?

NewBottomNav class:

public final class newBottomNav extends BottomNavigationView {
    private final Context context;
    private Typeface fontFace = null;
    public static Integer initialization = 0;

...
}

LocalNav Class:

public class LocalNav extends AppCompatActivity {
    Context mContext;
    WebView view;
    BottomNavigationView bottomNavigation;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int size = bottomNavigation.getMenu().size();

        bottomNavigation.setOnNavigationItemSelectedListener (new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem Item) {

            bottomNavigation.initialization = 1; //<= How to access to initialization
...
}

XML:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LocalNav">

    <WebView
        android:id="@+id/vieweb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="-2dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.NewTelApps.ToEvent.newBottomNav
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        app:itemIconSize="45dp"
        android:visible="false"
        app:itemBackground="@drawable/nav_item_drawable"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

I don't know how to access it because it is a BottomNavigationView. Is there any specific things to do?

Thanks in advance.


Solution

  • You just need to change the declaration from

    BottomNavigationView bottomNavigation;
    

    to

    newBottomNav bottomNavigation;
    

    As initialization is not defined in BottomNavigationView