4EverNoob
Lurker
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
MainFragment.java
AdMobFragment.java
main_activity.xml
fragment_ad_mob.xml
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:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (savedInstanceState == null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.main_fragment, MainFragment.newInstance()).commitNow();
fm.beginTransaction().replace(R.id.ad_fragment, AdMobFragment.newInstance()).commitNow();
}
}
}
MainFragment.java
Java:
public class MainFragment extends Fragment {
private MainViewModel mViewModel;
public static MainFragment newInstance() {
return new MainFragment();
}
/* @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, container, false);
}
*/
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
// TODO: Use the ViewModel
}
}
AdMobFragment.java
Java:
public class AdMobFragment extends Fragment {
private FragmentAdMobBinding binding;
public static AdMobFragment newInstance() {
return new AdMobFragment();
}
//Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("534153A818F863B61501C7071FB0D8BA"))
private void setContainerHeight(FragmentActivity view, int id, int dp) {
ConstraintLayout layout = view.findViewById(id);
ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
layoutParams.height = dp;
layout.setLayoutParams(layoutParams);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
binding = FragmentAdMobBinding.inflate(inflater, container, false);
FragmentActivity activity = getActivity();
MobileAds.initialize(activity, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {}
});
//AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
AdView adView = activity.findViewById(R.id.ad_view);
AdSize adSize;
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, (int) (displayMetrics.widthPixels/displayMetrics.density));
setContainerHeight(activity, R.id.ad_fragment, adSize.getHeight());
// setContainerHeight(activity, R.id.main_fragment, -adSize.getHeight());
adView.setAdSize(adSize);
//adView.loadAd(adRequest);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
AdView adView = binding.adView;
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
main_activity.xml
HTML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_fragment"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FF00FF00"
tools:context=".ui.main.MainFragment">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFFF0000"
android:id="@+id/ad_fragment"
tools:context=".ui.main.AdMobFragment">
<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
fragment_ad_mob.xml
HTML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ad_fragment"
tools:context=".ui.main.AdMobFragment">
<!--Change to ButtonImage with default ad-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="why?"
app:layout_constraintBottom_toTopOf="@id/ad_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>