3

I have an app that is ad supported. As a result when the screen is rotated the ad persists. I would like to be able to make the ad only exist when the phone is in portrait and not in landscape. Do I need to declare a separate landscape xml or is there a different way to do this? If I need to declare a landscape xml how do I handle that in java?
Here is the relevant xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:screenOrientation="portrait"
    >
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=" "
        android:id="@+id/textView"
        android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/pic"
                android:layout_weight="1"
        >
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"/>


</RelativeLayout>
<com.google.ads.AdView android:id="@+id/adView"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       ads:adUnitId="4"
                       ads:adSize="BANNER"

                       android:layout_centerHorizontal="true"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/pic"
        >
       //a bunch of buttons
</LinearLayout>

Thanks!

Update Here is the logcat for when the app crashes after implementing layout-land folder and removing the element I don't want, aka the adview .txt

Sam Borick
  • 935
  • 2
  • 10
  • 31

2 Answers2

2

you need to add a folder layout-land to the res folder and have an xml file with the same name with the layout set as you wish to be shown on landscape orientatation.

that new layout will be displayed when orientation changes to landscape

check out the documentation on Supporting Different Screens:

http://developer.android.com/training/basics/supporting-devices/screens.html

another option that might be relevant is monitoring orientation changes and perform specific task on orientation change by overriding onConfigurationChanged. there are many post on SO regarding that

for example:

Why not use always android:configChanges="keyboardHidden|orientation"?

How to detect orientation change in layout in Android?

hope this helps...

Community
  • 1
  • 1
Guy S
  • 1,424
  • 1
  • 15
  • 34
  • so I implemented this by copying the folder and renaming it, and then removing the element I don't want. Now it crashes when there are any changes, but runs fine when there are no changes. any ideas? – Sam Borick May 18 '14 at 00:29
  • could you post logcat of crash. what do you mean by " removing the element" – Guy S May 18 '14 at 06:07
0

You can provide different xml layout files for landscape and portrait orientations, and you can handle the rotation in your code, by using the following method :

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
{

    // You can set the "visibilty=gone" , on the view that is showing the ad-
    //which will result in not displaying that view
} 
else
{
    //set visibilty=visible on the view component
}
}
Farhad
  • 12,178
  • 5
  • 32
  • 60