How to properly configure AdMob using IntelliJ IDEA? - android

I'm trying to incorporate AdMob into my Android application. I'm using IntelliJ IDEA for development and I cannot seem to set my project up properly. At this point I've done the following:
Did all the preliminary steps necessary to download the AdMob SDK.
Followed the directions here, trying to augment them for IntelliJ IDEA. I've added a Single-Entry Module Library dependency to the AdMob SDK to my project.
It looks like the IDE has no problem recognizing the classes from the SDK if I try to use them in code. However, it fails to resolve them in XML. I have the following two errors:
Cannot resolve symbol 'AdActivity' when I set up the ad activity in AndroidManifest.xml the instructions call for.
Element com.google.ads.AdView is not allowed here when I try to add an ad view to a layout in the manner documented here.
Thank you very much in advance for your help. I hope I've been clear.
EDIT
A clarification based on Cristian's answer. It's true that the first error seems to not matter. However, the second error causes the project build to break with the following message:
.../res/layout/main.xml:7: error: Error parsing XML: unbound prefix
The XML in question is the following layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="123dp"
android:src="#drawable/logo"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/cart"/>
<Button android:id="#+id/new_shopping_list"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="#string/new_shopping_list_btn"/>
<Button android:id="#+id/view_all_shopping_lists"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_below="#id/new_shopping_list"
android:text="#string/saved_shopping_lists_btn"/>
<ImageView android:id="#+id/copyright_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="7dp"
android:layout_marginRight="5dp"
android:src="#drawable/copyright"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/copyright_notice"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="4dp"
android:src="#drawable/techsmart_logo"/>
<ImageButton android:id="#+id/user_guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="7dp"
android:layout_marginLeft="5dp"
android:src="#drawable/user_guide"/>
</RelativeLayout>

Do not worry about those errors. AdMob library is obfuscated, thus IntelliJ cannot read the correct names of the classes. However, your application will compile and run fine. This is how one of my projects looks like, and they work fine:
As you can see, there are other libraries like Pontiflex or AirPush that has the same problem.
With regards to your second problem, it seems you forget to add the XML NameSpace. This answer says that you must add this one:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

Related

Code okay and yet I get error - XML document structures must start and end within the same entity

I am taking the first step into android programming and XML. I am using the Big Nerd Ranch Guide book. In Eclipse, I used the XML given in the book and I got the error XML document structures must start and end within the same entity.
How do I fix this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
</LinearLayout>
</LinearLayout>
Problem is solved. I removed the code and saved the xml file. After that, I replaced it with the above code and saved. The error is gone. It was probably a remanant from the time when "automatically build project" was enabled.
Strange.

Cannot resolve symbol 'container' - Android Studio

I just created a new projet, building some layouts, writing some code. Everything was fine except I got this error as shown in the picture below.
I tried to remove and type it again and when typing 'R.id.', there was no 'container' showing up in the list. So that means theres no problem with my R file, just that the 'container' word lost somewhere.
Those code above are actually default code in onCreate() method in your main activity everytime you create a new project in Android Studio. (I actually tried to create a new project to compare and yes it's the same, and without error as it's brand new.).
All I did was comment out the if statement block so the error goes away, and the application has run just fine on the emulator. I just don't know why I got that problem and how I can handle it as I may need to do that in the future. Thank you!
Edit after #Raghunandan has mentioned: Below is my xml.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/provide_information"
android:id="#+id/provideInfoButton"
android:layout_alignParentTop="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/take_picture"
android:id="#+id/takePictureButton"
android:layout_below="#+id/provideInfoButton"
android:layout_alignParentLeft="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/takePictureButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/classNameTextView"
android:layout_above="#+id/personNametextView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginBottom="38dp"
android:layout_above="#+id/emailTextView"
android:layout_alignLeft="#+id/emailTextView"
android:id="#+id/personNameTextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/emailTextView"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/classNameTextView"
android:layout_marginBottom="82dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/send_button"
android:id="#+id/sendButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
The reason is that I changed the root layout to RelativeLayout (the default of Android Studio was FrameLayout which has an id of "container"). My bad :). Thanks for mentioning about posting the xml file. I have not noticed that as I'm not as familiar with Android Studio as with Eclipse yet.

not able to add Google Admob

