0

Hi I'm making Android App with Exercises and I already included Navigation Drawer, but does is Possible to add Expand and Hide button so when I press Chest Exercises to show all Chest Exercises when I press the button to hide them same for other Exercises. I Already included NavigationDrawer I read that I can add ListView but I don't know how.

Thanks

Here is my Code:

MainActivity.java

package com.Hristijan.Aleksandar.GymAssistant.Exercises;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String s = getIntent().getStringExtra("qrcode_result");
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

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

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


        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();


        if (s != null) {

            if (s.equals("Bench Press")) {
                setTitle("Bench Press");

                BenchFragment fragment = new BenchFragment();
                fragmentTransaction.replace(R.id.frame, fragment, "Bench Press");
                fragmentTransaction.commit();

            } else if (s.equals("Incline Bench Press")) {
                setTitle("Incline Bench Press");

                InclineFragment fragment = new InclineFragment();
                fragmentTransaction.replace(R.id.frame, fragment, "Incline Bench");
                fragmentTransaction.commit();

            } else if (s.equals("Decline Bench Press")) {
                setTitle("Decline Bench Press");

                DeclineFragment fragment = new DeclineFragment();
                fragmentTransaction.replace(R.id.frame, fragment, "Decline Bench");
                fragmentTransaction.commit();

            } else if (s.equals("Dumbell Flys")) {
                setTitle("Dumbell Flys");

                DumbellFlysFragment fragment = new DumbellFlysFragment();
                fragmentTransaction.replace(R.id.frame, fragment, "Dumbell Flys");
                fragmentTransaction.commit();

            } else if (s.equals("Chest Machine")) {
                setTitle("Chest Machine");

                ChestMachineFragment fragment = new ChestMachineFragment();
                fragmentTransaction.replace(R.id.frame, fragment, "Chest Machine");
                fragmentTransaction.commit();

            } else if (s.equals("Push Ups")) {
                setTitle("Push Ups");

                PushupsFragment fragment = new PushupsFragment();
                fragmentTransaction.replace(R.id.frame, fragment, "Push Ups");
                fragmentTransaction.commit();
            }

            else{
                Toast.makeText(this,"You scanned QR code that doesn't exist",Toast.LENGTH_SHORT).show();
                setTitle("Home");
                HomeFragment fragment = new HomeFragment();
                fragmentTransaction.replace(R.id.frame, fragment, "Home");
                fragmentTransaction.commit();
            }

        } else {
            setTitle("Home");
            HomeFragment fragment = new HomeFragment();
            fragmentTransaction.replace(R.id.frame, fragment, "Home");
            fragmentTransaction.commit();

        }

    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();


        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.home_fragment) {
            setTitle("Home");
            HomeFragment fragment = new HomeFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Home  ");
            fragmentTransaction.commit();

        } else if (id == R.id.qr_activity) {
            Intent i = new Intent(MainActivity.this,QrActivity.class);
            startActivity(i);
            return true;

        } else if (id == R.id.bmi_calculator) {
            Intent i = new Intent(MainActivity.this,BmiActivity.class);
            startActivity(i);
            return true;

        } else if (id == R.id.about_fragment) {
            setTitle("About");
            AboutFragment fragment = new AboutFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"About");
            fragmentTransaction.commit();

        } else if (id == R.id.bench_fragment) {
            setTitle("Bench Press");
            BenchFragment fragment = new BenchFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Bench Press");
            fragmentTransaction.commit();

        } else if (id == R.id.incline_fragment) {
            setTitle("Incline Bench Press");
            InclineFragment fragment = new InclineFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Incline Bench Press");
            fragmentTransaction.commit();

        } else if (id == R.id.decline_fragment) {
            setTitle("Decline Bench Press");
            DeclineFragment fragment = new DeclineFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Decline Bench Press");
            fragmentTransaction.commit();
        } else if (id == R.id.dumbellflys_fragment) {
            setTitle("Dumbell Flys");
            DumbellFlysFragment fragment = new DumbellFlysFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Dumbell Flys");
            fragmentTransaction.commit();
        } else if (id == R.id.chestmachine_fragment) {
            setTitle("Chest Machine");
            ChestMachineFragment fragment = new ChestMachineFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Chest Machine");
            fragmentTransaction.commit();
        } else if (id == R.id.pushups_fragment) {
            setTitle("Push Ups");
            PushupsFragment fragment = new PushupsFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Push Ups");
            fragmentTransaction.commit();

        } else if (id == R.id.latpulldown_fragment) {
            setTitle("Lat Pull Down");
            LatPullDownFragment fragment = new LatPullDownFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Lat Pull Down");
            fragmentTransaction.commit();

        } else if (id == R.id.latpulldownrear_fragment) {
            setTitle("Rear Lat Pull Down");
            LatPullDownRearFragment fragment = new LatPullDownRearFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Rear Lat Pull Down");
            fragmentTransaction.commit();
        } else if (id == R.id.deadlift_fragment) {
            setTitle("Dead Lift");
            DeadLiftFragment fragment = new DeadLiftFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Dead Lift");
            fragmentTransaction.commit();

        } else if (id == R.id.tbar_fragment) {
            setTitle("T-Bar Row");
            TbarFragment fragment = new TbarFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"T-Bar Row");
            fragmentTransaction.commit();

        } else if (id == R.id.barbellrow_fragment) {
            setTitle("Barbell Row");
            BarbellRowFragment fragment = new BarbellRowFragment ();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Barbell Row");
            fragmentTransaction.commit();

        } else if (id == R.id.crunches_fragment) {
            setTitle("Crunches");
            CrounchesFragment fragment = new CrounchesFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Crunches");
            fragmentTransaction.commit();

        } else if (id == R.id.plank_fragment) {
            setTitle("Plank");
            PlankFragment fragment = new PlankFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Plank");
            fragmentTransaction.commit();

        } else if (id == R.id.legraises_fragment) {
            setTitle("Leg Raises");
            LegRaisesFragment fragment = new LegRaisesFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Leg Raises");
            fragmentTransaction.commit();

        } else if (id == R.id.weightedcrunch_fragment) {
            setTitle("Weighted Crunch");
            WeightedCrunchFragment fragment = new WeightedCrunchFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Weighted Crunch");
            fragmentTransaction.commit();

        } else if (id == R.id.legextension_fragment) {
            setTitle("Leg Extension");
            LegExtensionFragment fragment = new LegExtensionFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Leg Extension");
            fragmentTransaction.commit();

        } else if (id == R.id.legpress_fragment) {
            setTitle("Leg Press");
            LegPressFragment fragment = new LegPressFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Leg Press");
            fragmentTransaction.commit();

        } else if (id == R.id.barbelllunges_fragment) {
            setTitle("Barbell Lunge");
            BarbellLungesFragment fragment = new BarbellLungesFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Barbell Lunge");
            fragmentTransaction.commit();

        } else if (id == R.id.squats_fragment) {
            setTitle("Squats");
            SquatsFragment fragment = new SquatsFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Squats");
            fragmentTransaction.commit();

        } else if (id == R.id.millitarypress_fragment) {
            setTitle("Millitary Press");
            MillitaryPressFragment fragment = new MillitaryPressFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Millitary Press");
            fragmentTransaction.commit();

        } else if (id == R.id.arnoldpress_fragment) {
            setTitle("Arnold Press");
            ArnoldPressFragment fragment = new ArnoldPressFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Arnold Press");
            fragmentTransaction.commit();

        } else if (id == R.id.uprightrows_fragment) {
            setTitle("Upright Rows");
            UprightRowsFragment fragment = new UprightRowsFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Upright Rows");
            fragmentTransaction.commit();

        } else if (id == R.id.weightplate_fragment) {
            setTitle("Weight Plate Front Raise");
            WeightPlateFragment fragment = new WeightPlateFragment();
            FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame,fragment,"Weight Plate Front Raise");
            fragmentTransaction.commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:background="@drawable/background"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#000000"
        android:theme="@style/NavigationView"
        app:itemTextColor="#ffffff"
        app:itemIconTint="#ffffff"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

