Баннер никак не хочет отображаться (
Я создал отдельный файл Ads. java.
В Gridle и Android в манифесте все ввел правильно .
Находится по пути: com.myproject.app.activity.Ads. java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.myproject.app.app.R;
public class Ads extends AppCompatActivity {
private AdView mAdView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
И пытается отобразить fragment_home. xml in xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/home_root"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_cat_title"
android:text="@string/collections"
android:textStyle="bold"
android:textSize="@dimen/text_16sp"
android:layout_margin="@dimen/margin_5dp" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/category_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My HomeFragment. Где они будут go fragment_home. xml Я должен писать в нем рекламу, или необязательно, или в другом файле.
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.myproject.app.app.R;
import com.myproject.app.app.activities.SecondActivity;
import com.myproject.app.app.utils.CategoryItem;
import com.myproject.app.app.utils.NetworkUtils;
import com.myproject.app.app.utils.RecyclerAdapter;
import com.myproject.app.app.utils.StaticUtils;
import java.util.ArrayList;
import com.myproject.app.app.Ads;
public class HomeFragment extends Fragment implements RecyclerAdapter.OnItemClickListener {
private static final String TAG = "HomeFragment";
private RecyclerView mRecyclerView;
private RecyclerAdapter adapter;
private View root;
private AdView mAdView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: making the layout visible");
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated: view created successfully");
super.onViewCreated(view, savedInstanceState);
mRecyclerView = view.findViewById(R.id.category_list);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemViewCacheSize(20);
mRecyclerView.setDrawingCacheEnabled(true);
mRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
root = view.findViewById(R.id.home_root);
//setting adapter
adapter = new RecyclerAdapter(getContext(),root, StaticUtils.categoryList, StaticUtils.imagesList,0); //adapter attached
adapter.setOnItemClickListener(this);
mRecyclerView.setAdapter(adapter);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: gets called.");
super.onCreate(savedInstanceState);
//getting the lists ready
StaticUtils.categoryList = new ArrayList<>();
StaticUtils.categoryList.add(new CategoryItem(R.drawable.featured,"Featured"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.abstruct,"Abstract"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.army,"Army"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.baby,"Baby"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.beach,"Beach"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.cars,"Cars"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.city,"City"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.foods,"Foods"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.girls,"Girls"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.macro,"Macro"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.nature,"Nature"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.religious,"Religious"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.sports,"Sports"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.stars,"Stars"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.technology,"Technology"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.water,"Water"));
StaticUtils.categoryList.add(new CategoryItem(R.drawable.wildlife,"Wildlife"));
}
@Override
public void onItemClick(int position) {
Log.d(TAG, "onItemClick: clicked item");
NetworkUtils network = new NetworkUtils(getContext());
if(network.checkConnection(root)){ //if network connected
Intent i = new Intent(getActivity(), SecondActivity.class);
i.putExtra(StaticUtils.KEY_FRAG_ID,0);
if (position!=0){
i.putExtra(StaticUtils.KEY_SEARCH_DATA,StaticUtils.categoryList.get(position).getmTitle());
}
startActivity(i);
}
}
}