Search code examples
javaandroidandroid-layoutandroid-constraintlayoutandroid-relativelayout

Constraint layout has space bottom and top


I'm building an app using Constraint layout, but it has extra space bottom and top. I can't find where they come from. I checked maybe ten times from top to bottom all code but there is no margin or padding or anything else for it.

enter image description here

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:paddingLeft="0dp"
    android:paddingTop="0dp"
    android:paddingRight="0dp"
    android:paddingBottom="0dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/Brand"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/brandname"
                style="@style/viewParent.headerText"
                android:text="Did"
                android:textColor="@color/white" />

            <TextView
                android:id="@+id/brandname2"
                style="@style/viewParent.headerText2"
                android:layout_toRightOf="@id/brandname"
                android:text="You"
                android:textColor="@color/yellow" />

            <TextView
                style="@style/viewParent.headerText2"
                android:layout_toRightOf="@id/brandname2"
                android:text="Know!"
                android:textColor="@color/red"
                android:textStyle="bold|italic" />
        </RelativeLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="4dp"
        android:background="@drawable/card_bg"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        app:layout_constraintBottom_toTopOf="@id/guideline2"
        app:layout_constraintEnd_toEndOf="@id/guidelineyazisag"
        app:layout_constraintStart_toStartOf="@id/guidelineyazisol"
        app:layout_constraintTop_toBottomOf="@id/guideline">

        <TextView
            android:id="@+id/activity_main_text_view_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="simple Title Text"
            android:textColor="#5E4C4C"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/activity_main_did_u_know"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Category"
            android:textColor="#5E4C4C"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:fillViewport="true"
            app:layout_constraintBottom_toTopOf="@id/activity_main_image_view"
            app:layout_constraintTop_toBottomOf="@id/activity_main_did_u_know">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@id/activity_main_image_view"
                app:layout_constraintTop_toBottomOf="@id/activity_main_did_u_know">

                <TextView
                    android:id="@+id/factTextView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:gravity="center_vertical|center_horizontal"
                    android:lineSpacingExtra="8dp"
                    android:padding="10dp"
                    android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
                    android:textColor="#000000"
                    android:textSize="16dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>

        <ImageView
            android:id="@+id/activity_main_image_view"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:clickable="true"
            android:src="@drawable/ic_share_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/scrollview" />

        <ImageView
            android:id="@+id/activity_main_fav_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_margin="16dp"
            android:clickable="true"
            android:src="@drawable/ic_favorite_border_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/scrollview" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/LeftRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline2">

        <ImageView
            android:id="@+id/activity_main_left_button"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:clickable="true"
            android:src="@drawable/ic_previous"
            android:elevation="3dp"
            android:background="@drawable/okarka"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/guideline3"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/activity_main_right_button"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:clickable="true"
            android:src="@drawable/ic_next"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            android:background="@drawable/okarka"
            app:layout_constraintStart_toEndOf="@+id/guideline3"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/LeftRight" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.076" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineyazisol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.07" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineyazisag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.93" />
</androidx.constraintlayout.widget.ConstraintLayout>

I tried adding guidelines to 0 points in the bottom but it also starts in that space. I gave -(minus) values to margin and padding but doesn't work. I don't know what else to do. You can see from screenshots how it looks.

public class MainActivity extends AppCompatActivity {

    private ColorWheel colorWheel = new ColorWheel();
    private TextView factTextView;
    private TextView textViewId;
    private TextView textViewTableName;
    ImageView tableImageView;
    AdView adView;
    ImageView shareImageView;
    ImageView favImageView;
    float x1,x2,y1,y2;
    int i = 1;
    int adCounter = 0;
    private ConstraintLayout constraintLayout;
    String fact;
    ArrayList<Fact> favList = new ArrayList<>();
    private InterstitialAd mInterstitial;
    AdRequest interAdRequest;
    int favId;
    String favTable;
    Typeface typeface;
    List<Fact> mData;
    private DatabaseFacts db;
    GradientDrawable shape;

    @SuppressLint({"ClickableViewAccessibility", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DatabaseFacts(this);
        db.copyDbIfNotExists();
        shape =  new GradientDrawable();
        shape.setCornerRadius(40);
        mData = new ArrayList<>();
        MobileAds.initialize(this, "ca-app-pub-2469721886989416~7390658870");
        adView = findViewById(R.id.adView);
        AdRequest bannerAdRequest = new AdRequest.Builder().build();
        adView.loadAd(bannerAdRequest);

        interAdRequest = new AdRequest.Builder().build();
        mInterstitial = new InterstitialAd(this);
        mInterstitial.setAdUnitId("ca-app-pub-2469721886989416/9441000894");
        mInterstitial.loadAd(interAdRequest);

        factTextView = findViewById(R.id.factTextView);
        constraintLayout = (ConstraintLayout) findViewById(R.id.container_main);
        textViewId = findViewById(R.id.activity_main_text_view_id);
        favImageView = findViewById(R.id.activity_main_fav_button);
        shareImageView = findViewById(R.id.activity_main_image_view);
        textViewTableName = findViewById(R.id.activity_main_did_u_know);
//        tableImageView = findViewById(R.id.img_user);
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//            typeface = getResources().getFont(R.font.sourceserifproregular);
//        }
        factTextView.setTypeface(typeface);
        Intent intent  = getIntent();
        final int choice = intent.getIntExtra("choice",0);

        switch (choice) {
            case 1:

                textViewTableName.setText("General Facts");
                factTextView.setText(db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).getFact());
                textViewId.setText("Fact " + (readFromShared("defaultKey") - 1) + " of " + db.getFactsCount(DatabaseFacts.TABLE_GENERAL_NAME));

                if (db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).isFavorite() == 0) {
                    favImageView.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                } else {
                    favImageView.setImageResource(R.drawable.ic_favorite_black_24dp);
                }

                ImageView leftClick = (ImageView) findViewById(R.id.activity_main_left_button);
                leftClick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        leftClickMethod("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME);

                    }
                });

                ImageView rightClick = (ImageView) findViewById(R.id.activity_main_right_button);
                rightClick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rightClickMethod("defaultKey",DatabaseFacts.TABLE_GENERAL_NAME);
                        return;
                    }
                });

                favImageView.setOnClickListener(v -> handleFavorites("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME));
                break;

Solution

  • Remove app:layout_constraintBottom_toTopOf="@id/guideline" from Brand and app:layout_constraintTop_toBottomOf="@id/LeftRight" from adView.
    You can't put constraints for Top and Bottom because it will center your View between those two constraints. Same about left and right (start, end), but you have match_parent here, so this problem doesn't occur (visualy - if you set wrap_content, view will go to the center too).