nav_header_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:layout_width="@dimen/nav_drawer_width"
        android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/backgroundheader"
        android:gravity="bottom"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            app:srcCompat="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:textStyle="bold"
            android:textColor="#ffffff"
            android:text="Welcome to GymAssistant"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/textView"
            android:textColor="#ffffff"
            android:textStyle="italic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Best Gym Workout App" />

    </LinearLayout>

activity_main_drawer.xml

  <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:showIn="navigation_view">
        <group
            android:layout_height="wrap_content"
            android:checkableBehavior="single">
        <item android:title="Home">
            <menu>
                <item
                    android:id="@+id/home_fragment"
                    android:icon="@drawable/home"
                    android:title="Home" />
                <item
                    android:id="@+id/qr_activity"
                    android:icon="@drawable/qrcodeiconnew"
                    android:title="Scan QR Code" />
                <item
                    android:id="@+id/bmi_calculator"
                    android:icon="@drawable/bodyicon"
                    android:title="BMI Calculator" />

                <item
                    android:id="@+id/about_fragment"
                    android:icon="@drawable/aboutus"
                    android:title="About" />
            </menu>
        </item>

        <item android:title="Chest Exercises">
            <menu>
                <item
                    android:id="@+id/bench_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Bench Press" />
                <item
                    android:id="@+id/incline_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Incline Bench Press" />
                <item
                    android:id="@+id/decline_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Decline Bench Press" />
                <item
                    android:id="@+id/dumbellflys_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Dumbell Flys" />
                <item
                    android:id="@+id/chestmachine_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Chest Machine" />
                <item
                    android:id="@+id/pushups_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Push Ups" />

            </menu>
        </item>


        <item android:title="Back Exercises">
            <menu>
                <item
                    android:id="@+id/latpulldown_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Lat Pull Down" />

                <item
                    android:id="@+id/latpulldownrear_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Rear Lat Pull Down" />

                <item
                    android:id="@+id/deadlift_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Dead Lift" />

                <item
                    android:id="@+id/tbar_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="T-bar Rows" />

                <item
                    android:id="@+id/barbellrow_fragment"
                    android:icon="@drawable/benchicon"
                    android:title="Barbell Row" />
            </menu>
        </item>

            <item android:title="Abs Exercises">
                <menu>
                    <item
                        android:id="@+id/crunches_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Crunches" />
                    <item
                        android:id="@+id/plank_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Plank" />
                    <item
                        android:id="@+id/legraises_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Leg Raises" />
                    <item
                        android:id="@+id/weightedcrunch_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Weighted Crunch" />
                </menu>
            </item>

            <item android:title="Legs Exercises">
                <menu>
                    <item
                        android:id="@+id/legextension_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Leg Extension" />
                    <item
                        android:id="@+id/legpress_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Leg Press" />
                    <item
                        android:id="@+id/barbelllunges_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Barbell Lunges" />
                    <item
                        android:id="@+id/squats_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Squats" />
                </menu>
            </item>

            <item android:title="Schoulder Exercises">
                <menu>
                    <item
                        android:id="@+id/millitarypress_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Millitary Press" />
                    <item
                        android:id="@+id/arnoldpress_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Arnold Press" />
                    <item
                        android:id="@+id/uprightrows_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Upright Rows" />
                    <item
                        android:id="@+id/weightplate_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Weight Plate Front Raise" />
                </menu>
            </item>

            <item android:title="Biceps Exercises">
                <menu>
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Lat Pull Down" />
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Dead Lift" />
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Rear Lat Pull Down" />
                </menu>
            </item>

            <item android:title="Triceps Exercises">
                <menu>
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Lat Pull Down" />
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Dead Lift" />
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Rear Lat Pull Down" />
                </menu>
            </item>

            <item android:title="Forearms Exercises">
                <menu>
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Lat Pull Down" />
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Dead Lift" />
                    <item
                        android:id="@+id/home_fragment"
                        android:icon="@drawable/benchicon"
                        android:title="Rear Lat Pull Down" />
                </menu>
            </item>
    </group>
    </menu>
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • Can you describe your situation more specific, I not sure what you want since you already know how to close a NavigationView so what the big deal when you add a button and set its own listener to close/open your drawer? – Tuấn Kiệt Feb 27 '18 at 01:39
  • @TuấnKiệt i want to do like this one https://stackoverflow.com/questions/38765790/default-navigation-drawer-view-to-expandablelistview –  Feb 27 '18 at 07:40

0 Answers0