I wrote code for Admob. This is my layout.xml file 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"
android:background="#ffffff"
android:id="#+id/relativeLayoutHomeParent"
tools:context=".Home" xmlns:app="http://schemas.android.com/apk/lib/com.google.ads">
<RelativeLayout
android:id="#+id/relativeLayoutHomeTopBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:text="Home"
style="#style/screen_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageViewHomeSettings"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/set_settings"
android:layout_marginRight="10dp" />
</RelativeLayout>
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/relativeLayoutHomeTopBar"
android:background="#drawable/shadow" />
<ListView
android:id="#+id/listViewHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#BDBDBD"
android:dividerHeight="1dp"
android:layout_below="#+id/view1" >
</ListView>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
app:adSize="BANNER"
app:adUnitId="a1512f50d8c3692"
app:loadAdOnCreate="true"
app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
Output:
I want the bar that is on the top(yes that black bar), on the bottom of the screen. I also wrote for it but it showed the output as the above image.
Also i dont understand that which one is the admob banner, the top one or the bottom one ?? Can anyone please explain the difference these two bars. Actually i am new to this. This is the first time i've code for admob. Please help me to learn on this.
Maybe the ADK update has messed-up with your project's build-path. Try removing the admob JAR and add it again to your libs folder, add it to your Build Path and do a Clean Project.
EDIT following poster's comment about not having a libs folder:
You should try this:
Remove all references to the AdMob JAR in your project -> your code should not build without error anymore
Create a libs folder at the root of your project.
Copy the AdMob JAR into the libs folder
Make sure this AdMob JAR is in your Build Path (this will allow your code to build wihtout error)
(If still not running OK) Right click your project > Android Tools > Fix Project Properties

RelativeLayout not working properly Android

I have the next problem: I use a lot the RelativeLayout, like the Android documentation is telling me to do. But, since few days, I don´t understand why, this is not working anymore:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/boton"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000" />
</RelativeLayout>
This is my layout, very simple. So, according to this one, the textView should be BELOW the button, but, instead, it appears OVER the button!
.
Сan anyone explain me what happens?
EDIT: The problem is not when I try to run it on a device, the problem comes when I see it on the graphical layout of the ADT Plugin for Eclipse...
I try this code it's working properly.
If your side not working properly Try to Clean build your project and then Run.
If still having problem then Try this
android:layout_below="#+id/boton"
remove above line with this
android:layout_below="#id/boton"
Because "+" create new reference to R.java file
This may be the issue.
try by removing the below line from your code
android:layout_marginTop="60dp"
Take the '+' sign out of the TextView's layout_below attribute. The '+' tells it to deine a new id. Without the '+' it should refer to the existing one.
I think your issue is the drawable for the background, since I am not seeing an image in your preview.
Having this issue is giving you a build error so the preview doesn't show the layout in its current state (it reverts to the last sucessful build). Either remove that line and save to see the preview or check to make sure that you have an image in your drawables folder titled "background"
Try this, without the drawable to check if it is working for you:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/boton"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000" />
</RelativeLayout>
I am trying to run it, and now, I know what´s the problem.
When running the app in a terminal, it is working properly, the problems comes when I try to see it on the graphical layout on the ADT plugin on Eclipse! Anyone with the same problem??
Add </RelativeLayout> at the end of your relative layout to close it.

RelativeLayout - Couldnt resolve resource

I have a relative layout which works fine at runtime but in the Eclipse graphical layout designer I receive the exception
Couldn't resolve resource #id/imgSessionOutcome_Product1_Icon
The resource of the imageview is defined and is before the references to it, so I can't quite see what the issue is ? The exception is listed twice so both references to it fail.
I've saved/cleaned project to make sure resources rebuilt, still same error.
Eclipse Version: Helios Service Release 2 running on OSX with latest updates etc.
<RelativeLayout
android:id="#+id/lySessionOutcome_Product1"
android:layout_width="match_parent"
android:layout_height="55dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="#+id/imgSessionOutcome_Product1_Icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dip"
android:src="#drawable/gr_bag_bc" />
<TextView
android:id="#+id/tvSessionOutcome_Product1_Description"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/imgSessionOutcome_Product1_Icon"
android:ellipsize="marquee"
android:singleLine="false"
android:text="Description" />
<TextView
android:id="#+id/tvSessionOutcome_Product1_Type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/tvSessionOutcome_Product1_Description"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/imgSessionOutcome_Product1_Icon"
android:gravity="center_vertical"
android:text="Product type"
android:textAppearance="#style/largeBlack" />
</RelativeLayout>
try using
android:layout_toRightOf="#+id/imgSessionOutcome_Product1_Icon"
Well I answered this long time ago I didn't knew why it works then and I am still not sure but based on my observation I think using + sign instead creates a temporary reference to the view so that we can view it in Android layout editor, actual anchor view gets picked at runtime only.
If you wont use the + sign it works fine at runtime as stated in the question itself.
Please correct me if I am wrong.

Categories

Resources