Admob ad not showing in my application. I am sensing ad is loading but somehow its not showing because of my layout arrangement. but i couldn't locate it. here is my code
protected void onSetContentView() {
final RelativeLayout relativeLayout = new RelativeLayout(this);
final FrameLayout.LayoutParams relativeLayoutLayoutParams = new FrameLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine);
final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT,Gravity.CENTER);
final android.widget.RelativeLayout.LayoutParams surfaceViewLayoutParams = new RelativeLayout.LayoutParams(layoutParams);
surfaceViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
FrameLayout frameLayout = new FrameLayout(this);
adView = new AdView(this, AdSize.BANNER, "##############");
adView.refreshDrawableState();
adView.setVisibility(AdView.VISIBLE);
frameLayout.addView(adView);
relativeLayout.addView(frameLayout);
this.setContentView(relativeLayout, relativeLayoutLayoutParams);
}
You have told your SurfaceView to FILL_PARENT, so it is taking all the space leaving none for the AdView sibling.
final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT,Gravity.CENTER);
Note also there is no need for the FrameLayout wrapping the AdView.
I suggest you follow jquery404's advice and define the layout in XML. You'll get a much better understanding of your ui design.
Related
I want to play a gif animation and this view will move on the screen every second, but if I start an activity I need to add this view again and the position will change.
See the attached image below. I need to stick this over every view. Any way to do it?
Create one BaseActivity for all activities in your app to show Overlay View or Layout.
you can inherit BaseActivity on other Activity
You can add gif supported views or layouts
for example i added overlay_layout in my showOverlay method.
you can call showOverlay method where ever you want to show and you can remove with removeOverlay with conditions.
Please note that showOverlay and removeOverlay should be in BaseActivity
void showOverlay(){
LayoutInflater inflater = activity.getLayoutInflater();
layout = inflater.inflate(R.layout.overlay_layout, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.BOTTOM;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION;
final WindowManager mWindowManager = (WindowManager);
activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(layout, params);
}
void removeOverlay(){
windowManager.removeView(view);
}
set this view into WindowManager and it will always show on top
How can i programmatically make my AlertDialog Scrollable ?
My AlertDialog is defined like this :
private void DisplayAlertChoice(String title, String msg, final int pos)
{
alert = new AlertDialog.Builder(this);
alert.setTitle(title);
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);
layout.addView(t[i]);
}
alert.setView(layout);
alert.show();
}
I tried a few solutions found in the net but didn't work for me.
Can someone give me a pertinent solution to make it scrollable programmatically ?
i tried it, but it forces my app to stop.
ScrollView can have just a child. It is an Android constraint. So, doing
final LinearLayout layout = new LinearLayout(this);
ScrollView scrollView = new ScrollView(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);
scrollView.addView(t[i]);
}
will make your app crash, because you are adding more than one child to the ScrollView. But if you do
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);
layout.addView(t[i]);
}
final ScrollView scrollView = new ScrollView(this);
scrollView.addView(layout);
it should work smoothly.
As the title reads, I am getting this error in my LogCat when I run my app. This is the code written after which the error occured:
public void coinAnim1() {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin1 = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
coin1 = (ImageView) findViewById(R.id.coinid);
Animation coinFall1 = AnimationUtils.loadAnimation(this,
R.anim.coinanimation);
coin1.startAnimation(coinFall1);
rl.addView(coin1, params);
}
public void coinAnim2() {
RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin2 = new ImageView(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(30, 40);
params2.leftMargin = 50;
coin2 = (ImageView) findViewById(R.id.coin2id);
Animation coinFall1 = AnimationUtils.loadAnimation(this,
R.anim.coinanimation);
coin2.startAnimation(coinFall1);
rl2.addView(coin2, params2);
}
LogCat says the line "rl.addView(coin1, params);" specifically causes the error.Does anyone know what I can do to fix this? There are similar questions, but as I am new to coding I don't know how to adapt the answers for my problem.
Any help is appreciated.
PROBLEM SOLVED:
Ashishduh solved the problem by replacing the line:
rl.addView(coin1, params);
With the line:
coin1.setLayoutParams(params);
Hope this helps someone! Thanks ashi <3
The View coin1 already has a parent (the RelativeLayout that you inflated). You can't assign it to another parent without removing it from that RelativeLayout.
I think what you're trying to do is modify the LayoutParams of coin1. Instead of doing this: rl.addView(coin1, params); you need to just set the layoutparams like this coin1.setLayoutParams(params);
From ur code snippet
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin1 = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
coin1 = (ImageView) findViewById(R.id.coinid);
u r creating coin1 & coin2 ImageView programmatically as u have already added those in xml either create those ImageViews programmatically or in xml
NOTE: Am using the new GooglePlayServices AdMob at version 4.4
When "onCreate" is call I create the content view like so:
ContentView = new FrameLayout(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
addContentView(ContentView, params);
When I set my AdView gravity, I do it like so:
FrameLayout.LayoutParams viewParams = new FrameLayout.LayoutParams(320, 50, Gravity.BOTTOM | Gravity.LEFT);
adView.setLayoutParams(viewParams);
Any ideas whats wrong?
Set the adview's parameters as follows:
RelativeLayout.LayoutParams viewParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
viewParams.addRule(RelativeLayout.ALIGN_LEFT);
adView.setLayoutParams(viewParams);
I have a surface (which extends SurfaceView), an AdView and now I want to add a WebView, which I'm going to need to position dynamically.
For start, I'm trying to add my web view at the center and make its size the device's width and half its height. I'm with a 800X480 device and landscape mode, so for now I want to set its border at left=0, top=120, right=800, bottom=360, just for the sake of testing. Later on I'll want to be able to set it dynamically, but so far this doesn't even work:
private MySurface surface;
private AdView adView;
private WebView
.
.
.
// create the ad view
adView = new AdView(this, AdSize.SMART_BANNER, AD_MOB_ID);
adView.setAdListener(new MyAdListener());
// create the surface
surface = MySurface.getRef(this);
// set a listener for touch event
surface.setOnTouchListener(this);
// create the web view
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
webView.setVisibility(View.GONE);
LayoutParams webViewParams = new LayoutParams(800,240); // this works
webViewParams.setMargins(0, 120, 800, 360); // this does nothing...
// create a relative layout
RelativeLayout l = new RelativeLayout(this);
// add the surface
l.addView(surface);
// add the ad view at the bottom
AdView.LayoutParams adViewParams = new AdView.LayoutParams(
AdView.LayoutParams.WRAP_CONTENT,
AdView.LayoutParams.WRAP_CONTENT);
adViewParams.addRule(AdView.ALIGN_PARENT_BOTTOM);
l.addView(adView, adViewParams);
l.addView(webView, webViewParams);
setContentView(l);
// load an ad
//loadAdMob();
when I later set the webview visibility to visible:
webView.loadUrl(url);
webView.setVisibility(View.VISIBLE);
It appears the right width and height but starts at the very top of the screen :(
Can anyone help ? Thanks.
Ok, I think I got it:
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
RelativeLayout.LayoutParams webViewParams= new RelativeLayout.LayoutParams(800, 240);
webViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
RelativeLayout l = new RelativeLayout(this);
l.addView(surface);
l.addView(adView, adViewParams);
l.addView(webView, webViewParams);
setContentView(l);
credit goes here