MarcDahlem / AndroidSidemenuFooterExample

14 stars 11 forks source link

Footer Shows at the most Top of Menu #1

Open BuraqsysStudio opened 4 years ago

BuraqsysStudio commented 4 years ago

I have used your example for using a custom footer. All works fine, In the preview Window the Footer Layout is at the bottom where it should be. But when I run the app it shows at the top most of Navigation Drawer. Please Help.

[Screenshot]https://ibb.co/M1kr6H5 Here is the screenshot

MarcDahlem commented 4 years ago

Hi BuraqsysStudio,

the important part is the "spacer" between the inner NavigationDrawer and the Footer: https://github.com/MarcDahlem/AndroidSidemenuFooterExample/blob/master/app/src/main/res/layout/activity_main.xml#L44

This spacer must be placed in a layout, which allows the 'weight' setting and must be placed on the same level as the navigationdrawer/real menu, and the footer. Does this help? If not, you can send me your layout.xml if you want and I will check it.

BuraqsysStudio commented 4 years ago

Thanks for such quick response. I have used the same method you have used. Here is the XML:

`<?xml version="1.0" encoding="utf-8"?> <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/consumer_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".Ui.Consumer.Profile" tools:openDrawer="start">

<include
    layout="@layout/consumer_profile"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.google.android.material.navigation.NavigationView
                android:id="@+id/consumer_navbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/dark"
                app:elevation="0dp"
                app:itemBackground="@drawable/menu_selected"
                app:itemTextColor="@color/white"
                app:menu="@menu/nav_menu_consumer" />

            <LinearLayout
                android:id="@+id/spacer_to_bottom"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="vertical" />

            <include layout="@layout/bottom_navigation_items"></include>

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>`

And the Class:

`public class Profile extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { DrawerLayout drawerLayout; NavigationView navigationView; Toolbar support_toolbar; ImageView edit_profile_btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consumer_profile);
    drawerLayout = findViewById(R.id.consumer_drawer_layout);
    navigationView = findViewById(R.id.consumer_navbar);
    support_toolbar = findViewById(R.id.profile_toolbar);
    edit_profile_btn = findViewById(R.id.edit_profile_btn);
    setSupportActionBar(support_toolbar);
    navigationView.setItemIconTintList(null);

    navigationView.bringToFront();
    NavigationMenuView navMenuView = (NavigationMenuView) navigationView.getChildAt(0);
    navMenuView.addItemDecoration(new DividerItemDecoration(Profile.this, DividerItemDecoration.VERTICAL));
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawerLayout, support_toolbar, R.string.nav_open, R.string.nav_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();
    Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.btn_menu_white);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setCheckedItem(R.id.nav_profile);
    TextView view = (TextView) navigationView.getMenu().findItem(R.id.nav_profile).getActionView();
    view.setText(R.string.consumer);

    edit_profile_btn.setOnClickListener(v -> {
        Intent i = new Intent(Profile.this, EditProfile.class);
        startActivity(i);
    });

    disableNavigationViewScrolling(navigationView);

}

@Override
public void onBackPressed() {

    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }

}

private void disableNavigationViewScrolling(NavigationView navigationView) {
    if (navigationView != null) {
        NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
        if (navigationMenuView != null) {
            navigationMenuView.setNestedScrollingEnabled(false);
        }
    }
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    switch (item.getItemId()) {
        case R.id.nav_support:
            Intent intent_support = new Intent(this, Support.class);
            startActivity(intent_support);
            break;

        case R.id.nav_setting:
            Intent intent_setting = new Intent(this, Setting.class);
            startActivity(intent_setting);
            break;

        case R.id.nav_profile:
            Intent intent_profile = new Intent(this, Profile.class);
            startActivity(intent_profile);
            break;
        case R.id.nav_favourites:
            Intent intent_favourites = new Intent(this, Favourites.class);
            startActivity(intent_favourites);
            break;

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

} `

BuraqsysStudio commented 4 years ago

Here is the Footer layout:

`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="bottom" android:layout_gravity="bottom" android:orientation="vertical" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:background="@color/dark">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    android:orientation="horizontal"
    android:baselineAligned="false">
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu_setting"
        android:paddingBottom="2dp"
        android:layout_gravity="center_horizontal"/>
    <com.google.android.material.textview.MaterialTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:text="@string/settings"/>
    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_support"
            android:paddingBottom="2dp"
            android:layout_gravity="center_horizontal"/>
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textAlignment="center"
            android:textColor="@color/white"

            android:text="@string/support"/>
    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_logout"
            android:paddingBottom="2dp"
            android:layout_gravity="center_horizontal"/>
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:layout_gravity="center_horizontal"
            android:textAlignment="center"
            android:text="@string/logout"/>
    </LinearLayout>
</LinearLayout>

`

MarcDahlem commented 4 years ago

Mhm, it looks quite good. I have to say, that my code is quite old and I have never updated it to androidx.

The only difference I could find is in the footer;

I used android:gravity="bottom", whereas your footer has android:baselineAligned="false". Maybe the problem is also the weight calculation in the footer. Have you tried with my footer layout instead of yours? Is that working?

BuraqsysStudio commented 4 years ago

I have tried with your footer it still shows up at top I don't know, even though in Preview its at the bottom

BuraqsysStudio commented 4 years ago

May be I should post it on Stack Overflow, what did you say? Because I don't see anything wrong in the code.