Set View Width Programmatically
Asked Answered
P

8

82

I am trying to set the width and height of a view in code to show an ad for a free app I am working on. All of the UI is done in XML with the exception of this ad. Here is the code that displays the ad. It is in onCreate.

AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
adView.setAdListener(this);
layout.addView(adView);

This results in the ad being displayed like this. As you can see the width doesn't quite fill the whole page.

enter image description here

Now when I change the code to this to attempt to set the size:

AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
adView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                       LayoutParams.WRAP_CONTENT));
adView.setAdListener(this);
layout.addView(adView);

I get this:

enter image description here

I have tried all different combinations of FILL_PARENT and WRAP_CONTENT all with the same result. Is there something I am missing? Or another way I can have the ad fill the width of the device without filling the height? Any suggestions and help is greatly appreciated.

EDIT when I try to put it in XML I get an exception:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/main_layout">

<fragment
    android:name="com.jasoncrosby.app.tipcalculatorfree.MainButtonFragment"
android:id="@+id/button_frag"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
<com.adsdk.sdk.banner.AdView
    android:id="@+id/banner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

> 09-09 22:39:43.448: E/AndroidRuntime(586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jasoncrosby.app.tipcalculatorfree/com.jasoncrosby.app.tipcalculatorfree.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class com.adsdk.sdk.banner.AdView
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.os.Looper.loop(Looper.java:132)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.ActivityThread.main(ActivityThread.java:4025)
09-09 22:39:43.448: E/AndroidRuntime(586):  at java.lang.reflect.Method.invokeNative(Native Method)
09-09 22:39:43.448: E/AndroidRuntime(586):  at java.lang.reflect.Method.invoke(Method.java:491)
09-09 22:39:43.448: E/AndroidRuntime(586):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-09 22:39:43.448: E/AndroidRuntime(586):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-09 22:39:43.448: E/AndroidRuntime(586):  at dalvik.system.NativeStart.main(Native Method)
09-09 22:39:43.448: E/AndroidRuntime(586): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.adsdk.sdk.banner.AdView
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.view.LayoutInflater.createView(LayoutInflater.java:579)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:671)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
09-09 22:39:43.448: E/AndroidRuntime(586):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.Activity.setContentView(Activity.java:1780)
09-09 22:39:43.448: E/AndroidRuntime(586):  at com.jasoncrosby.app.tipcalculatorfree.MainActivity.onCreate(MainActivity.java:95)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
09-09 22:39:43.448: E/AndroidRuntime(586):  ... 11 more
09-09 22:39:43.448: E/AndroidRuntime(586): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
09-09 22:39:43.448: E/AndroidRuntime(586):  at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:235)
09-09 22:39:43.448: E/AndroidRuntime(586):  at java.lang.Class.getConstructor(Class.java:459)
09-09 22:39:43.448: E/AndroidRuntime(586):  at android.view.LayoutInflater.createView(LayoutInflater.java:551)
09-09 22:39:43.448: E/AndroidRuntime(586):  ... 21 more
Procathedral answered 9/9, 2012 at 22:5 Comment(3)
Just a suggestions: Why don't you add the View you want in xml (full class path like com.ads.AdView - i just made this path up, but you get the point), then access it in your Activity with findViewById(), and do whatever you want with it?Operetta
I added what happens when I put it in XML in the edit.Procathedral
mContext.getResources().getDisplayMetrics().widthPixels*3/4Grandpapa
M
75

This code let you fill the banner to the maximum width and keep the ratio. This will only work in portrait. You must recreate the ad when you rotate the device. In landscape you should just leave the ad as is because it will be quite big an blurred.

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
double ratio = ((float) (width))/300.0;
int height = (int)(ratio*50);

AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
mAdView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,height));
adView.setAdListener(this);
layout.addView(adView);
Meliorate answered 11/9, 2012 at 21:55 Comment(3)
FILL_PARENT deprecated, use MATCH_PARENT with new SDKTrevor
Display.getWidth() also deprecated. Use getSize() instead.Nemato
@Nemato getSize(new Point) returns void but we need int. how to get that valueSightly
S
117

You can use something like code below, if you need to affect only specific value, and not touch others:

view.getLayoutParams().width = newWidth;
Stinkpot answered 30/9, 2014 at 17:5 Comment(4)
what about 'wrap-content' ?Biradial
There is constant WRAP_CONTENT = -2; So you can try something like this view.getLayoutParams().width = LayoutParams.WRAP_CONTENT;Stinkpot
For some reason view.getLayoutParams().width = newWidht; and setWidth are not having the same effect, can you explain this fact?Bookmobile
this should me marked as an answer, works better and more proper than option with new layout params creationClavius
M
75

This code let you fill the banner to the maximum width and keep the ratio. This will only work in portrait. You must recreate the ad when you rotate the device. In landscape you should just leave the ad as is because it will be quite big an blurred.

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
double ratio = ((float) (width))/300.0;
int height = (int)(ratio*50);

AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
mAdView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,height));
adView.setAdListener(this);
layout.addView(adView);
Meliorate answered 11/9, 2012 at 21:55 Comment(3)
FILL_PARENT deprecated, use MATCH_PARENT with new SDKTrevor
Display.getWidth() also deprecated. Use getSize() instead.Nemato
@Nemato getSize(new Point) returns void but we need int. how to get that valueSightly
V
9

in code add below line:

spin27.setLayoutParams(new LinearLayout.LayoutParams(200, 120));
Virgina answered 21/5, 2014 at 10:45 Comment(2)
This does not seem to be an answer to this question.Pudding
i had similar problem fixing the width of spinner programatically but finally above code worked for me. Adview is another view, so same should be applied here, have not implemented but ideally should work to fix the width of any view android support.Virgina
N
7
yourView.setLayoutParams(new LinearLayout.LayoutParams(width, height));
Noellenoellyn answered 3/12, 2018 at 7:48 Comment(0)
O
1
hsThumbList.setLayoutParams(new LayoutParams(100, 400));
Ortiz answered 25/11, 2016 at 6:53 Comment(0)
D
0

The first parameter to LayoutParams is the width and the second is the height. So if you want the width to be FILL_PARENT, but the width to be, say, 20px, then use something new LayoutParams(FILL_PARENT, 20). Of course you should never use actual pixels in your code; you'll need to conver that to density-independent pixels, but you get the idea. Also, you need to make sure your parent LinearLayout has the right width and height that you're looking for. Seems to be you want the LinearLayout to fill the parent width-wise and then have the adview fill that linearlayout witdh-wise as well, so you probably need to specify android:layout_width:"fill_parent" and android:layout_height:"wrap_content" in your linear layout's xml.

Dodi answered 9/9, 2012 at 22:14 Comment(0)
A
0

In Kotlin you can use this way:

anyview.layoutParams.height=MATCH_PARENT
anyview.layoutParams.width=MATCH_PARENT

but remember you can just set after setContentView, and you can't change it by click immediately look like animate()

Alar answered 14/4, 2022 at 20:21 Comment(0)
S
-1

check it in mdpi device.. If the ad displays correctly, the error should be in "px" to "dip" conversion..

Spiky answered 19/6, 2013 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.