Difficult Beginnings (Layout, Fragments, AdMob) – Android Development

The basic idea is very simple. I read that a banner ad must display for some time period for it to “count.” (Many years ago.) So my thinking was to create essentially the flow: Splash Screen -> App Activity. The app activity would be composed of the content area and the ad area. The first fragment would be the login fragment. If already logged in, proceed to the app ui fragment.

I’m having a hard time with simple things. I did make some progress while writing this and going back and forth between my project. I have been at this for a couple of days. I had a lot of questions but it can be boiled down to my biggest problem: I don’t understand the complete relationship between the XML layouts and code in OnCreate / OnViewCreate, etc.

I used the Fragment+ViewModel wizard in Android Studio and then added an AdMobFragment. I then left the original XML files and then modified the main_activity.xml to include both fragments. I then seemed to have some sort of combination of the attributes from both.

I can go on regarding the different iterations but I’m just trying to get the content area to take up the screen remaining after setting the ad area, and load an ad. I was successful in the latter…. only using the separate XML file, not the monolithic one. I also seem to have a problem if I remove the android:adSize attribute… yet for adaptive banners it says “n/a” and I attempt to set it programmatically.

How do I use a monolithic XML layout? (just the main_activity.xml)

Setting main area to take remainder after ad? match_parent overwrites the entire area… can explicitly set but that’s good for only my device.

I assume I must be doing another task incorrectly since it appears the ads are sometimes cut off. The adaptive banner page on the developer resources seems to set it once and forget it…. what did I do here?

I appreciate any help. Thank you.

MainActivity.java

Code (Text):

  1. public class MainActivity extends AppCompatActivity {

  2. @Override

  3. protected void onCreate(Bundle savedInstanceState) {

  4. super.onCreate(savedInstanceState);

  5. setContentView(R.layout.main_activity);

  6. if (savedInstanceState == null) {

  7. FragmentManager fm = getSupportFragmentManager();

  8. fm.beginTransaction().replace(R.id.main_fragment, MainFragment.newInstance()).commitNow();

  9. fm.beginTransaction().replace(R.id.ad_fragment, AdMobFragment.newInstance()).commitNow();

  10. }

  11. }

  12. }

MainFragment.java

Code (Java):

  1. public class MainFragment extends Fragment {

  2. private MainViewModel mViewModel;

  3. public static MainFragment newInstance() {

  4. return new MainFragment();

  5. }

  6. /* @Nullable

  7. @Override

  8. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,

  9. @Nullable Bundle savedInstanceState) {

  10. return inflater.inflate(R.layout.main_fragment, container, false);

  11. }

  12. */

  13. @Override

  14. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  15. super.onViewCreated(view, savedInstanceState);

  16. mViewModel = new ViewModelProvider(this).get(MainViewModel.class);

  17. // TODO: Use the ViewModel

  18. }

  19. }

AdMobFragment.java

Code (Java):

  1. public class AdMobFragment extends Fragment {

  2. private FragmentAdMobBinding binding;

  3. public static AdMobFragment newInstance() {

  4. return new AdMobFragment();

  5. }

  6. //Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList(“534153A818F863B61501C7071FB0D8BA”))

  7. private void setContainerHeight(FragmentActivity view, int id, int dp) {

  8. ConstraintLayout layout = view.findViewById(id);

  9. ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();

  10. layoutParams.height = dp;

  11. layout.setLayoutParams(layoutParams);

  12. }

  13. @Neighbours

  14. @Override

  15. public View onCreateView(@NonNull LayoutInflater inflater,
  16. @Nullable ViewGroup container,

  17. @Nullable Bundle savedInstanceState) {

  18. binding = FragmentAdMobBinding.inflat(inflater container, false);

  19. FragmentActivity activity = getActivity();

  20. MobileAds.initialize(activity, new OnInitializationCompleteListener() {

  21. @Override

  22. public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {}

  23. });

  24. //AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();

  25. AdView = activity.findViewById(R.id.ad_view);

  26. AdSize adSize;

  27. DisplayMetrics displayMetrics = new DisplayMetrics();

  28. getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

  29. adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, (int) (displayMetrics.widthPixels/displayMetrics.density));

  30. setContainerHeight(activity, R.id.ad_fragmentadSize.getHeight());

  31. // setContainerHeight(activity, R.id.main_fragment, -adSize.getHeight());

  32. adView.setAdSize(adSize);

  33. //adView.loadAd(adRequest);

  34. return binding.getRoot();

  35. }

  36. @Override

  37. public void onViewCreated(@NorthGirl View view, @Nullable Bundle savedInstanceState) {
  38. super.onViewCreated(view, savedInstanceState);

  39. AdView = binding.adView;

  40. AdRequest adRequest = new AdRequest.Builder().build();

  41. adView.loadAd(adRequest);

  42. }

  43. @Override

  44. public void onDestroyView() {

  45. super.onDestroyView();

  46. binding = null;

  47. }

  48. }

