0

Here's my toolbar screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- Include your Toolbar -->
    <include
        android:id="@+id/mytoolbar"
        layout="@layout/toolbar" />
</LinearLayout>

<FrameLayout
    android:id="@+id/container_body"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <!-- Embed your fragment in a Layout View-->
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.Activities.MenuActivity"
        />
</FrameLayout>
 </LinearLayout>

How it looks on your phone:image You can see that the toolbar is too high. Manifest:

<activity
        android:name="com.example.Activities.MenuActivity"
        android:label="@string/title_activity_test"
        tools:ignore="InnerclassSeparator" />
    <activity

Styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

MenuActivity.class

setContentView(R.layout.activity_menu);
    Toolbar toolbar = findViewById(R.id.mytoolbar);
    setSupportActionBar(toolbar);
    Drawer(toolbar,MenuActivity.this,MenuActivity.this);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("Аппараты");
    }

void Drawer:

public void  Drawer(Toolbar toolbar, Context context, android.app.Activity activity){


    DatabaseReference rootRef2 = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef2 = rootRef2.child("users").child(getUid());
    ValueEventListener valueEventListener2 = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String email = dataSnapshot.child("account").getValue(String.class);
            String Avatar = dataSnapshot.child("Avatar").getValue(String.class);
            Long coinsAmount = dataSnapshot.child("coinsAmount").getValue(Long.class);

            String coinsView = "Баланс: " + coinsAmount;




                if (Avatar.equals("1")) {
                    avatar = (R.drawable.a1);
                }
                if (Avatar.equals("2")) {
                    avatar = (R.drawable.a2);
                }


            IProfile profile = new ProfileDrawerItem()
                    .withName(email)
                    .withEmail(coinsView)
                    .withIcon(avatar);

            AccountHeader headerResult = new AccountHeaderBuilder()
                    .withActivity(activity)
                    .withHeaderBackground(R.drawable.account_header_background)
                    .addProfiles(
                            profile
                    )
                    .build();


               result = new DrawerBuilder()
                    .withActivity(activity)
                    .withToolbar(toolbar)
                    .withAccountHeader(headerResult)
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Устройства")
                                    .withIcon(R.drawable.device)
                                    .withIdentifier(1)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Сканер")
                                    .withIcon(R.drawable.scaner)
                                    .withIdentifier(2)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Магазины")
                                    .withIcon(R.drawable.shop)
                                    .withIdentifier(3)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Купоны")
                                    .withIcon(R.drawable.coupons)
                                    .withIdentifier(4)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Архив Купонов")
                                    .withIcon(R.drawable.oldcoupons)
                                    .withIdentifier(5)
                    )
                    .withOnDrawerItemClickListener((view, i, iDrawerItem) -> {
                        if(iDrawerItem.getIdentifier() == 1 && !Activity.equals("Menu")){
                            Intent intent = new Intent(context,MenuActivity.class);
                            startActivity(intent);
                        }
                        else if(iDrawerItem.getIdentifier() == 2 && !Activity.equals("Scaner")){
                            Intent intent = new Intent(context,DecoderActivity.class);
                            startActivity(intent);
                        }
                        else if(iDrawerItem.getIdentifier() == 3 && !Activity.equals("Shop")){
                            Intent intent = new Intent(context,ShopActivity.class);
                            startActivity(intent);
                        }
                        else if(iDrawerItem.getIdentifier() == 4 && !Activity.equals("oldCoupons")){
                            Intent intent = new Intent(context,oldCouponsActivity.class);
                            startActivity(intent);
                        }

                        return false;
                    })
                    .build();

            Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(false);
            Objects.requireNonNull(result.getActionBarDrawerToggle()).setDrawerIndicatorEnabled(true);

        }

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

        }

    };
    uidRef2.addValueEventListener(valueEventListener2);




}

How to fix it and how do we all? I have long tried to fix it I used android:fitsSystemWindows="true" and I still did not help activity is cut only the first time if you restart the activity is all OK. I came to the conclusion that this may be due to firebase but how to solve this and eventually fix this problem I could not

Toolbar.xml

<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"


android:id="@+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="@dimen/materialize_toolbar"
android:background="@color/grey_500"
android:minHeight="?attr/actionBarSize"

android:theme="?attr/actionBarTheme" />
Edric
  • 24,639
  • 13
  • 81
  • 91

2 Answers2

1

Try setting android:fitsSystemWindows="true" to your base linear layout.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

Update - The answer that finally worked was to use CoordinatorLayout as the base layout.

P.S - To any person looking at this in the future, the other possible considerations were,

  • Setting it in the theme using <item name="android:fitsSystemWindows">true</item>
  • Setting it's value as false instead of true. It doesn't make logical sense, but I've seen people state that that works at times
  • Ensure that you have android:minHeight="?attr/actionBarSize" in your Toolbar
  • It might be worth completely replacing the container_toolbar LinearLayout with your Toolbar and set fitsSystemWindows as true.
  • Using a CoordinatorLayout or DrawerLayout your base layout. They're supposed to handle fitsSystemWindows differently from the other 'basic' layouts.
  • Consider the plethora of ways suggested here
  • And if all else fails, then you can override fitSystemWindows(Rect insets) to get your desired layout as the official docs suggests.
Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24
0

Try simplifying your layout. Put the toolbar and fragment inside the LinearLayout

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <include
        android:id="@+id/mytoolbar"
        layout="@layout/toolbar" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.Activities.MenuActivity"
        />

</LinearLayout>
SABANTO
  • 1,316
  • 9
  • 24