I manage to add adMob to the title screen, which layout is written in a *.xml-file.
setContentView(R.layout.main);
AdView adView = (AdView)findViewById(R.id.ad);
adView.requestFreshAd();
main.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe"
android:background="#drawable/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
</RelativeLayout>
The game itself is written in Tttview.
public class TttView extends View
The Activity Game.java create a instance of TttView and uses setcontent(tttView) to use it as layout.
public class Game extends Activity{
private TttView tttView;
.
.
.
setContentView(tttView);
}
How can I add adMob to the game itself?
Well it's not obvious from your question what tttView is or how it is setup, but if it creates a layout type to display you can add a AdView programatically into a LinearLayout or similar like this:
// Setup layout parameters
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Create a linear layout
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);
AdView mAdView = new AdView(this);
layout.addView(mAdView,params);
Or if you are loading the layout from an XML file you can do similar what you have above
setContentView(R.layout.game);
Where game.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe"
android:background="#drawable/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<tttView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
...
/>
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
</RelativeLayout>
Related
i'm very new to Android and AChartengine and i'm working in my project.
Assume i have a Layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/displayECG"
android:layout_width="fill_parent"
android:layout_height="100px"
android:orientation="horizontal" >
</LinearLayout>
And in MainActivity.java i code like:
view = ChartFactory.getLineChartView(this, dataSet, renderer);
LinearLayout layout = (LinearLayout) findViewById(R.id.displayECG);
layout.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
But when i run my App and i got a graph, it very smaller than layout space. I want a graph, it fits with my layout, so how can i do that?
Thanks,
I have problem with AdMob in my Android application. I have done exactly everything described here, but when I start the program, it doesn't run (there are a lot of bugs). I will show you a fragment of program:
public class MenuMainActivity extends Activity{
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_main);
adView = new AdView(this, AdSize.BANNER, "(code-15 signs)");
RelativeLayout layout = (RelativeLayout)findViewById(R.layout.activity_menu_main);
layout.addView(adView);
adView.loadAd(new AdRequest());
And also xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/titleOfGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="#string/title_of_game_text" />
...
When I comments two lines:
//layout.addView(adView);
//adView.loadAd(new AdRequest());
Everything is ok. Where is the problem?
If you use findViewById() you must pass an id, there you are passing a "layout".
should be something like:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);
create a Layout with "adLayer" as the "id" and position it in your layout where you like the Ads to show up.
As the previous posters have mentioned you need to create a specific place in your layout where you want yout advert to appear, for example, using your supplied code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/titleOfGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="#string/title_of_game_text"/>
<RelativeLayout
android:id="#+id/adLayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
etc. etc./>
You then need to change the code in your activity to :
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);
as BrainCrash suggested.
Hope this helps!
I am trying to make dynamic layout in which there is a scroll view.
In the scroll view , I am adding vertical linear layout which references main.xml.
The code stops as soon , I try to add the layout referencing main.xml.
ScrollView scroll;
EditText search;
Button checkin;
LinearLayout vt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
scroll=new ScrollView(getApplicationContext());
vt=(LinearLayout) findViewById(R.id.llv);///referencing linear layout located in main.xml.This is where problem the is occurring.
//vt.setOrientation(LinearLayout.VERTICAL);
scroll.addView(vt);
this.setContentView(scroll);
}
main.xml ( where R.id.llv is located )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/llv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
try to add
setContentView(R.layout.main);
before:
vt=(LinearLayout) findViewById(R.id.llv);
try this :
view row = LayoutInflater.from(this).inflate(R.layout.main, null);
LinearLayout layout=row.findViewById(R.id.YourLayout);
Check this to create Linear Layout Dynamically.
http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/
I have this XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fondoverde3"
android:gravity="top"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="adasdasdasdas"
ads:loadAdOnCreate="true" />
</LinearLayout>
<ScrollView
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
......
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout4"
android:layout_marginTop="10dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
So what I would like is to refer in my java code to the LinearLayout2 an add some things. I prove with an easy code to know that my code works (and it works!! I show the two textviews), exactly with this:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tituloIngr2 = new TextView(this);
tituloIngr2.setText("AAAAA");
ll.addView(tituloIngr2);
TextView tituloIngr1 = new TextView(this);
tituloIngr1.setText("BBBBB");
ll.addView(tituloIngr1);
But what I would like to do is to show in this linearLayout2 many Textviews that I take from a StringArray, so the code is the next:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
for (int m= 0; m<few.length; m++) {
TextView ingr= new TextView(this);
ingr.setText(few[m]);
ingr.setPadding(5, 5, 5, 5);
ingr.setGravity(0x11);//sacados todos los values de developers
ingr.setTextColor(0x96610098);
ingr.setBackgroundResource(android.R.color.white);
ingr.setTextSize(22);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 25, 0, 0);
ll.addView(ingr, layoutParams);
}
this.setContentView(ll);
The error is a force close, and the log cat:
11-27 17:09:58.213: E/AndroidRuntime(1440): java.lang.RuntimeException: Unable to start activity ComponentInfo{back.now/back.now.Tree}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Remove this call:
this.setContentView(ll);
ll is already part of the content view. Trying to add it again as the content view is likely what's triggering the exception. You also don't need this call:
ll.setOrientation(LinearLayout.VERTICAL);
The orientation is specified as vertical in the xml.
I am new to android development, but I am trying to add admob to my app which is using a surfaceview (panelView - GameView).
I tried following http://rx-games.com/admob-adverts-on-surfaceview-no-xml-tutorial/ but I must be doing something wrong, as rl.addView(panelView); adds a nullPointerException. Any help is greatly appreciated.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//setContentView(R.layout.main);
AdView adView = new AdView(this, AdSize.BANNER, "a14ded47ad3779e");
panelView = (GameView) findViewById(R.id.gameScreen);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(panelView);
rl.addView(adView);
setContentView(rl);
threadView = panelView.getThread();
}
You are inflating the GameView class from an xml layout and add it to a root View that is not the one defined in that same xml. You have two options:
You create your SurfaceView entirely in Java (as in the example you posted)
You include the AdView in the xml layout (I would prefer this option over the other one)
If you name the xml file game_layout.xml, you use it as:
setContentView(R.layout.game_layout.xml);
game_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_alignParentBotom="true"
/>
<your.package.GameView
android:id="#+id/gameScreen"
android:layout_alignParentTop="true"
android:layout_above="#id/adView"
/>
</RelativeLayout>