Search code examples
androidadmobmarkup

Adding AdBlock to Layout Android


I'm new at android developing, so asking you for help.

I have the code like that:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/home_layout"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_above="@+id/adView">
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:id="@+id/progressBar" />
</LinearLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

</LinearLayout>

In my logic, AdBlock should display at the bottom of view. But in my case it's just not appearing at all. If I past Ad's markup inside LinearLayout it gives me crash of VM.

What I'm doing wrong? Thank you in advance

UPD: Java activity

public class PddViewActivity extends ActionBarActivity{

private WebView webView;
private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pdd_view);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    progressBar = (ProgressBar) findViewById(R.id.progressBar);

    String file = getIntent().getStringExtra("file");

    webView = (WebView) findViewById(R.id.webview);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);


    webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            Log.i("PROGRESS IS :", newProgress+"");
            progressBar.setProgress(newProgress);
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getApplicationContext(), "Error: " + description + " " + failingUrl, Toast.LENGTH_LONG).show();
        }

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            if (progressBar.getVisibility() == WebView.VISIBLE) {
                progressBar.setVisibility(WebView.GONE);
            }
        }
    });

    webView.loadUrl(String.format("file:///android_asset/html/%s", file));

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

Solution

  • Your problem is that home_layout has a layoutHeight of match_parent, which means it will consume all the height of its parent leaving none for you adView.

    Instead use wrap_content and specify android:layoutWeight="1" to tell it to consume all unused space. Eg

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    <LinearLayout 
        android:id="@+id/home_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:id="@+id/progressBar" />
    </LinearLayout>
    
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
    
    </LinearLayout>
    

    Note also:

    1. layout_above has no meaning except within a relative_layout.
    2. fill_parent has been deprecated, use match_parent instead.
    3. You had the same problem with your WebView.