Search code examples
androidandroid-layoutandroid-viewpagerandroid-toolbar

Add View Below Toolbar in android


I am adding a view in ViewPager programmatically But when I do so my layout does not starts below actionbar. It overlaps with actionbar.

Here is screenshot of app.

enter image description here

In my Pager adapter I have wrote this code.

ProductPagerAdapter.java

@Override
    public Object instantiateItem(ViewGroup container, int position) {

        rate = list.get(position);

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.fragment_product, container,false);

        tvProdName = (TextView) v.findViewById(R.id.tvProdName);
        tvProdAmount = (TextView) v.findViewById(R.id.tvProdAmount);
        tvProdDesc = (TextView) v.findViewById(R.id.tvProdDesc);
        ivProdImage = (ImageView) v.findViewById(R.id.ivProdImage);

        tvProdName.setText(rate.Name);
        tvProdAmount.setText(rate.amount);
        tvProdDesc.setText(rate.longDescription);
        Picasso.with(context).load(rate.image).into(ivProdImage);

        ((ViewPager) container).addView(v);

        return v;
    }

fragment_product.xml

<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

activity xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.ashishkudale.linksolutions.DetailsActivity">

    <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.design.widget.AppBarLayout>

    <!--<include layout="@layout/content_details"/>-->

    <android.support.v4.view.ViewPager
        android:id="@+id/productPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

Can some tell me how to start layout below action bar programatically.


Solution

  • As you are using CoordinatorLayout it places views above each other like a FrameLayout with the most recently added child on top. To fix your issue you have to add android:layout_marginTop="?attr/actionBarSize" to your ViewPager:

    <android.support.v4.view.ViewPager
        android:id="@+id/productPager"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />