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);
Related
I have a resource png of a house linked if you want to view it. It's basically just a dark home button.
I have it included and imported as an image asset as a drawable.
I'm attempting to set it as a button, but change it to be a white color programmatically.
Here's how I'm attempting to change the color to be white:
ImageButton txtbtnAccept = new ImageButton(this);
this._surveyHomeButton = txtbtnAccept;
txtbtnAccept.setId(this.generateViewId());
txtbtnAccept.setLayoutParams(new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
layoutParams = (RelativeLayout.LayoutParams) txtbtnAccept.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.height = 200;
layoutParams.width = 200;
txtbtnAccept.setScaleType(ImageView.ScaleType.FIT_CENTER);
txtbtnAccept.setAdjustViewBounds(true);
txtbtnAccept.setLayoutParams(layoutParams);
txtbtnAccept.setBackgroundResource(0);
txtbtnAccept.setVisibility(View.GONE);
Bitmap homeImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_home);
Drawable whiteImg = new BitmapDrawable(getResources(), homeImage);
whiteImg.setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
txtbtnAccept.setImageDrawable(whiteImg);
txtbtnAccept.setImageBitmap(homeImage);
Any idea where I'm going wrong?
What you want is called tinting. Have a look at DrawableCompat.setTint(Drawable, int).
I am trying to create a method that will add a new LinearLayout to an existing LinearLayout. I would like to send a parameter to this method telling it whether the new LinearLayout should be VERTICAL or HORIZONTAL. This is what I have so far but 'LinearLayout.Orientation' is not the correct name for this parameter type.
private LinearLayout addLinearLayout(View linearLayout, LinearLayout.Orientation orientation)
{
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(LinearLayout.orientation);
((LinearLayout) linearLayout).addView(newLinearLayout, layoutParams);
return newLinearLayout;
}
Is this possible? If so, what is the type of the parameter I am trying to pass?
Thanks for any help.
From the documentation:
public void setOrientation (int orientation)
(...)
orientation Pass HORIZONTAL or VERTICAL. Default value is HORIZONTAL.
private LinearLayout addLinearLayout(LinearLayout linearLayout, int orientation) {
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(orientation);
linearLayout.addView(newLinearLayout, layoutParams);
return newLinearLayout;
}
Call this method like this:
addLinearLayout(yourLinearlayout, LinearLayout.HORIZONTAL);
The orientation of a Layout seems to be represented by an Integer...
private LinearLayout addLinearLayout(LinearLayout linearLayout, int orientation)
{
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(orientation);
linearLayout.addView(newLinearLayout, layoutParams);
}
// Then you can call your function like this
addLinearLayout(layout, LinearLayout.VERTICAL);
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
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.
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