From 928f23945f3c46ff948ca5fd6a4fa3c51a2bdf20 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 20 Feb 2016 14:46:10 +0100 Subject: [PATCH] Don't inflate ad view, use it directly in layouts --- .../solovyev/android/calculator/AdView.java | 37 +++++++++++++------ .../solovyev/android/calculator/ads/AdUi.java | 2 +- .../preferences/PreferencesFragment.java | 13 ++----- app/src/main/res/layout/ad.xml | 31 ---------------- app/src/main/res/layout/admob.xml | 34 ----------------- app/src/main/res/layout/cpp_plot_fragment.xml | 3 +- .../cpp_plot_function_settings_fragment.xml | 2 +- .../layout/cpp_plot_functions_fragment.xml | 2 +- .../res/layout/cpp_plot_range_fragment.xml | 2 +- .../main/res/layout/cpp_plotter_fragment.xml | 2 +- app/src/main/res/layout/fragment_entities.xml | 2 +- app/src/main/res/layout/fragment_history.xml | 2 +- .../main/res/layout/matrix_edit_fragment.xml | 2 +- app/src/main/res/values/ids.xml | 1 + app/src/main/res/values/styles.xml | 7 ++++ 15 files changed, 46 insertions(+), 96 deletions(-) delete mode 100644 app/src/main/res/layout/ad.xml delete mode 100644 app/src/main/res/layout/admob.xml diff --git a/app/src/main/java/org/solovyev/android/calculator/AdView.java b/app/src/main/java/org/solovyev/android/calculator/AdView.java index 82d6a893..7d769cb1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AdView.java +++ b/app/src/main/java/org/solovyev/android/calculator/AdView.java @@ -2,18 +2,19 @@ package org.solovyev.android.calculator; import android.content.Context; import android.util.AttributeSet; -import android.view.LayoutInflater; +import android.view.Gravity; import android.view.View; import android.widget.FrameLayout; - import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; - -import org.solovyev.android.Check; +import com.google.android.gms.ads.AdSize; import javax.annotation.Nonnull; 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 { @Nullable @@ -23,14 +24,22 @@ public class AdView extends FrameLayout { public AdView(Context context) { super(context); + init(); } public AdView(Context context, AttributeSet attrs) { super(context, attrs); + init(); } public AdView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); + init(); + } + + private void init() { + setVisibility(GONE); + setId(R.id.cpp_ad); } public void destroy() { @@ -66,13 +75,7 @@ public class AdView extends FrameLayout { return; } - LayoutInflater.from(getContext()).inflate(R.layout.admob, this); - admobView = (com.google.android.gms.ads.AdView) findViewById(R.id.admob); - Check.isNotNull(admobView); - if (admobView == null) { - return; - } - + admobView = addAdmobView(); admobListener = new AdView.AdViewListener(this); admobView.setAdListener(admobListener); @@ -85,6 +88,18 @@ public class AdView extends FrameLayout { 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() { if (admobView == null) { return; diff --git a/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java b/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java index c364cf24..57902b21 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java @@ -24,7 +24,7 @@ public class AdUi { @NonNull private final Handler handler; @Nullable - @Bind(R.id.ad) + @Bind(R.id.cpp_ad) AdView adView; @Nullable private Boolean adFree = null; diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java index ede0ab26..45ea65d3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java @@ -7,15 +7,9 @@ import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.util.SparseArray; -import android.view.LayoutInflater; import android.view.View; import android.widget.ListView; - -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.*; import org.solovyev.android.calculator.language.Language; import org.solovyev.android.calculator.language.Languages; 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.RequestListener; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.List; import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW; import static org.solovyev.android.wizard.WizardUi.startWizard; @@ -212,7 +205,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc if (adView != null) { return; } - adView = (AdView) LayoutInflater.from(getActivity()).inflate(R.layout.ad, null); + adView = new AdView(getActivity()); adView.show(); try { listView.addHeaderView(adView); diff --git a/app/src/main/res/layout/ad.xml b/app/src/main/res/layout/ad.xml deleted file mode 100644 index 0a046d96..00000000 --- a/app/src/main/res/layout/ad.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - diff --git a/app/src/main/res/layout/admob.xml b/app/src/main/res/layout/admob.xml deleted file mode 100644 index 0dd0235e..00000000 --- a/app/src/main/res/layout/admob.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - diff --git a/app/src/main/res/layout/cpp_plot_fragment.xml b/app/src/main/res/layout/cpp_plot_fragment.xml index db473f00..acc73f65 100644 --- a/app/src/main/res/layout/cpp_plot_fragment.xml +++ b/app/src/main/res/layout/cpp_plot_fragment.xml @@ -29,7 +29,6 @@ a:layout_width="match_parent" a:layout_height="match_parent"> - - + \ No newline at end of file diff --git a/app/src/main/res/layout/cpp_plot_function_settings_fragment.xml b/app/src/main/res/layout/cpp_plot_function_settings_fragment.xml index 3d4931ee..50a524a5 100644 --- a/app/src/main/res/layout/cpp_plot_function_settings_fragment.xml +++ b/app/src/main/res/layout/cpp_plot_function_settings_fragment.xml @@ -31,7 +31,7 @@ a:layout_gravity="center_horizontal" a:orientation="vertical"> - + - + - + - + - + - + - + + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index a7e6c91e..1f37e30b 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -349,6 +349,13 @@ @color/cpp_material_actionbar_light + + 16sp 14sp \ No newline at end of file