简体   繁体   中英

How to properly set layout visibility from another activity?

I have a LinearLayout in my BottomNavgigation's MainActivity, and I'm trying to hide the layout from one of the fragments, but I keep getting a nullpointer exception, and i'm quite certain everything is set right

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"
        android:orientation="vertical"
        android:background="@color/colorGrey"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation" />

    <LinearLayout
        android:id="@+id/create_transaction_layout"  \\view I'm trying to set visibility on
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_padding_size_60"
        android:layout_gravity="bottom"
        android:layout_marginBottom="@dimen/margin_padding_size_24"
        android:gravity="center"
        android:visibility="visible">

        <LinearLayout
            android:id="@+id/send_receive_layout"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/margin_padding_size_40"
            android:background="@drawable/card_bg"
            android:elevation="4dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/fab_send"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/card_bg"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center"
                android:paddingStart="@dimen/margin_padding_size_16"
                android:paddingEnd="@dimen/margin_padding_size_16">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/ic_communication_call_made"
                    android:drawablePadding="8dp"
                    android:gravity="center_vertical"
                    android:text="Send"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="18sp"
                    app:fontFamily="@font/source_sans_pro" />

            </LinearLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="24dp"
                android:background="@color/colorPrimary" />

            <LinearLayout
                android:id="@+id/fab_receive"
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin_padding_size_40"
                android:background="@drawable/card_bg"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center"
                android:paddingStart="@dimen/margin_padding_size_16"
                android:paddingEnd="@dimen/margin_padding_size_16">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/ic_communication_call_received"
                    android:drawablePadding="8dp"
                    android:gravity="center_vertical"
                    android:text="Receive"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="18sp"
                    app:fontFamily="@font/source_sans_pro" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static BottomNavigationView navView;
    private LinearLayout createTransaction;

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

        createTransaction = findViewById(R.id.create_transaction_layout);

        navView = findViewById(R.id.nav_view);
        addBadgeView();
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_transactions, R.id.navigation_wallets, R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

    private void addBadgeView() {
        navView = findViewById(R.id.nav_view);
        navView.getOrCreateBadge(R.id.navigation_transactions); // Show badge
    }

    public void hideFAB() {
        createTransaction.setVisibility(View.GONE);
    }

}

and in my Fragment I try setting visibility like this

WalletFragment.java

private MainActivity mainActivity;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    walletsViewModel =
            ViewModelProviders.of(this).get(WalletsViewModel.class);
    View root = inflater.inflate(R.layout.fragment_wallets, container, false);

    mainActivity.hideFAB();

    return root;
}

please what could I have done wrong? as everything seems fine to me

Inside your fragment you can call

((MainActivity) requiredActivity()).hideFAB()

Use getActivity to get the instance of hosting activity, cast it to its class and then call the public methods.

( (MainActivity) getActivity() ).hideFAB()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM