0

I just started with android development and run into a problem i cannot find any information for. I created a project using the two-pane template. I was able to add a AdView and many other views to my app while in two-pane mode (activity_setting_twopane.xml). Where i struggle is on how to add a view (any) to the other layout for smaller displays (activity_setting_detail.xml, activity_setting_list.xml or fragment_setting_detail.xml?). I was able to add a view (an AdView) to fragment_setting_detail.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/setting_list"
    android:name="com.test.firstapp.SettingListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:context=".SettingListActivity" 
    tools:layout="@android:layout/list_content" >

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

</fragment>

I think this is not the correct way but the only one that works right now. But the AdView is displayed at the top of the screen and i have no idea on how to position it because in Fragments most parameters arent allowed to use. I even tried to wrap it in a LinearLayout but then the sample project crashes where i want to add the view to the layout in code. What is the correct way to add any views to xml based Fragments (using the sample so i understand)? Many thx in advance

Blo
  • 11,903
  • 5
  • 45
  • 99
Digit
  • 3
  • 4
  • Have you tried to set a gravity to your `AdView` as: `android:layout_gravity:bottom`? – Blo Dec 15 '13 at 10:47
  • yes i tried before by adding android:layout_gravity="bottom" but its not working too. It stays at the top. – Digit Dec 15 '13 at 10:55
  • Try to add a `layout_weight` with a `weightSum` to the parent. Or try the `RelativeLayout` way. See my update answer below. – Blo Dec 15 '13 at 11:00
  • I just tried again and now it is at the bottom using gravity. I can't believe why yesterday it didn't... :) Thanks! Problem solved! – Digit Dec 15 '13 at 11:01
  • You're welcome. :) (I'll update my answer to other people who search the same thing as you ask) – Blo Dec 15 '13 at 11:04

1 Answers1

1

The right way to do this:

<!-- assign a layout_gravity to your Views -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingListActivity" 
    tools:layout="@android:layout/list_content"
    android:orientation="vertical" >

    <fragment 
         android:id="@+id/setting_list"
         android:name="com.test.firstapp.SettingListFragment"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="16dp"
         android:layout_marginRight="16dp" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="480dp"
        android:layout_height="75dp"
        ads:adSize="BANNER"
        ads:adUnitId="xxxxxxxxxxxxxxxxx"
        android:layout_gravity="bottom" />

</LinearLayout>  

Note:
Don't confuse android:gravity and android:layout_gravity.

android:gravity > assign a gravity to the "children".
android:layout_gravity > assign a gravity to the "parent".
See this answer for more details: Android - gravity and layout_gravity

Edit:
FYI: a ViewGroup is:

"A special view that can contain other views (called children.) The view group is the base class for layouts and views containers." ref: ViewGroup Android Developer Documentation

Your fragment cannot be a parent for AdView, you need a "parent view" as LinearLayout, RelativeLayout, AbsoluteLayout (deprecated), TableLayout, etc.

And your AdView failed because you must to assign it a width/height value as you can see here: AdView failed to instantiate. Also to read: com.google.ads.AdView failed to instantiate

Hope this helps.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • Thanks that works. Maybe i really used the other gravity parameter :) – Digit Dec 15 '13 at 11:11
  • I think `layout_gravity` in multi-pane also works. It's exactly the same above. (You're welcome) – Blo Dec 15 '13 at 11:19
  • In multi-pane i was able to use a relative layout so i don't have any problems. I just struggle to add a layout to fragment_setting_detail.xml because then the code crashes when calling MyadView.loadAd(adRequest); Btw: The above code raises two errors in Eclipse ADT when switching to the layout designer "activity_setting_list.xml: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup" and "com.google.android.gms.ads.AdView failed to instantiate." But it works – Digit Dec 15 '13 at 11:35
  • I updated my answer (instead of `LinearLayout`, feel free to put what you want: relative or others). Hope this will be helpful. – Blo Dec 15 '13 at 12:02
  • I checked my history and saw that yesterday i used the correct gravity so something was wrong with my system. Because of this i tried a relative layout on my fragment above again and guess what happens? It is working now too, no crashes anymore. I really dont know what was wrong yesterday, i even started a new Emulator wqiped everything, cleaned my project etc..... Now all my problems gone. Anyway the stuff you posted helped me a lot and i learned something new. Thanks! – Digit Dec 15 '13 at 12:06