0

I have two activities in my app and I don't wanna show Admob in main activity, I tried adding only in second activity but it doesn't show up.

Even if I add in main activity, it shows only in main activity not in second activity.

But my intention is only to show in second activity.

How can I achieve it?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:ads="http://schemas.android.com/apk/res-auto">
    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchView"
        android:background="#595959"
        android:queryHint="Search..."
        android:theme="@style/Theme.AppCompat.Light">
    </SearchView>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@drawable/gradient_horizontal_line"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@drawable/list_selector"/>
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</LinearLayout>

Second Activity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlist);

        sv = (SearchView)findViewById(R.id.searchView);
        int id = sv.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) sv.findViewById(id);
        textView.setTextColor(Color.WHITE);
        textView.setHintTextColor(Color.LTGRAY);

        plm = new SongsManager();

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
}
Selva
  • 1,310
  • 2
  • 14
  • 31
  • There are no restrictions to add an AdView in a second activity... Share your code... – guipivoto Mar 01 '16 at 18:39
  • probably ad does not fit in layout if it is a banner which you didn't mention. I suggest give a wide area to ad banner to test after ad shows up you can resize it properly. – Burak Karasoy Mar 01 '16 at 18:42
  • @giannisf ,@Guilherme P,@Burak Karasoy, I have updated with code, please check. – Selva Mar 01 '16 at 19:01
  • @Selva add `addTestDevice` to your `AdRequest` – giannisf Mar 01 '16 at 19:15
  • @giannisf, yeah I'll add that,I did a mistake in design, don't know how to correct it and show admob , can you please correct it? – Selva Mar 01 '16 at 19:19

3 Answers3

0

There some issues with your layout.

Your ListView is taking all the space and not allowing for AdView to show. change it's height to wrap_content.

<ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@drawable/gradient_horizontal_line"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@drawable/list_selector"/>

Those attributes

android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"

are for a RelativeLayout parent, remove them. Instead add

android:layout_gravity='center|bottom'

and the fill_parent is deprecated, you should instead use match_parent.

giannisf
  • 2,479
  • 1
  • 17
  • 29
  • This doesn't work, I checked and found answer and posted below, thanks a lot for your time and immediate reply . – Selva Mar 01 '16 at 19:32
0

listview has a layout_weight set to 1. That will keep the button fixed in its place at the bottom.

 <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/gradient_horizontal_line"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="@drawable/list_selector"
    android:layout_weight="1"/>


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

Fixed Button below a scrollable ListView

Community
  • 1
  • 1
Selva
  • 1,310
  • 2
  • 14
  • 31
0

change LinearLayout to RelativeLayout

gonga dev
  • 117
  • 3
  • 13