-1

I have a navbar which is supposed to be hidden when a button is pressed, but nothing happens.

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView mavisTxt;
private Button testBtn;
private Fragment navigationBar;


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

}

private void initVariables() {
    navigationBar = new HeaderNav();
    mavisTxt = (TextView)findViewById(R.id.mavisTxt);
    testBtn = (Button)findViewById(R.id.testBtn);


    testBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                FragmentManager fm = getSupportFragmentManager();
                fm.beginTransaction()
                        .hide(navigationBar)
                        .commit();
                mavisTxt.setText("navbar is hidden");
        }
    });

}
}

Here is the java file for the fragment

public class HeaderNav extends Fragment {
    private static Button homeBtn, optionsBtn, connectionBtn, micBtn, aboutBtn;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.navbar, container, false);

        homeBtn = (Button)view.findViewById(R.id.homeBtn);
        optionsBtn = (Button)view.findViewById(R.id.optionsBtn);
        connectionBtn = (Button)view.findViewById(R.id.micBtn);
        micBtn = (Button)view.findViewById(R.id.homeBtn);
        aboutBtn = (Button)view.findViewById(R.id.aboutBtn);

        return view;
    }
}

and the fragment tag in the main_activity xml :

<fragment
        android:id="@+id/navigationBar"
        android:name="com.example.egi.mavisme.HeaderNav"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/navbar" />

what am I doing wrong? none of the solutions here in SO works for me. Someone help me.

EDIT:

I have edited my code and now it hides. I have another issue when adding a custom animation, I am getting a

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.startViewTransition(android.view.View)' on a null object reference

Here is my code now :

public class MainActivity extends AppCompatActivity {
    private TextView mavisTxt;
    private Button testBtn;
    private HeaderNav navigationBar;

    private FragmentManager fm;
    private FragmentTransaction ft;


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

    }

    private void initVariables() {
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        navigationBar = (HeaderNav)fm.findFragmentById(R.id.navigationBar);

        mavisTxt = (TextView)findViewById(R.id.mavisTxt);
        testBtn = (Button)findViewById(R.id.testBtn);


        testBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
                    ft.hide(navigationBar)
                    .commit();
                    if(navigationBar.isHidden()) {mavisTxt.setText("navbar is hidden");}
            }
        });

    }
}
Chain Cross
  • 351
  • 1
  • 2
  • 12

1 Answers1

0

Here are some things to note-

  1. Use the ActionBar/ToolBar, this isn't iOS
  2. You have to add a fragment first
  3. You are using the show call instead of hiding it

    testBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    FragmentManager fm = getSupportFragmentManager();
                    fm.beginTransaction()
                            .**show**(navigationBar)
                            .commit();
                    mavisTxt.setText("navbar is hidden");
            }
        });
    
Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
DennisVA
  • 2,068
  • 1
  • 25
  • 35
  • That is supposed to be .hide() made the corrections and no, still not working. What do you mean by add a fragment first? I already created the fragment did I not? – Chain Cross Jul 16 '18 at 15:47