java - How to add admob banner in a fragment view? -
i new android development , confused how add admob banner fragment activity(i think calling fragment). modifying source code.
i have layout file called fragment_ftp.xml , below
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:ads="http://schemas.android.com/apk/res-auto"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="" android:id="@+id/statustext" android:layout_above="@+id/startstopbutton" android:layout_centerhorizontal="true" android:layout_marginbottom="99dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="" android:id="@+id/ftpaddresstext" android:layout_below="@+id/startstopbutton" android:layout_centerhorizontal="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start_ftp" android:id="@+id/startstopbutton" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:textcolor="@color/primary_red" android:text="" android:id="@+id/warningtext" android:layout_above="@+id/startstopbutton" android:layout_centerhorizontal="true" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ftp_image" android:layout_above="@+id/warningtext" android:layout_centerhorizontal="true" /> <relativelayout android:id="@+id/relativelayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true"> <com.google.android.gms.ads.adview android:id="@+id/adview" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adsize="smart_banner" ads:adunitid="@string/banner_home_footer" /> </relativelayout> </relativelayout>
i had added admob banner , in preview, can see that. please check below screenshot.
but issues that, should write java code admob banner. don't have corresponding activity fragment_ftp.xml.
searching fragment_ftp, see java file follows. ftpserverfragment.java
package com.filename.fragments; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.content.sharedpreferences; import android.net.connectivitymanager; public class ftpserverfragment extends fragment { textview statustext,warningtext,ftpaddrtext; button ftpbtn; futils utils = new futils(); private mainactivity mainactivity; private view rootview; private broadcastreceiver mwifireceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { connectivitymanager conman = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo netinfo = conman.getactivenetworkinfo(); if (netinfo != null && netinfo.gettype() == connectivitymanager.type_wifi){ warningtext.settext(""); } else{ stopserver(); statustext.settext(utils.getstring(getcontext(),r.string.ftp_status_not_running)); warningtext.settext(utils.getstring(getcontext(),r.string.ftp_no_wifi)); ftpaddrtext.settext(""); ftpbtn.settext(utils.getstring(getcontext(),r.string.start_ftp)); } } }; private broadcastreceiver ftpreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if(action == ftpservice.action_started) { statustext.settext(utils.getstring(getcontext(), r.string.ftp_status_running)); warningtext.settext(""); ftpaddrtext.settext(getftpaddressstring()); ftpbtn.settext(utils.getstring(getcontext(),r.string.stop_ftp)); } else if(action == ftpservice.action_failedtostart){ statustext.settext(utils.getstring(getcontext(),r.string.ftp_status_not_running)); warningtext.settext("oops! went wrong"); ftpaddrtext.settext(""); ftpbtn.settext(utils.getstring(getcontext(),r.string.start_ftp)); } else if(action == ftpservice.action_stopped){ statustext.settext(utils.getstring(getcontext(),r.string.ftp_status_not_running)); ftpaddrtext.settext(""); ftpbtn.settext(utils.getstring(getcontext(),r.string.start_ftp)); } } }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sethasoptionsmenu(false); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment rootview = inflater.inflate(r.layout.fragment_ftp,container,false); // return inflater.inflate(r.layout.article_view, container, false); statustext =(textview) rootview.findviewbyid(r.id.statustext); warningtext = (textview) rootview.findviewbyid(r.id.warningtext); ftpaddrtext = (textview) rootview.findviewbyid(r.id.ftpaddresstext); ftpbtn = (button) rootview.findviewbyid(r.id.startstopbutton); sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(getcontext()); int th = integer.parseint(sp.getstring("theme", "0")); // checking if theme should set light/dark or automatic int theme1 = th == 2 ? preferenceutils.hourofday() : th; imageview ftpimage = (imageview)rootview.findviewbyid(r.id.ftp_image); //light theme if(theme1 == 0){ ftpimage.setimageresource(r.drawable.ic_ftp_light); }else{ //dark ftpimage.setimageresource(r.drawable.ic_ftp_dark); } ftpbtn.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { if(!ftpservice.isrunning()){ if(ftpservice.isconnectedtowifi(getcontext())) startserver(); else warningtext.settext(utils.getstring(getcontext(),r.string.ftp_no_wifi)); } else{ stopserver(); } } }); return rootview; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); setretaininstance(true); mainactivity=(mainactivity)getactivity(); mainactivity.setactionbartitle(utils.getstring(getactivity(), r.string.ftp)); mainactivity.floatingactionbutton.hidemenubutton(true); mainactivity.buttonbarframe.setvisibility(view.gone); mainactivity.supportinvalidateoptionsmenu(); } @override public void ondestroy(){ super.ondestroy(); } private void startserver() { getcontext().sendbroadcast(new intent(ftpservice.action_start_ftpserver)); } private void stopserver() { getcontext().sendbroadcast(new intent(ftpservice.action_stop_ftpserver)); } @override public void onresume(){ super.onresume(); updatestatus(); intentfilter wififilter = new intentfilter(); wififilter.addaction(connectivitymanager.connectivity_action); getcontext().registerreceiver(mwifireceiver,wififilter); intentfilter ftpfilter = new intentfilter(); ftpfilter.addaction(ftpservice.action_started); ftpfilter.addaction(ftpservice.action_stopped); ftpfilter.addaction(ftpservice.action_failedtostart); getcontext().registerreceiver(ftpreceiver,ftpfilter); } @override public void onpause(){ super.onpause(); getcontext().unregisterreceiver(mwifireceiver); getcontext().unregisterreceiver(ftpreceiver); } private void updatestatus(){ if(ftpservice.isrunning()){ statustext.settext(utils.getstring(getcontext(),r.string.ftp_status_running)); ftpbtn.settext(utils.getstring(getcontext(),r.string.stop_ftp)); ftpaddrtext.settext(getftpaddressstring()); } else{ statustext.settext(utils.getstring(getcontext(),r.string.ftp_status_not_running)); ftpbtn.settext(utils.getstring(getcontext(),r.string.start_ftp)); } } private string getftpaddressstring(){ return "ftp://"+ftpservice.getlocalinetaddress(getcontext()).gethostaddress()+":"+ftpservice.getport(); } }
i java page shall write admob call?
try , use code solve issue
http://www.androidhive.info/2016/02/android-how-to-integrate-google-admob-in-your-app/
Comments
Post a Comment