Error parsing XML: unbound prefix in custom view - android

I tried customizing a view from a library I'm using for my android app. The default xml code for this looks like this:
<it.gmariotti.cardslib.library.view.CardView
android:id="#+id/carddemo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"/>
This works without any problems. But when I add a new line in the element, to use my custom layout, I get an error in the opening line, that you can see in the title of this question: This is the line I was adding in the end:
card:card_layout_resourceID="#layout/custom_layout" />
This is also exactly the code that was used as an example in the docs of the library.

You probably forgot to "define" a namespace for the custom attribute card. So add this to your root-view in your xml-file:
xmlns:card="http://schemas.android.com/apk/res-auto"

Related

Namespace prefix “app” cannot find declaration

While I try to add wenchaojiang AndroidSwipeableCardStack library to my project. While I succeed to add this library but got an error in the following case.
In main Activity layout, I required adding the,
<com.wenchao.cardstack.CardStack
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center"
android:padding="10dp"
/>
Also I need to add the following with this
app:card_enable_loop="false"
app:card_enable_rotation="true"
app:card_gravity="top"
app:card_margin="20dp"
app:card_stack_size="4"
I got error as Decleration not found or error: attribute 'business.contacts.cardwithlib:card_enable_loop' not found.
I try many solutions like,
Include card 'com.android.support:cardview-v7:27.1.1' , add auto-generating xmlns:app="http://schemas.android.com/apk/res-auto" and Open their project and work fine for me in that project
I noticed that you are no including the Android's CardView but what it looks like a 3rd party library.
com.wenchao.cardstack.CardStack
Is this intentional? If so, maybe this component doesn't extend the CardView element, those specific style attributes might not be available.

Adding custom attribute to a view

I'm using a libary for creating a donut shaped pie chart.
https://www.numetriclabz.com/donut-chart-tutorial-using-numandroidcharts-library-tutorial/
In this provided tutorial under step 4 there is a custom attribute called custom:pieInnerCircleRatio being added to the donut chart item. However there is no specific explanation written, on how that is done.
This link will guide you: https://developer.android.com/training/custom-views/create-view.html
You need to define attrs.xml file as explained in the Define Custom Attributes section.
Add following line to your parent element in xml :
xmlns:custom="http://schemas.android.com/apk/res-auto"
Step 4 shows how to use the custom attribute. Unfortunately, the code snippets include literal HTML tags and entities. Here is a translated version:
<com.numetriclabz.numandroidcharts.PieChart
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:id="#+id/piegraph"
custom:pieInnerCircleRatio="128"/>

Android Studio Rendering fragment issue

I am, at the moment, trying to make a google maps app using android studio.
Right now, everything is fine, except for one thing, when i go to the "Design" Tab, in the XML file i have this Redering message:
Rendering Problems A tag allows a layout file to
dynamically include different layouts at runtime. At layout editing
time the specific layout to be used is not known. You can choose which
layout you would like previewed while editing the layout...
And the main problem is that I cannot use any of the gui components in my layout, I searched about my problem and i understood that with this error, people couldn't see their map but they could put on textfields, widgets, layouts, etc.
But for me, my preview is completly frozen and i can't do any modification.
Picture of my android studio page.
As you can change the fragments dynamically with your code, android studio doesn't know which layout to show in design time. This is the reason of your error.
To specifically tell android which layout to show, add tools:layout="#layout/Your_layout_name" attribute to your fragment.
There is also a shortcut link below the error description which you have told. Just click on the link and android will add it for you and you will see the fragment in your layout with no rendering error messages.
For a detailed example:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.insane.fragmenttest.MainActivity">
<fragment
android:id="#+id/testFragmentID"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.insane.fragmenttest.WorkOutDetails"
tools:layout="#layout/fragment_work_out_details" /> <!-- This is the line you want to add. -->
</LinearLayout>
Try using Designtime Layout Attributes. http://tools.android.com/tips/layout-designtime-attributes
These attributes guide Android Studio on how to render run-time attributes in the layout editor.
I think you should include tools:showIn="#layout/activity_maps" in your the <fragment> part of your google_maps_api.xml
Just use this design for the fragment. This solved my problem. Also, refer to this link
<fragment
android:id = "#+id/ma"
android:name = "com.google.android.gms.maps.SupportMapFragment"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "com.example.demomaps.MapsActivity"
/>
Yes I solved this easily..Simply ignore the message and click line that is showing below of the rendering messages.
Actually fragment contains layout inner and if its not include its showing warning that it must contains you can simply ignore this message below the warning then this rendering issue will be resolved.

app:something...What is app:?

I am using this famous ViewPagerIndicator library in my project. I wanted to add color on ViewPageIndicator. I found a solution from here on StackOverflow.
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
app:fillColor="#FF888888"
app:pageColor="#88FF0000"
app:strokeColor="#FF000000" />
But one thing I can't understand here: why it is written app: instead of android:? What does app: mean? I have never used app:? What is its namespace? I googled but no solution found.
For the namespace, add this: xmlns:app="http://schemas.android.com/apk/res-auto"
The app namespace is used for all 'new' attributes. For example: if I make a custom view, that has its own attributes (let's say RainbowView with new attribute 'numberOfColors'), then I have to use that attribute in the app namespace because it isn't declared in the android namespace. So use the app namespace for all custom attributes. Either attrs you defined yourself, or attrs the author of a library added (in your case for CirclePageIndicator). But most of the time, when you start typing 'strokeCo...' and you see 'strokeColor' in the autocomplete list, the namespace will be added automatically when you select it.
Probably you only need to add the missing namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
Or take a look at this answer for referencing a library:
How can I set up custom namespaces in layout file in Android studio?

Android custom xml tag in main.xml

I'm modifying the source code of a terminal emulator program for Android that contains the following confusing xml code in the linear layout of main.xml:
<com.vtrandal.bluesentry.EmulatorView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
This same xml code also appears in another xml file also in /res/layout along with main.xml. I can see that it appears to begin with a custom tag created using my package name. But why? How is this possible and why would someone want something like this?
When you extend a View to create a custom component, you need a way to add this objects to the xml layout. You do that by using the full class name, including the package name. The link to the dev guide the Nicholas posted previously explains it.
The attributes used inside that tag can be either attributes used for the View you are extending, or custom attributes defined in a new schema.
You can follow a nice tutorial about this here: http://www.anddev.org/creating_custom_views_-_the_togglebutton-t310.html
This is the syntax for creating custom components and views in Android.
Not sure what exactly is confusing. I'll assume its com.vtrandal.bluesentry.EmulatorView
part. This is name of the class that will be instantiated when inflating xml file.
If you got used to something like this:
<TextView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
Then you should know that this is because TextView is located in android.widget, and you should've wrote this:
<android.widget.TextView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
But because inflater knows about several special packages (android.widget, android.view, android.webkit) you can skip them in xml.

Categories

Resources