Skip to content

Commit

Permalink
Fix deprecated API warnings in the java/advanced/APIDemo project.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 633961498
  • Loading branch information
JillSong authored and copybara-github committed May 15, 2024
1 parent 2a5cd11 commit 8c6be90
Show file tree
Hide file tree
Showing 29 changed files with 415 additions and 601 deletions.
4 changes: 3 additions & 1 deletion java/advanced/APIDemo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.preference:preference:1.2.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.gms:play-services-ads:23.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}
1 change: 1 addition & 0 deletions java/advanced/APIDemo/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<activity
android:exported="true"
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;
import com.google.android.gms.ads.admanager.AppEventListener;

/**
* The {@link AdManagerAppEventsFragment} class demonstrates how to receive App Events from an Ad
* The {@link AdManagerAppEventsFragment} class demonstrates how to receive app events from an Ad
* Manager creative.
*/
public class AdManagerAppEventsFragment extends Fragment {
Expand All @@ -45,39 +47,39 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
adView = view.findViewById(R.id.appevents_av_main);

adView = getView().findViewById(R.id.appevents_av_main);
adView.setAppEventListener(
new AppEventListener() {
@Override
public void onAppEvent(@NonNull String name, @NonNull String data) {

adView.setAppEventListener(new AppEventListener() {
@Override
public void onAppEvent(String name, String data) {
// The Ad Manager ad this fragment loads contains JavaScript code that sends App
// Events to this host application. This AppEventListener receives those events,
// and sets the background of the fragment to match the value sent in the app event.
// The ad will send "red" when it loads, "blue" five seconds later, and "green"
// if the user taps the ad.

// The Ad Manager ad this fragment loads contains JavaScript code that sends App
// Events to the host application. This AppEventListener receives those events,
// and sets the background of the fragment to match the data that comes in.
// The ad will send "red" when it loads, "blue" five seconds later, and "green"
// if the user taps the ad.
// This is just a demonstration, of course. Your apps can do much more interesting
// things with app events.

// This is just a demonstration, of course. Your apps can do much more interesting
// things with App Events.

if (name.equals("color")) {
switch (data) {
case "blue":
rootView.setBackgroundColor(Color.rgb(0xD0, 0xD0, 0xFF));
break;
case "red":
rootView.setBackgroundColor(Color.rgb(0xFF, 0xD0, 0xD0));
break;
case "green":
rootView.setBackgroundColor(Color.rgb(0xD0, 0xFF, 0xD0));
break;
if (name.equals("color")) {
switch (data) {
case "blue":
rootView.setBackgroundColor(Color.rgb(0xD0, 0xD0, 0xFF));
break;
case "red":
rootView.setBackgroundColor(Color.rgb(0xFF, 0xD0, 0xD0));
break;
case "green":
rootView.setBackgroundColor(Color.rgb(0xD0, 0xFF, 0xD0));
break;
}
}
}
}
}
});
});

AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
adView.loadAd(adRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;
Expand All @@ -43,12 +45,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

noExclusionsAdView = getView().findViewById(R.id.no_exclusions_av);
dogsExcludedAdView = getView().findViewById(R.id.exclusions_av_dogsexcluded);
catsExcludedAdView = getView().findViewById(R.id.exclusions_av_catsexcluded);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
noExclusionsAdView = view.findViewById(R.id.no_exclusions_av);
dogsExcludedAdView = view.findViewById(R.id.exclusions_av_dogsexcluded);
catsExcludedAdView = view.findViewById(R.id.exclusions_av_catsexcluded);

AdManagerAdRequest noExclusionsRequest = new AdManagerAdRequest.Builder().build();
AdManagerAdRequest dogsExcludedRequest = new AdManagerAdRequest.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
Expand Down Expand Up @@ -68,22 +70,16 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

refresh = getView().findViewById(R.id.btn_refresh);
startVideoAdsMuted = getView().findViewById(R.id.cb_start_muted);
customControlsCheckbox = getView().findViewById(R.id.cb_custom_controls);
nativeAdsCheckbox = getView().findViewById(R.id.cb_native);
customFormatAdsCheckbox = getView().findViewById(R.id.cb_custom_format);
customControlsView = getView().findViewById(R.id.custom_controls);

refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View unusedView) {
refreshAd();
}
});
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
refresh = view.findViewById(R.id.btn_refresh);
startVideoAdsMuted = view.findViewById(R.id.cb_start_muted);
customControlsCheckbox = view.findViewById(R.id.cb_custom_controls);
nativeAdsCheckbox = view.findViewById(R.id.cb_native);
customFormatAdsCheckbox = view.findViewById(R.id.cb_custom_format);
customControlsView = view.findViewById(R.id.custom_controls);

refresh.setOnClickListener(unusedView -> refreshAd());

