Don't inflate ad view, use it directly in layouts

This commit is contained in:
serso 2016-02-20 14:46:10 +01:00
parent 8b87de2f29
commit 928f23945f
15 changed files with 46 additions and 96 deletions

View File

@ -2,18 +2,19 @@ package org.solovyev.android.calculator;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.LayoutInflater; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import org.solovyev.android.Check;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
public class AdView extends FrameLayout { public class AdView extends FrameLayout {
@Nullable @Nullable
@ -23,14 +24,22 @@ public class AdView extends FrameLayout {
public AdView(Context context) { public AdView(Context context) {
super(context); super(context);
init();
} }
public AdView(Context context, AttributeSet attrs) { public AdView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
init();
} }
public AdView(Context context, AttributeSet attrs, int defStyle) { public AdView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
init();
}
private void init() {
setVisibility(GONE);
setId(R.id.cpp_ad);
} }
public void destroy() { public void destroy() {
@ -66,13 +75,7 @@ public class AdView extends FrameLayout {
return; return;
} }
LayoutInflater.from(getContext()).inflate(R.layout.admob, this); admobView = addAdmobView();
admobView = (com.google.android.gms.ads.AdView) findViewById(R.id.admob);
Check.isNotNull(admobView);
if (admobView == null) {
return;
}
admobListener = new AdView.AdViewListener(this); admobListener = new AdView.AdViewListener(this);
admobView.setAdListener(admobListener); admobView.setAdListener(admobListener);
@ -85,6 +88,18 @@ public class AdView extends FrameLayout {
admobView.loadAd(b.build()); admobView.loadAd(b.build());
} }
@Nonnull
private com.google.android.gms.ads.AdView addAdmobView() {
final com.google.android.gms.ads.AdView v = new com.google.android.gms.ads.AdView(getContext());
v.setVisibility(GONE);
v.setAdSize(AdSize.BANNER);
v.setAdUnitId(getResources().getString(R.string.admob));
final LayoutParams lp = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
addView(v, lp);
return v;
}
public void hide() { public void hide() {
if (admobView == null) { if (admobView == null) {
return; return;

View File

@ -24,7 +24,7 @@ public class AdUi {
@NonNull @NonNull
private final Handler handler; private final Handler handler;
@Nullable @Nullable
@Bind(R.id.ad) @Bind(R.id.cpp_ad)
AdView adView; AdView adView;
@Nullable @Nullable
private Boolean adFree = null; private Boolean adFree = null;

View File

@ -7,15 +7,9 @@ import android.os.Bundle;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.ListView; import android.widget.ListView;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.AdView;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.language.Language; import org.solovyev.android.calculator.language.Language;
import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.checkout.BillingRequests; import org.solovyev.android.checkout.BillingRequests;
@ -23,10 +17,9 @@ import org.solovyev.android.checkout.Checkout;
import org.solovyev.android.checkout.ProductTypes; import org.solovyev.android.checkout.ProductTypes;
import org.solovyev.android.checkout.RequestListener; import org.solovyev.android.checkout.RequestListener;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List;
import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW; import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW;
import static org.solovyev.android.wizard.WizardUi.startWizard; import static org.solovyev.android.wizard.WizardUi.startWizard;
@ -212,7 +205,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
if (adView != null) { if (adView != null) {
return; return;
} }
adView = (AdView) LayoutInflater.from(getActivity()).inflate(R.layout.ad, null); adView = new AdView(getActivity());
adView.show(); adView.show();
try { try {
listView.addHeaderView(adView); listView.addHeaderView(adView);

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2013 serso aka se.solovyev
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Contact details
~
~ Email: se.solovyev@gmail.com
~ Site: http://se.solovyev.org
-->
<org.solovyev.android.calculator.AdView
xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/ad"
a:layout_width="match_parent"
a:layout_height="wrap_content"
a:layout_gravity="center"
a:visibility="gone" />

View File

@ -1,34 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2013 serso aka se.solovyev
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Contact details
~
~ Email: se.solovyev@gmail.com
~ Site: http://se.solovyev.org
-->
<com.google.android.gms.ads.AdView
xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
a:id="@+id/admob"
a:layout_width="match_parent"
a:layout_height="wrap_content"
a:layout_gravity="center"
a:visibility="gone"
ads:adSize="BANNER"
ads:adUnitId="@string/admob" />

View File

@ -29,7 +29,6 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent"> a:layout_height="match_parent">
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
</LinearLayout> </LinearLayout>

View File

@ -31,7 +31,7 @@
a:layout_gravity="center_horizontal" a:layout_gravity="center_horizontal"
a:orientation="vertical"> a:orientation="vertical">
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
<TextView <TextView
a:layout_width="match_parent" a:layout_width="match_parent"

View File

@ -29,7 +29,7 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent"> a:layout_height="match_parent">
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
<LinearLayout <LinearLayout
a:layout_width="match_parent" a:layout_width="match_parent"

View File

@ -31,7 +31,7 @@
a:layout_gravity="center_horizontal" a:layout_gravity="center_horizontal"
a:orientation="vertical"> a:orientation="vertical">
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
<LinearLayout <LinearLayout
a:layout_width="match_parent" a:layout_width="match_parent"

View File

@ -29,7 +29,7 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent"> a:layout_height="match_parent">
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
<org.solovyev.android.plotter.PlotView <org.solovyev.android.plotter.PlotView
a:id="@+id/plotview" a:id="@+id/plotview"

View File

@ -29,7 +29,7 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent"> a:layout_height="match_parent">
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
<FrameLayout <FrameLayout
a:layout_width="match_parent" a:layout_width="match_parent"

View File

@ -29,7 +29,7 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent"> a:layout_height="match_parent">
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
<FrameLayout <FrameLayout
a:layout_width="match_parent" a:layout_width="match_parent"

View File

@ -37,7 +37,7 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content" /> a:layout_height="wrap_content" />
<include layout="@layout/ad" /> <org.solovyev.android.calculator.AdView style="@style/CppAd" />
<LinearLayout <LinearLayout
a:layout_width="match_parent" a:layout_width="match_parent"

View File

@ -21,6 +21,7 @@
--> -->
<resources> <resources>
<item name="cpp_ad" type="id" />
<item name="cpp_button_1" type="id" /> <item name="cpp_button_1" type="id" />
<item name="cpp_button_2" type="id" /> <item name="cpp_button_2" type="id" />
<item name="cpp_button_3" type="id" /> <item name="cpp_button_3" type="id" />

View File

@ -349,6 +349,13 @@
<item name="backgroundSplit">@color/cpp_material_actionbar_light</item> <item name="backgroundSplit">@color/cpp_material_actionbar_light</item>
</style> </style>
<style name="CppAd">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center</item>
<item name="android:visibility">gone</item>
</style>
<dimen name="list_item_text_size">16sp</dimen> <dimen name="list_item_text_size">16sp</dimen>
<dimen name="list_item_text_size_small">14sp</dimen> <dimen name="list_item_text_size_small">14sp</dimen>
</resources> </resources>