Shabbir Dhangot
Shabbir Dhangot

Reputation: 9141

hiding google ad changes Layout of application

I am Facing problem while i give google ad view.GONE while no internet connection. Here is the image that is perfect when internet connection is active . enter image description here

But when no internet connection previous next button go to top I think becaouse of view.GONE it losses reference.

enter image description here

What to do. I tried View.INVISIBLE but it occupy empty space in bottom.

I want previous next button to bottom when no internet connection.

My layout file.

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/txtAd"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#000"
        android:gravity="center"
        android:text="Advertisement"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/RelativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/navigation" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/share" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/save" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Birthday Cards"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textSize="18sp" />
    </RelativeLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/ad_unit_id" >
    </com.google.android.gms.ads.AdView>

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

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

            <Button
                android:id="@+id/button4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/ic_action_previous_item" />

            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="set as Wallpaper" />

            <Button
                android:id="@+id/button3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/ic_action_next_item" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

Help me to solve problem.

Upvotes: 0

Views: 61

Answers (3)

reVerse
reVerse

Reputation: 35264

Adding android:layout_alignWithParentIfMissing="true" to your LinearLayout with the id ll1 should work.

Result:

<!-- Other views -->
<LinearLayout
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/adView"
    android:layout_alignParentLeft="true"
    android:layout_alignWithParentIfMissing="true"
    android:orientation="vertical" >

<!-- Child views -->
</LinearLayout>

So what does alignWithParentIfMissing do? Let's quote the docs:

If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.

Upvotes: 2

You are using RelativeLayout, and some of your elements (LinearLayout with id = "@+id/ll1" for example) are placed relatively to your AdView ( android:layout_above="@+id/adView" ).

Try to break this relationship or use LinearLayout as root layout to solve your problem.

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

<RelativeLayout
        android:id="@+id/RelativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/navigation" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/share" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/save" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Birthday Cards"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textSize="18sp" />
    </RelativeLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:weight="1"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

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

            <Button
                android:id="@+id/button4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/ic_action_previous_item" />

            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="set as Wallpaper" />

            <Button
                android:id="@+id/button3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/ic_action_next_item" />
        </LinearLayout>
    </LinearLayout>
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/ad_unit_id" >
    </com.google.android.gms.ads.AdView>

</LinearLayout >

Upvotes: 1

Michael Krause
Michael Krause

Reputation: 4869

Use View.GONE instead. This will ensure it gets ignored when things get laid out.

As an aside, android:orientation is not a valid RelativeLayout attribute and will be ignored.

Upvotes: 0

Related Questions