refreshAd();
}
Expand Down Expand Up @@ -173,33 +169,23 @@ private void populateSimpleTemplateAdView(final NativeCustomFormatAd nativeCusto
headline.setText(nativeCustomFormatAd.getText("Headline"));
caption.setText(nativeCustomFormatAd.getText("Caption"));

headline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View unusedView) {
nativeCustomFormatAd.performClick("Headline");
}
});
headline.setOnClickListener(unusedView -> nativeCustomFormatAd.performClick("Headline"));

FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder);

// Apps can check the MediaContent's hasVideoContent property to determine if the
// NativeCustomTemplateAd has a video asset.
if (nativeCustomFormatAd.getMediaContent() != null
&& nativeCustomFormatAd.getMediaContent().hasVideoContent()) {
MediaView mediaView = new MediaView(getActivity());
MediaView mediaView = new MediaView(requireActivity());
mediaView.setMediaContent(nativeCustomFormatAd.getMediaContent());
mediaPlaceholder.addView(mediaView);
} else {
ImageView mainImage = new ImageView(getActivity());
mainImage.setAdjustViewBounds(true);
mainImage.setImageDrawable(nativeCustomFormatAd.getImage("MainImage").getDrawable());

mainImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View unusedView) {
nativeCustomFormatAd.performClick("MainImage");
}
});
mainImage.setOnClickListener(unusedView -> nativeCustomFormatAd.performClick("MainImage"));
mediaPlaceholder.addView(mainImage);
}
customControlsView.setMediaContent(nativeCustomFormatAd.getMediaContent());
Expand Down Expand Up @@ -314,7 +300,3 @@ public void onAdFailedToLoad(LoadAdError loadAdError) {
customControlsView.reset();
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;
Expand All @@ -33,7 +35,6 @@
public class AdManagerCustomTargetingFragment extends Fragment {

private Spinner sportsSpinner;
private Button loadButton;
private AdManagerAdView adView;

public AdManagerCustomTargetingFragment() {
Expand All @@ -46,15 +47,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

sportsSpinner = getView().findViewById(R.id.customtargeting_spn_sport);
loadButton = getView().findViewById(R.id.customtargeting_btn_loadad);
adView = getView().findViewById(R.id.customtargeting_av_main);
sportsSpinner = view.findViewById(R.id.customtargeting_spn_sport);
Button loadButton = view.findViewById(R.id.customtargeting_btn_loadad);
adView = view.findViewById(R.id.customtargeting_av_main);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getView().getContext(),
R.array.customtargeting_sports, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(
view.getContext(),
R.array.customtargeting_sports,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sportsSpinner.setAdapter(adapter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;
Expand All @@ -43,25 +45,25 @@ public AdManagerFluidSizeFragment() {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// Inflate the layout for this fragment.
return inflater.inflate(R.layout.fragment_gam_fluid_size, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// The size for this AdManagerAdView is defined in the XML layout as AdSize.FLUID. It could
// also be set here by calling adView.setAdSizes(AdSize.FLUID).
// The size for this AdManagerAdView is defined in the XML layout as AdSize.FLUID.
// It could also be set here by calling adView.setAdSizes(AdSize.FLUID).
//
// An ad with fluid size will automatically stretch or shrink to fit the height of its
// content, which can help layout designers cut down on excess whitespace.
adView = getView().findViewById(R.id.fluid_av_main);
adView = view.findViewById(R.id.fluid_av_main);

AdManagerAdRequest request = new AdManagerAdRequest.Builder().build();
adView.loadAd(request);

changeAdViewWidthButton = getView().findViewById(R.id.fluid_btn_change_width);
changeAdViewWidthButton = view.findViewById(R.id.fluid_btn_change_width);
changeAdViewWidthButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdSize;
Expand All @@ -48,19 +50,18 @@ public AdManagerMultipleAdSizesFragment() {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// Inflate the layout for this fragment.
return inflater.inflate(R.layout.fragment_gam_multiple_ad_sizes, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

loadButton = getView().findViewById(R.id.adsizes_btn_loadad);
cb120x20 = getView().findViewById(R.id.adsizes_cb_120x20);
cb320x50 = getView().findViewById(R.id.adsizes_cb_320x50);
cb300x250 = getView().findViewById(R.id.adsizes_cb_300x250);
adView = getView().findViewById(R.id.adsizes_pav_main);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
loadButton = view.findViewById(R.id.adsizes_btn_loadad);
cb120x20 = view.findViewById(R.id.adsizes_cb_120x20);
cb320x50 = view.findViewById(R.id.adsizes_cb_320x50);
cb300x250 = view.findViewById(R.id.adsizes_cb_300x250);
adView = view.findViewById(R.id.adsizes_pav_main);

adView.setAdListener(new AdListener() {
@Override
Expand Down
Loading

0 comments on commit 8c6be90

Please sign in to comment.