0

I'm making a GymExercises app. I have two questions:

  1. I include ads in every Exercise but now the UI lags too much. How can I put them in the background so they run in the background?

  2. When I use smart banner, sometimes it doesn't show ads. With small banner, it's working perfectly.

BenchFragment.java:

package com.Hristijan.Aleksandar.GymAssistant.Exercises; 
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.VideoView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView; 

public class BenchFragment extends Fragment {

    private VideoView player;
    private String videopath;
    private MediaController mediacon;
    public  View rootView;
    private AdView mAdView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_bench, container, false);
        mAdView = rootView.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        MobileAds.initialize(getActivity().getApplicationContext(), "ca-app-pub-7751214307362146~7028081975");
        mAdView.loadAd(adRequest);
        mediacon = new MediaController(getActivity());
        player = (VideoView) rootView.findViewById(R.id.videoplayer);
        videopath = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.bench;
        player.setVideoURI(Uri.parse(videopath));
        mediacon.setAnchorView(player);
        player.start();
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                mp.setVolume(0, 0);
            }
        });
        return rootView;
    }
}

fragment_bench.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bench_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:fillViewport="true"
    tools:context="com.Hristijan.Aleksandar.GymAssistant.Exercises.BenchFragment">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

            <VideoView
                android:layout_width="match_parent"
                android:layout_height="@dimen/_160sdp"
                android:foregroundGravity="center"
                android:id="@+id/videoplayer" />

            <TableLayout
                android:layout_below="@+id/videoplayer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:shrinkColumns="*">

            <TableRow>
                <TextView
                    android:text="@string/group"
                    android:background="@color/colorPrimary"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textColor="#ffffff"
                    android:textSize="@dimen/_10sdp"
                    android:padding="5dip" />
            </TableRow>

            <TableRow>
                <TextView
                    android:text="@string/bencbody"
                    android:textSize="@dimen/_10sdp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textColor="#ffffff"
                    android:padding="5dip" />
            </TableRow>

            <TableRow>
                <TextView
                    android:text="@string/description"
                    android:textSize="@dimen/_10sdp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/colorPrimary"
                    android:textColor="#ffffff"
                    android:padding="5dip" />
            </TableRow>

            <TableRow>
                <TextView
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="left"
                    android:layout_marginTop="5dp"
                    android:text="@string/stepone"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/_10sdp"
                    android:padding="3dip" />
            </TableRow>

            <TableRow>
                <TextView
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="5dp"
                    android:gravity="left"
                    android:text="@string/steptwo"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/_10sdp"
                    android:padding="3dip" />
            </TableRow>
        </TableLayout>

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-7751214307362146/9071791385">
        </com.google.android.gms.ads.AdView>    
    </RelativeLayout>
</ScrollView>
Pang
  • 9,564
  • 146
  • 81
  • 122

0 Answers0