0

I'm trying to implement the navigation drawer menu which appears with toolbar on each activity. I've decided to create common menuactivity class, and all other activities extend from it. It works fine except that it puts activity under the toolbar of nav. menu.

MenuActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
...
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                changeActivity();
            }
        };
        drawer.addDrawerListener(toggle);
        toggle.syncState();

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

        navigationView.getMenu().getItem(currentMenuItem).setChecked(true);
...

at the same class I manage the item touches (menu item selection):

Intent intent = new Intent(getApplicationContext(), TrainingActivity.class);
startActivity(intent);

And in TrainingActivity:

public class TrainingActivity extends MenuActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setStartingState(MenuItemNames.TRAINING);
        super.onCreate(savedInstanceState);
        FrameLayout contentLayout = (FrameLayout) findViewById(R.id.content_frame);
        View contentView = getLayoutInflater().inflate(R.layout.activity_training, contentLayout, false);
        drawer.addView(contentView, 0);

//        setContentView(R.layout.activity_training);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

...

Here is activity_menu.xml:

<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:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            layout="@layout/app_bar_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/content_frame"/>
    </LinearLayout>

    <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:fitsSystemWindows="true"

        app:menu="@menu/activity_menu_drawer" />

</android.support.v4.widget.DrawerLayout>

The app_bar_menu.xml:

<android.support.design.widget.CoordinatorLayout 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/app_bar_menu_coord_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.magnifi.pennantrace.MenuActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/relativelayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:layout_height="wrap_content"
                    android:id="@+id/toolbar_div_rank"
                    android:text="4th Place"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent" />

                ...

            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

So the logic I'm trying to implement is that whenever item is selected in the menu, I am loading appropriate Activity (inflating it) into the FrameLayout (content_frame under the toolbar in activity_menu.xml). So seems like it must be under toolbar but it's not. Could you help me and tell me where is my mistake?

maximus
  • 4,201
  • 15
  • 64
  • 117
  • `drawer.addView(contentView, 0);` - You want to add `contentView` to `contentLayout`, not `drawer` – Mike M. Jun 12 '17 at 11:06
  • @MikeM. if I do the contentLayout.addView(contentView, 0); then it doesn't show anything just blank, except menu/toolbar. – maximus Jun 12 '17 at 11:14
  • Yeah, just noticed, you've got two things inside the vertical `LinearLayout` that's holding `content_frame`, and both have `match_parent` heights, so `content_frame` is being pushed out the bottom. You probably want to move `content_frame` into `app_bar_menu`, and just get rid of the enclosing `LinearLayout` in the `DrawerLayout`. – Mike M. Jun 12 '17 at 11:19
  • @MikeM. tried, didn't work. (no difference) – maximus Jun 12 '17 at 11:27
  • Well, you kinda need to know what you're doing with `CoordinatorLayout`. It's tricky, sometimes. Are you sure you need a `CoordinatorLayout`? In any case, if you've put the `FrameLayout` below the `AppBarLayout` in the XML, you can add this to it: https://stackoverflow.com/a/33229375. – Mike M. Jun 12 '17 at 11:31
  • @MikeM. Yeah tried it as well. Kind of weird, there must be something else wrong. Still working on that – maximus Jun 12 '17 at 12:14
  • If you don't really need the `CoordinatorLayout`, I'd just replace it with a vertical `LinearLayout`. Just make sure to get the `layout_height`s right. – Mike M. Jun 12 '17 at 12:17

1 Answers1

0

I've solved the issue by replacing activities by fragments. So instead of declaring the activities as: public class TrainingActivity extends MenuActivity {

I replaced it by: public class TrainingActivity extends Fragment {

Example of activity changed to fragment:

public class TrainingActivity extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static TrainingActivity newInstance(String param1, String param2) {
        TrainingActivity fragment = new TrainingActivity();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_training, container, false);
        imageButtonsSetup(view);
        return view;
    }

    private void imageButtonsSetup(View view) {
        setImageButtonWithResourceId(view, R.id.imageButtonSelectTraining);
        setImageButtonWithResourceId(view, R.id.imageButtonSelectPlayers);
    }

    private void setImageButtonWithResourceId(View view, int resourceId) {
        ((ImageButton)view.findViewById(resourceId)).setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageButton view = (ImageButton ) v;
                        view.getBackground().setColorFilter(0x77777700, PorterDuff.Mode.SRC_ATOP);
                        v.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:

                        // Your action here on button click

                    case MotionEvent.ACTION_CANCEL: {
                        ImageButton view = (ImageButton) v;
                        view.getBackground().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return true;
            }
        });
    }
}

And if menu item selected changed i did the following:

try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

So I didn't need anymore the drawer addView etc. As I am now replacing the content_frame.

The final app_bar_menu.xml is changed like this:

<LinearLayout 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/app_bar_menu_coord_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context="com....">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            .....
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/content_frame"/>
</LinearLayout>
maximus
  • 4,201
  • 15
  • 64
  • 117