Search code examples
javaandroidandroid-studioandroid-fragmentsscrollview

How to add ScrollView to Navigation Drawer


I want to add ScrollView to my navigation drawer, I first tried to but the linear layout as a child under Scrollview, but when I opened my fragment, the navigation drawer shows under my fragment. I want to have my fragments untouched, whereas the navigation drawer does now show behind the fragments, and the navigation drawer has scrollview.

drawer code:

public class drawer_home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


private DrawerLayout drawer;
Toolbar toolbar;

TextView mStartDate, mEndDate, mPlan, mDays;
FirebaseAuth firebaseAuth;
FirebaseUser user;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;


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

    toolbar = findViewById(R.id.tb);
    mStartDate = findViewById(R.id.startDateTV);
    mEndDate = findViewById(R.id.endDateTV);
    mPlan = findViewById(R.id.planTV);
    mDays = findViewById(R.id.daysTV);

    setSupportActionBar(toolbar);
    /*TextView textView = (TextView)toolbar.findViewById(R.id.toolbartv);
    textView.setText("No Excusas");
    getSupportActionBar().setDisplayShowTitleEnabled(false);*/

    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    if(savedInstanceState == null) {

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new androidx.fragment.app.Fragment()).commit();
        navigationView.setCheckedItem(R.id.nav_homeMain);
    }
    firebaseAuth = FirebaseAuth.getInstance();
    user = firebaseAuth.getCurrentUser();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("Users");
    Query query = databaseReference.orderByChild("email").equalTo(user.getEmail());
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            //checkc until requiered data get
            for (DataSnapshot ds : dataSnapshot.getChildren()) {

                String startdate = "Fecha de Inicio = " + ds.child("Start Date").getValue();
                String enddate = "Fecha de Salida = " + ds.child("End Date").getValue();
                String plan = "Plan = " + ds.child("Plan").getValue();
                String days = "Tu plan acaba en " + ds.child("Days Left").getValue() +" dias ";


                mStartDate.setText(startdate);
                mEndDate.setText(enddate);
                mPlan.setText(plan);
                mDays.setText(days);


            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    });

}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_homeMain:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new androidx.fragment.app.Fragment()).commit();
            break;
        case R.id.nav_qr:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new QRcodeFragment()).commit();
            break;
        case R.id.nav_profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new UserProfile()).commit();
            break;
        case R.id.nav_order:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new OrdersActivity()).commit();
            break;
        case R.id.nav_nutrition:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new NutritionActivity()).commit();
            break;
        case R.id.nav_chat:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ContactActivity()).commit();
            break;
        case R.id.nav_coach:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new CoachesActivity()).commit();
            break;
        case R.id.nav_information:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new AboutUsActivity()).commit();
            break;
    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }


}

}

xml:

<androidx.drawerlayout.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/homebcgself"
tools:context=".drawer_home"
tools:openDrawer="start">


<LinearLayout

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:background="@drawable/toolbar_bg"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <TextView
        android:id="@+id/toolbartv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="No Excusas"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="#ffffff"
        android:textSize="40sp"
        android:textStyle="bold" />

    </androidx.appcompat.widget.Toolbar>


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/startDateTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginTop="200dp"
                android:textColor="@color/colorWhite"
                android:textSize="30sp"
                android:textStyle="bold" />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />


        <TextView
            android:id="@+id/endDateTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textStyle="bold"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorWhite"/>


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />


        <TextView
            android:id="@+id/planTV"
            android:layout_width="match_parent"
            android:textSize="30sp"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorWhite"/>


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />



    <TextView
        android:id="@+id/daysTV"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginStart="5dp"
        android:textColor="@color/colorWhite"/>


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhite" />




</LinearLayout>




<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_menu" />


</androidx.drawerlayout.widget.DrawerLayout>

example of scrolling I want for the main content:

screen 1

screen 2

when the main content is scrollable, it shows under the first fragment

error part 1

error part 2

error part 3/3


Solution

  • Had a lot of help and figured it out.

    Add scrollview to separate fragment and incorporate in the onCreate.

    '''

    ScrollView

    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:layout_width="match_parent"
    android:layout_height="match_parent">
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/startDateTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginTop="200dp"
            android:textColor="@color/colorWhite"
            android:textSize="30sp"
            android:textStyle="bold" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />
    
        <TextView
            android:id="@+id/endDateTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textStyle="bold"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorWhite"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />
    
        <TextView
            android:id="@+id/planTV"
            android:layout_width="match_parent"
            android:textSize="30sp"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorWhite"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />
    
        <TextView
            android:id="@+id/daysTV"
            android:layout_width="match_parent"
            android:textSize="30sp"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorWhite"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />
    
    </LinearLayout>