For android development, we need XML and I have a couple of doubts about the syntax.
Here's sample code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="#+id/vMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="false"
ads:adUnitId="#string/banner_ad_unit_id"
ads:adSize="BANNER"
/>
</RelativeLayout>
</RelativeLayout>
On line 8, there is a >, could someone explain the significance of that? If I remove it, the editor complains because it seems to be invalid syntax.
Also, on line 12, if I replace the /> with the full closing tage of </RelativeLayout>, it will not work. Why?
Thanks!
The '>' on line 8 closes the first <RelativeLayout node. '/>' is short hand for closing a node that has no children. The last </RelativeLayout> tag closes that first one. You can't replace it with '/>' because it has child nodes in it.
Related
I have a problem. I was trying to create an app, but I get an error and I really have no idea how to fix it. It gives a red cross before EditText, and I think it has to do with how everything is opened/closed, but I just can't find a problem.
<LinearLayout
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:orientation="horizontal"
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
/>
Would like to hear from you.
this is your problem
you left the > closing sign on your linear layout.
this is the right way
<LinearLayout
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:orientation="horizontal" >
<!--items go here -->
</LinearLayout>
LinearLayout should have an open and close tag:
<LinearLayout
attributes here...>
....stuf there...
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
android:background="#ff000640">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/adView"
/>
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
Why is it red? When I run the app the adview works just fine so I have no idea why it wouldn't be working. The red part also disappears for a short while once I run the app.
when I change above to below it gives no error
#+id/adView : the + sign means to generate a new id if it does not exist yet.
I think it would be better to
<WebView ...
android:layout_above="#id/adView"
meaning to refer to another component with id adView
and as Krishnabhadra suggests, put the AdView declaration first.
#+id/ is used to create an id. To reference it use #id/. Also put the code for the adview above the code for the webview, as the id needs to be defined, before you can reference it.
Try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
android:background="#ff000640">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_above="#id/adView"
/>
</RelativeLayout>
I think you didn't import Google Admob Library. Please import this library into your project and try again. Click Here
Because when I try your code, any red lines appear. Just import the library.
Try File->Invalidate Caches.... I've run into this before when AS doesn't re-generate the data bindings when editing layouts. Same thing sometimes happens for new string resources.
I am stuck in a problem and search alot but could'nt find the solution here is my code
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
app:adSize="BANNER"
app:adUnitId="xxxxxxxxxx" >
</com.google.ads.AdView>
This code show
Error parsing XML:unbound prefix
when i remove app:adUnitId and app:adsize it do not give any error, how can i remove this error?
Your error is typographical, you're setting:
app:adSize="BANNER"
app:adUnitId="xxxxxxxxxx"
But it should be:
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxxx"
xmlns:ads="http://schemas.android.com/apk/res-auto"
Add this to your parent tag.
In my case.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:orientation="vertical" >
Hope this will work.
I tried to use the include tag to reuse the layout code from the activity_main.xml and contact_details.xml during landscape mode. I would like to display the activity_main on the left and contact_details on the right. But the application crashes when I switch to landscape mode.
This is the coding for the activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/btnAddContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="0"
android:text="Add Contact" />
</LinearLayout>
This is the coding for contact_details.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvContactDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
This is the coding for my layout-land/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<include layout="#layout/activity_main"/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#A3A3A3"/>
<include layout="#layout/contact_details"/>
</LinearLayout>
Any tips for resolving this problem? Thanks in advance.
Thanks Adinia for the suggestion. It was because I should not call back the same Layout name, which is activity_main.xml. This is the main cause of the crash. it might be a circular reference as Adinia had stated. Thanks again. By the way, I am only allowed to accept my answer in 2 days.
In your activity_main.xml ,contact_details.xml you are using
android:layout_width="fill_parent"
instead that try using
android:layout_width="wrap_content"
If not post you logcat report
I am trying to implement AdMob in my Application. But dont know somehow its showing this error and my R.java file is not being generated due to it. I have tried all the ways to solve this problem, like Clean,Build, Build All. But non is working for me.
Following my code snippet in which its showing error "Error in parsing XML: Unbound prefix"
<?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:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:background="#color/bgcolor">
<LinearLayout
android:id="#+id/Linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="XXX"
ads:refreshInterval="60"/>
</LinearLayout>
Please help me out. I am stuck here :(
Probably a namespace issue. You have to define the namespace.
try adding
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
New Admob SDK (Google Play services) requiried another namespace
xmlns:ads="http://schemas.android.com/apk/res-auto"
<?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:layout_width="fill_parent" android:id="#+id/rltvLayoutPromote"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/linearLayoutwebview"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical">
<WebView android:id="#+id/webViewPromote"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fitsSystemWindows="true" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:id="#+id/ad_layout" android:layout_height="wrap_content"
android:gravity="bottom" android:layout_alignParentBottom="true"
android:layout_alignBottom="#+id/home_layout">
<com.google.ads.AdView android:layout_width="wrap_content"
android:layout_height="wrap_content" ads:adUnitId="XXXXXXXXXX"
ads:adSize="BANNER" android:id="#+id/adView" ads:refreshInterval="60" />
<!-- put 3 if not working-->
</LinearLayout>
</RelativeLayout>
and put this lines in manifest.xml file
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" />
the above code is working perfectly for me... visit this site for complete reference help forandroid-admobThanks Pragna