main_activity.xml

HTML:

  1. version=“1.0” encoding=“utf-8”?>

  2. xmlns:android=“http://schemas.android.com/apk/res/android”

  3. xmlns:tools=http://schemas.android.com/tools

  4. android:id=“@+id/container”

  5. android:layout_width=“match_parent”

  6. android:layout_height=“match_parent”

  7. android:orientation=“vertical”

  8. tools:context=“.MainActivity” >

  9. xmlns:android=“http://schemas.android.com/apk/res/android”

  10. xmlns:app=http://schemas.android.com/apk/res-auto

  11. xmlns:tools=http://schemas.android.com/tools

  12. android:id=“@+id/main_fragment”

  13. android:layout_width=“match_parent”

  14. android:layout_height=“100dp”

  15. android:background=“#FF00FF00”

  16. tools:context=“.ui.main.MainFragment”>

  17. android:id=“@+id/message”

  18. android:layout_width=“wrap_content”

  19. android:layout_height=“wrap_content”

  20. android:text=“Main Fragment”

  21. app:layout_constraintBottom_toBottomOf=“parent”

  22. app:layout_constraintEnd_toEndOf=“parent”

  23. app: layout_constraintStart_toStartOf=“parent”

  24. app:layout_constraintTop_toTopOf=“parent” />

  25. </androidx.constraintlayout.widget.ConstraintLayout>

  26. xmlns:android=“http://schemas.android.com/apk/res/android”

  27. xmlns:app=http://schemas.android.com/apk/res-auto

  28. xmlns:tools=http://schemas.android.com/tools

  29. android:layout_width=“match_parent”

  30. android:layout_height=“0dp”

  31. android:background=“#FFFF0000”

  32. android:id=“@+id/ad_fragment”

  33. tools:context=“.ui.main.AdMobFragment”>

  34. android:id=“@+id/ad_view”

  35. android:layout_width=“wrap_content”

  36. android:layout_height=“wrap_content”

  37. android:layout_alignParentBottom=“true”

  38. android:layout_centerHorizontal=“true”

  39. app:adUnitId=“ca-app-pub-3940256099942544/6300978111”

  40. app:layout_constraintBottom_toBottomOf=“parent”

  41. app:layout_constraintEnd_toEndOf=“parent”

  42. app: layout_constraintStart_toStartOf=“parent” />

  43. </androidx.constraintlayout.widget.ConstraintLayout>

  44. </LinearLayout>

fragment_ad_mob.xml

HTML:

  1. version=“1.0” encoding=“utf-8”?>

  2. =“http://schemas.android.com/apk/res/android”

  3. xmlns:app=http://schemas.android.com/apk/res-auto

  4. xmlns:tools=http://schemas.android.com/tools

  5. android:layout_width=“match_parent”

  6. android:layout_height=“match_parent”

  7. android:id=“@+id/ad_fragment”

  8. tools:context=“.ui.main.AdMobFragment”>

  9. android:layout_width=“wrap_content”

  10. android:layout_height=“wrap_content”

  11. android:text=“why?”

  12. app: layout_constraintBottom_toTopOf=“@id/ad_view”

  13. app:layout_constraintEnd_toEndOf=“parent”

  14. app: layout_constraintStart_toStartOf=“parent”

  15. app:layout_constraintTop_toTopOf=“parent” />

  16. android:id=“@+id/ad_view”

  17. android:layout_width=“wrap_content”

  18. android:layout_height=“wrap_content”

  19. android:layout_alignParentBottom=“true”

  20. android:layout_centerHorizontal=“true”

  21. app:adSize=“BANNER”

  22. app:adUnitId=“ca-app-pub-3940256099942544/6300978111”

  23. app:layout_constraintBottom_toBottomOf=“parent”

  24. app:layout_constraintEnd_toEndOf=“parent”

  25. app: layout_constraintStart_toStartOf=“parent” />

  26. </androidx.constraintlayout.widget.ConstraintLayout>

Leave a Comment