I'm trying to put a mobclix ad banner in a cocos2D game. I have the ad banner showing up on top of the openGL view. However, I can not figure out how to place it at the bottom of the screen which is what we want. The example from mobclix shows the use of a LinearLayout with gravity set to bottom. I tried this in the GameActivity which is the main activity on startup:
adview_banner = new MobclixMMABannerXLAdView(this);
adview_banner.addMobclixAdViewListener(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
this.addContentView(adview_banner, params);
adview_banner.bringToFront();
adview_banner.getAd();
adview_banner.setRefreshTime(30000);
No matter what I do here the banner always shows up on the top of the screen. Any help would be greatly appreciated.
OK! I think I finally have it! Those layouts are tricky at first programmatically but they do match up with what the example had in the xml. I just needed to embed the banner in a layout object and send it to the bottom of that layout.
In the main Activity:
#Override
public void onStart() {
super.onStart();
RelativeLayout layout = new RelativeLayout(this);
adview_banner = new MobclixMMABannerXLAdView(this);
adview_banner.addMobclixAdViewListener(this);
RelativeLayout.LayoutParams childParams = new RelativeLayout.LayoutParams(320, 50);
childParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adview_banner, childParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(layout, params);
//adview_banner.bringToFront();
adview_banner.getAd();
adview_banner.setRefreshTime(30000);
}
Now the only issue is that I get a "can't get the viewWidth after the first layout error" on the Logcat from "webcore". And the ad banner displays "An error has occurred. Click here for more details."
I think I'll consult the Mobclix support team on that one.
Use Absolute Layout.
android:layout_width="40px"
android:layout_marginRight="5px"
android:layout_height="40px"
etc
Linear Layouts can only set gravities to each individual child.
Was it useful?
Related
i know, there are a lot of questions like this. I read a lot on stackoverflow and google about this topic, but nothing help me :(
Ok, here is the problem. I have a small app. In this app i have a fragment. The layout.xml for this fragment includes a placeholder linearlayout like the following
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/placeholderLinLayout">
</LinearLayout>
The fragment has a button. If u click on it a DialogFragmentPopup opens and u can enter some data-stuff. After you enter the data you can click on another button on this dialog and the data will be transfere to the main-fragment. Here i call a method which should generate programmatically a layout to present the data. I use the following code
myRoot = (LinearLayout) view.findViewById(R.id.placeholderLinLayout);
innerLayout = new LinearLayout(view.getContext());
innerLayout.setOrientation(LinearLayout.VERTICAL);
innerLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout productHeaderLayout = new LinearLayout(getContext());
productHeaderLayout.setOrientation(LinearLayout.HORIZONTAL);
productHeaderLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TextView product_header = new TextView(getContext());
product_header.setText("Produkt");
product_header.setLayoutParams(layoutParams);
TextView amount_header = new TextView(getContext());
amount_header.setText("Menge");
amount_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
amount_header.setLayoutParams(layoutParams);
TextView packaging_header = new TextView(getContext());
packaging_header.setText("Verpackung");
packaging_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
packaging_header.setLayoutParams(layoutParams);
TextView price_header = new TextView(getContext());
price_header.setText("Preis");
price_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
price_header.setLayoutParams(layoutParams);
TextView payment_header = new TextView(getContext());
payment_header.setText("Zahlart");
payment_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
payment_header.setLayoutParams(layoutParams);
productHeaderLayout.addView(product_header);
productHeaderLayout.addView(amount_header);
productHeaderLayout.addView(packaging_header);
productHeaderLayout.addView(price_header);
productHeaderLayout.addView(payment_header);
innerLayout.addView(productHeaderLayout);
The problem is, that the first textview push all other textviews out of the visible space, see the screenshot
What i want to do is, that these 5 textviews spread out automatically to the existing width. I googled a lot and the code i post here is the result of which i found many times on the internet.
So i hope someone can help find out the problem in my code :)
Greetings
Set all your TextView layout paramters to this:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
And remove .setWidth(ViewGroup.LayoutParams.MATCH_PARENT); from all the TextViews.
This will guarantee that all the views will have same weight set to them, and that weight gives all the views in LinearLayout same Width (or Height if orientation is set to vertical).
The issue is that your setting the TextView to MATCH_PARENT in its width. So one TextView takes the whole screen and the other starts just out of it. To solve this set the layoutparam width to WRAP_CONTENT.
Better yet, if you want to spread it, you can use the LinearLayout's weight property so they take as much space as they can:
textview.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
The third parameter 1f is the weight. A weight of one means it'll take all the available space without intruding on the other children, hence they will all spread evenly.
If you want to have your TextViews side by side, you must set the orientation of the LinearLayout to horizontal instead of vertical
I'm sure this has come up often, but I couldn't find an exact solution for my issue. I'm simply trying to programmatically create a linear layout with a textview and 2 buttons and add it to the bottom of my fragment (which is also a linear layout). I.e. I want it to appear below the other views on the screen not the actual bottom. However, when I try to add it, it always appears at the top corner and not the end. Here's my code:
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setBackgroundColor(Color.CYAN);
TextView noteView = new TextView(getActivity());
LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
noteView.setLayoutParams(lparams);
noteView.setText(noteText);
layout.addView(noteView);
getActivity().addContentView(layout, lparams);
}
Have you tried to add gravity to the LinearLayout?
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
layout.setLayoutParams(params);
If your main goal is to have your views line up relative to each other, perhaps it would be worth considering using a Relative Layout to accomplish a similar task.
If that's not an option for whatever reason, I think LayoutParams has a gravity option that can be set to bottom which will align views to the bottom of the parent view. I can't guarantee this will work though as I usually do not accomplish tasks like these programmatically.
I've implemented Admob in my PreferenceActivity as a custom preference. It works, even though the ad gets a little cut of, but when I scroll down and up the ad disappears for a second and shows again.
Is it possible to place the add to the bottom of the screen, overlaying the preference list? (Like in a regular activity).
You can create a layout for the Ad, Also set parameters for the Layout according to the coordinates of the screen, now add this layout to your main layout.
RelativeLayout myLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(adWidth,adHeight);
params.leftMargin=0;
params.topMargin = h-adHeight;
//adding Adview and other portion view to relative layout created
myLayout.addView(adview,params);
myLayout.addView(yourotherportionview);
setContentView(myLayout);
I have created a pagecontroller application and added it on my page in its own linear layout.
However, I can not get the layout width to fill the entire parent page..
This is my code:
pagecontrol = new LinearLayout(this.context);
pagecontrol.setOrientation(LinearLayout.HORIZONTAL);
pagecontrol.setGravity(Gravity.CENTER_HORIZONTAL);
params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT);
pagecontrol.addView(btn,params);
public LinearLayout getView() {
return pagecontrol;
}
I have attached the screenshot of the page. Could anyone please advise?
Thanks in advance.
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
You must use these properties.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Please check the height and width of layout if you take the outer layout of your linear layout.
Change it:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT);
replace WRAP_CONTENT with FILL_PARENT it will work
I've been looking at similar questions but they are more on how to get the ads to show up with this new Admob version, I'm getting ads to show so thats not my problem, my problem is that ads show in the bottom of the screen so when the app expands the content on the screen the adview disappears since it goes under all the content. my solution is to place the adview on top of the screen and that way no matter how much content is on the screen the adview will be visible! I was able to accomplish this before when the adview was embedded into my xml, but following the admob tutorial you don't put it on the xml anymore. So how can I place the Ad on top of the mainLayout? here is the code I have been trying.
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
adView = new AdView(this, AdSize.BANNER, "my code");
layout.addView(adView);
AdRequest r = new AdRequest();
adView.loadAd(r);
// this is what I try to get it on top of the screen, but is not working
adView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
adView.bringToFront();
this is all since I haven't done anything to the XML, is still showing the adview in the bottom, so how can I get it on the top part of the screen?
Thanks
Your code is setting the gravity of the AdView, but the AdView is within a LinearLayout. Place the LinearLayout where you want it to appear.
Here is the documentation for how to add your banner in XML in 4.3.1.
If you still want to do it in code, try making your top level layout a RelativeLayout, and use this code:
adView = new AdView(this, AdSize.BANNER, "my code");
AdRequest r = new AdRequest();
adView.loadAd(r);
RelativeLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, params);
When you add the view, add it with an index. This works for me:
layout.addView(adView, 0);